Use runtime configuration for HTTP clients user-agent
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
parent
e991d7d373
commit
5ac02bae5d
|
@ -5,28 +5,26 @@ defmodule Mobilizon.Service.HTTP.ActivityPub do
|
||||||
|
|
||||||
alias Mobilizon.Config
|
alias Mobilizon.Config
|
||||||
|
|
||||||
@adapter Application.get_env(:tesla, __MODULE__, [])[:adapter] || Tesla.Adapter.Hackney
|
|
||||||
@default_opts [
|
@default_opts [
|
||||||
recv_timeout: 20_000
|
recv_timeout: 20_000
|
||||||
]
|
]
|
||||||
@user_agent Config.instance_user_agent()
|
|
||||||
|
|
||||||
def client(options \\ []) do
|
def client(options \\ []) do
|
||||||
headers = Keyword.get(options, :headers, [])
|
headers = Keyword.get(options, :headers, [])
|
||||||
|
adapter = Application.get_env(:tesla, __MODULE__, [])[:adapter] || Tesla.Adapter.Hackney
|
||||||
opts = Keyword.merge(@default_opts, Keyword.get(options, :opts, []))
|
opts = Keyword.merge(@default_opts, Keyword.get(options, :opts, []))
|
||||||
|
|
||||||
middleware = [
|
middleware = [
|
||||||
{Tesla.Middleware.Headers,
|
{Tesla.Middleware.Headers,
|
||||||
[{"User-Agent", @user_agent}, {"Accept", "application/activity+json"}] ++ headers},
|
[{"User-Agent", Config.instance_user_agent()}, {"Accept", "application/activity+json"}] ++
|
||||||
|
headers},
|
||||||
Tesla.Middleware.FollowRedirects,
|
Tesla.Middleware.FollowRedirects,
|
||||||
{Tesla.Middleware.Timeout, timeout: 10_000},
|
{Tesla.Middleware.Timeout, timeout: 10_000},
|
||||||
{Tesla.Middleware.JSON,
|
{Tesla.Middleware.JSON,
|
||||||
decode_content_types: ["application/activity+json", "application/ld+json"]}
|
decode_content_types: ["application/activity+json", "application/ld+json"]}
|
||||||
]
|
]
|
||||||
|
|
||||||
adapter = {@adapter, opts}
|
Tesla.client(middleware, {adapter, opts})
|
||||||
|
|
||||||
Tesla.client(middleware, adapter)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def get(client, url) do
|
def get(client, url) do
|
||||||
|
|
|
@ -13,13 +13,11 @@ defmodule Mobilizon.Service.HTTP.GeospatialClient do
|
||||||
|
|
||||||
adapter(Tesla.Adapter.Hackney, @default_opts)
|
adapter(Tesla.Adapter.Hackney, @default_opts)
|
||||||
|
|
||||||
@user_agent Config.instance_user_agent()
|
|
||||||
|
|
||||||
plug(Tesla.Middleware.FollowRedirects)
|
plug(Tesla.Middleware.FollowRedirects)
|
||||||
|
|
||||||
plug(Tesla.Middleware.Timeout, timeout: 10_000)
|
plug(Tesla.Middleware.Timeout, timeout: 10_000)
|
||||||
|
|
||||||
plug(Tesla.Middleware.Headers, [{"User-Agent", @user_agent}])
|
plug(Tesla.Middleware.Headers, [{"User-Agent", Config.instance_user_agent()}])
|
||||||
|
|
||||||
plug(Tesla.Middleware.JSON)
|
plug(Tesla.Middleware.JSON)
|
||||||
end
|
end
|
||||||
|
|
|
@ -12,11 +12,9 @@ defmodule Mobilizon.Service.HTTP.RemoteMediaDownloaderClient do
|
||||||
|
|
||||||
adapter(Tesla.Adapter.Hackney, @default_opts)
|
adapter(Tesla.Adapter.Hackney, @default_opts)
|
||||||
|
|
||||||
@user_agent Config.instance_user_agent()
|
|
||||||
|
|
||||||
plug(Tesla.Middleware.FollowRedirects)
|
plug(Tesla.Middleware.FollowRedirects)
|
||||||
|
|
||||||
plug(Tesla.Middleware.Timeout, timeout: 10_000)
|
plug(Tesla.Middleware.Timeout, timeout: 10_000)
|
||||||
|
|
||||||
plug(Tesla.Middleware.Headers, [{"User-Agent", @user_agent}])
|
plug(Tesla.Middleware.Headers, [{"User-Agent", Config.instance_user_agent()}])
|
||||||
end
|
end
|
||||||
|
|
|
@ -12,11 +12,9 @@ defmodule Mobilizon.Service.HTTP.RichMediaPreviewClient do
|
||||||
|
|
||||||
adapter(Tesla.Adapter.Hackney, @default_opts)
|
adapter(Tesla.Adapter.Hackney, @default_opts)
|
||||||
|
|
||||||
@user_agent Config.instance_user_agent()
|
|
||||||
|
|
||||||
plug(Tesla.Middleware.FollowRedirects)
|
plug(Tesla.Middleware.FollowRedirects)
|
||||||
|
|
||||||
plug(Tesla.Middleware.Timeout, timeout: 10_000)
|
plug(Tesla.Middleware.Timeout, timeout: 10_000)
|
||||||
|
|
||||||
plug(Tesla.Middleware.Headers, [{"User-Agent", @user_agent}])
|
plug(Tesla.Middleware.Headers, [{"User-Agent", Config.instance_user_agent()}])
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,14 +13,12 @@ defmodule Mobilizon.Service.HTTP.WebfingerClient do
|
||||||
|
|
||||||
adapter(Tesla.Adapter.Hackney, @default_opts)
|
adapter(Tesla.Adapter.Hackney, @default_opts)
|
||||||
|
|
||||||
@user_agent Config.instance_user_agent()
|
|
||||||
|
|
||||||
plug(Tesla.Middleware.FollowRedirects)
|
plug(Tesla.Middleware.FollowRedirects)
|
||||||
|
|
||||||
plug(Tesla.Middleware.Timeout, timeout: 10_000)
|
plug(Tesla.Middleware.Timeout, timeout: 10_000)
|
||||||
|
|
||||||
plug(Tesla.Middleware.Headers, [
|
plug(Tesla.Middleware.Headers, [
|
||||||
{"User-Agent", @user_agent},
|
{"User-Agent", Config.instance_user_agent()},
|
||||||
{"Accept", "application/json, application/activity+json, application/jrd+json"}
|
{"Accept", "application/json, application/activity+json, application/jrd+json"}
|
||||||
])
|
])
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue