Merge branch 'fix-auto-link-for-profile-fields' into 'develop'
[akkoma] / lib / pleroma / http / adapter_helper.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.HTTP.AdapterHelper do
6 alias Pleroma.HTTP.Connection
7
8 @type proxy ::
9 {Connection.host(), pos_integer()}
10 | {Connection.proxy_type(), Connection.host(), pos_integer()}
11
12 @callback options(keyword(), URI.t()) :: keyword()
13 @callback after_request(keyword()) :: :ok
14
15 @spec options(keyword(), URI.t()) :: keyword()
16 def options(opts, _uri) do
17 proxy = Pleroma.Config.get([:http, :proxy_url], nil)
18 maybe_add_proxy(opts, format_proxy(proxy))
19 end
20
21 @spec maybe_get_conn(URI.t(), keyword()) :: keyword()
22 def maybe_get_conn(_uri, opts), do: opts
23
24 @spec after_request(keyword()) :: :ok
25 def after_request(_opts), do: :ok
26
27 @spec format_proxy(String.t() | tuple() | nil) :: proxy() | nil
28 def format_proxy(nil), do: nil
29
30 def format_proxy(proxy_url) do
31 case Connection.parse_proxy(proxy_url) do
32 {:ok, host, port} -> {host, port}
33 {:ok, type, host, port} -> {type, host, port}
34 _ -> nil
35 end
36 end
37
38 @spec maybe_add_proxy(keyword(), proxy() | nil) :: keyword()
39 def maybe_add_proxy(opts, nil), do: opts
40 def maybe_add_proxy(opts, proxy), do: Keyword.put_new(opts, :proxy, proxy)
41 end