mobilizon.chapril.org-mobil.../lib/service/http/generic_json_client.ex
Thomas Citharel ee20e03cc2
Migrate to Vue 3 and Vite
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
2022-08-11 16:46:31 +02:00

40 lines
1.1 KiB
Elixir

defmodule Mobilizon.Service.HTTP.GenericJSONClient do
@moduledoc """
Tesla HTTP Client that is preconfigured to get JSON content
"""
alias Mobilizon.Config
@default_opts [
recv_timeout: 20_000
]
@spec client(Keyword.t()) :: Tesla.Client.t()
def client(options \\ []) do
headers = Keyword.get(options, :headers, [])
adapter = Application.get_env(:tesla, __MODULE__, [])[:adapter] || Tesla.Adapter.Hackney
opts = Keyword.merge(@default_opts, Keyword.get(options, :opts, []))
middleware = [
{Tesla.Middleware.Headers,
[{"User-Agent", Config.instance_user_agent()}] ++
headers},
Tesla.Middleware.FollowRedirects,
{Tesla.Middleware.Timeout, timeout: 10_000},
Tesla.Middleware.JSON
]
Tesla.client(middleware, {adapter, opts})
end
@spec get(Tesla.Client.t(), String.t()) :: Tesla.Env.result()
def get(client, url) do
Tesla.get(client, url)
end
@spec post(Tesla.Client.t(), String.t(), map() | String.t()) :: Tesla.Env.result()
def post(client, url, data) do
Tesla.post(client, url, data)
end
end