38
lib/service/http/activity_pub.ex
Normal file
38
lib/service/http/activity_pub.ex
Normal file
@@ -0,0 +1,38 @@
|
||||
defmodule Mobilizon.Service.HTTP.ActivityPub do
|
||||
@moduledoc """
|
||||
Tesla HTTP Client that is preconfigured to get and post ActivityPub content
|
||||
"""
|
||||
|
||||
alias Mobilizon.Config
|
||||
|
||||
@adapter Application.get_env(:tesla, __MODULE__, [])[:adapter] || Tesla.Adapter.Hackney
|
||||
@default_opts [
|
||||
recv_timeout: 20_000
|
||||
]
|
||||
@user_agent Config.instance_user_agent()
|
||||
|
||||
def client(options \\ []) do
|
||||
headers = Keyword.get(options, :headers, [])
|
||||
opts = Keyword.merge(@default_opts, Keyword.get(options, :opts, []))
|
||||
|
||||
middleware = [
|
||||
{Tesla.Middleware.Headers,
|
||||
[{"User-Agent", @user_agent}, {"Accept", "application/activity+json"}] ++ headers},
|
||||
Tesla.Middleware.FollowRedirects,
|
||||
{Tesla.Middleware.Timeout, timeout: 10_000},
|
||||
{Tesla.Middleware.JSON, decode_content_types: "application/activity+json"}
|
||||
]
|
||||
|
||||
adapter = {@adapter, opts}
|
||||
|
||||
Tesla.client(middleware, adapter)
|
||||
end
|
||||
|
||||
def get(client, url) do
|
||||
Tesla.get(client, url)
|
||||
end
|
||||
|
||||
def post(client, url, data) do
|
||||
Tesla.post(client, url, data)
|
||||
end
|
||||
end
|
||||
30
lib/service/http/base_client.ex
Normal file
30
lib/service/http/base_client.ex
Normal file
@@ -0,0 +1,30 @@
|
||||
defmodule Mobilizon.Service.HTTP.BaseClient do
|
||||
@moduledoc """
|
||||
Tesla HTTP Basic Client
|
||||
"""
|
||||
|
||||
use Tesla
|
||||
alias Mobilizon.Config
|
||||
|
||||
@default_opts [
|
||||
recv_timeout: 20_000
|
||||
]
|
||||
|
||||
adapter(Tesla.Adapter.Hackney, @default_opts)
|
||||
|
||||
@user_agent Config.instance_user_agent()
|
||||
|
||||
plug(Tesla.Middleware.FollowRedirects)
|
||||
|
||||
plug(Tesla.Middleware.Timeout, timeout: 10_000)
|
||||
|
||||
plug(Tesla.Middleware.Headers, [{"User-Agent", @user_agent}])
|
||||
|
||||
def get(url) do
|
||||
get(url)
|
||||
end
|
||||
|
||||
def post(url, data) do
|
||||
post(url, data)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user