2020-07-09 17:24:28 +02:00
|
|
|
defmodule Mobilizon.Service.HTTP.ActivityPub do
|
|
|
|
@moduledoc """
|
|
|
|
Tesla HTTP Client that is preconfigured to get and post ActivityPub content
|
|
|
|
"""
|
|
|
|
|
|
|
|
alias Mobilizon.Config
|
|
|
|
|
|
|
|
@default_opts [
|
|
|
|
recv_timeout: 20_000
|
|
|
|
]
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec client(Keyword.t()) :: Tesla.Client.t()
|
2020-07-09 17:24:28 +02:00
|
|
|
def client(options \\ []) do
|
|
|
|
headers = Keyword.get(options, :headers, [])
|
2021-04-09 10:43:45 +02:00
|
|
|
adapter = Application.get_env(:tesla, __MODULE__, [])[:adapter] || Tesla.Adapter.Hackney
|
2020-07-09 17:24:28 +02:00
|
|
|
opts = Keyword.merge(@default_opts, Keyword.get(options, :opts, []))
|
|
|
|
|
|
|
|
middleware = [
|
|
|
|
{Tesla.Middleware.Headers,
|
2021-04-09 10:43:45 +02:00
|
|
|
[{"User-Agent", Config.instance_user_agent()}, {"Accept", "application/activity+json"}] ++
|
|
|
|
headers},
|
2020-07-09 17:24:28 +02:00
|
|
|
Tesla.Middleware.FollowRedirects,
|
|
|
|
{Tesla.Middleware.Timeout, timeout: 10_000},
|
2020-12-17 17:32:12 +01:00
|
|
|
{Tesla.Middleware.JSON,
|
|
|
|
decode_content_types: ["application/activity+json", "application/ld+json"]}
|
2020-07-09 17:24:28 +02:00
|
|
|
]
|
|
|
|
|
2021-04-09 10:43:45 +02:00
|
|
|
Tesla.client(middleware, {adapter, opts})
|
2020-07-09 17:24:28 +02:00
|
|
|
end
|
|
|
|
|
2021-10-05 16:04:50 +02:00
|
|
|
@spec get(Tesla.Client.t(), String.t()) :: Tesla.Env.result()
|
2020-07-09 17:24:28 +02:00
|
|
|
def get(client, url) do
|
|
|
|
Tesla.get(client, url)
|
|
|
|
end
|
|
|
|
|
2021-10-05 16:04:50 +02:00
|
|
|
@spec post(Tesla.Client.t(), String.t(), map() | String.t()) :: Tesla.Env.result()
|
2020-07-09 17:24:28 +02:00
|
|
|
def post(client, url, data) do
|
|
|
|
Tesla.post(client, url, data)
|
|
|
|
end
|
|
|
|
end
|