Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[akkoma] / lib / pleroma / http / http.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.HTTP do
6 @moduledoc """
7
8 """
9
10 alias Pleroma.HTTP.Connection
11 alias Pleroma.HTTP.RequestBuilder, as: Builder
12
13 @type t :: __MODULE__
14
15 @doc """
16 Builds and perform http request.
17
18 # Arguments:
19 `method` - :get, :post, :put, :delete
20 `url`
21 `body`
22 `headers` - a keyworld list of headers, e.g. `[{"content-type", "text/plain"}]`
23 `options` - custom, per-request middleware or adapter options
24
25 # Returns:
26 `{:ok, %Tesla.Env{}}` or `{:error, error}`
27
28 """
29 def request(method, url, body \\ "", headers \\ [], options \\ []) do
30 try do
31 options =
32 process_request_options(options)
33 |> process_sni_options(url)
34
35 params = Keyword.get(options, :params, [])
36
37 %{}
38 |> Builder.method(method)
39 |> Builder.headers(headers)
40 |> Builder.opts(options)
41 |> Builder.url(url)
42 |> Builder.add_param(:body, :body, body)
43 |> Builder.add_param(:query, :query, params)
44 |> Enum.into([])
45 |> (&Tesla.request(Connection.new(options), &1)).()
46 rescue
47 e ->
48 {:error, e}
49 catch
50 :exit, e ->
51 {:error, e}
52 end
53 end
54
55 defp process_sni_options(options, nil), do: options
56
57 defp process_sni_options(options, url) do
58 uri = URI.parse(url)
59 host = uri.host |> to_charlist()
60
61 case uri.scheme do
62 "https" -> options ++ [ssl: [server_name_indication: host]]
63 _ -> options
64 end
65 end
66
67 def process_request_options(options) do
68 Keyword.merge(Pleroma.HTTP.Connection.hackney_options([]), options)
69 end
70
71 @doc """
72 Performs GET request.
73
74 See `Pleroma.HTTP.request/5`
75 """
76 def get(url, headers \\ [], options \\ []),
77 do: request(:get, url, "", headers, options)
78
79 @doc """
80 Performs POST request.
81
82 See `Pleroma.HTTP.request/5`
83 """
84 def post(url, body, headers \\ [], options \\ []),
85 do: request(:post, url, body, headers, options)
86 end