93ac9d62bc8247d426a2747f6af907e296ec0c6e
[akkoma] / lib / pleroma / http / http.ex
1 defmodule Pleroma.HTTP do
2 require HTTPoison
3 alias Pleroma.HTTP.Connection
4 alias Pleroma.HTTP.RequestBuilder, as: Builder
5
6 def request(method, url, body \\ "", headers \\ [], options \\ []) do
7 options =
8 process_request_options(options)
9 |> process_sni_options(url)
10
11 %{}
12 |> Builder.method(method)
13 |> Builder.headers(headers)
14 |> Builder.opts(options)
15 |> Builder.url(url)
16 |> Builder.add_param(:body, :body, body)
17 |> Enum.into([])
18 |> (&Tesla.request(Connection.new(), &1)).()
19 end
20
21 defp process_sni_options(options, url) do
22 uri = URI.parse(url)
23 host = uri.host |> to_charlist()
24
25 case uri.scheme do
26 "https" -> options ++ [ssl: [server_name_indication: host]]
27 _ -> options
28 end
29 end
30
31 def process_request_options(options) do
32 config = Application.get_env(:pleroma, :http, [])
33 proxy = Keyword.get(config, :proxy_url, nil)
34 options = options ++ [adapter: [pool: :default]]
35
36 case proxy do
37 nil -> options
38 _ -> options ++ [proxy: proxy]
39 end
40 end
41
42 def get(url, headers \\ [], options \\ []),
43 do: request(:get, url, "", headers, options)
44
45 def post(url, body, headers \\ [], options \\ []),
46 do: request(:post, url, body, headers, options)
47 end