Merge branch 'develop' into activation-meta
[akkoma] / lib / pleroma / http / http.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 do
6 @moduledoc """
7 Wrapper for `Tesla.request/2`.
8 """
9
10 alias Pleroma.HTTP.Connection
11 alias Pleroma.HTTP.Request
12 alias Pleroma.HTTP.RequestBuilder, as: Builder
13 alias Tesla.Client
14 alias Tesla.Env
15
16 require Logger
17
18 @type t :: __MODULE__
19 @type method() :: :get | :post | :put | :delete | :head
20
21 @doc """
22 Performs GET request.
23
24 See `Pleroma.HTTP.request/5`
25 """
26 @spec get(Request.url() | nil, Request.headers(), keyword()) ::
27 nil | {:ok, Env.t()} | {:error, any()}
28 def get(url, headers \\ [], options \\ [])
29 def get(nil, _, _), do: nil
30 def get(url, headers, options), do: request(:get, url, "", headers, options)
31
32 @spec head(Request.url(), Request.headers(), keyword()) :: {:ok, Env.t()} | {:error, any()}
33 def head(url, headers \\ [], options \\ []), do: request(:head, url, "", headers, options)
34
35 @doc """
36 Performs POST request.
37
38 See `Pleroma.HTTP.request/5`
39 """
40 @spec post(Request.url(), String.t(), Request.headers(), keyword()) ::
41 {:ok, Env.t()} | {:error, any()}
42 def post(url, body, headers \\ [], options \\ []),
43 do: request(:post, url, body, headers, options)
44
45 @doc """
46 Builds and performs http request.
47
48 # Arguments:
49 `method` - :get, :post, :put, :delete, :head
50 `url` - full url
51 `body` - request body
52 `headers` - a keyworld list of headers, e.g. `[{"content-type", "text/plain"}]`
53 `options` - custom, per-request middleware or adapter options
54
55 # Returns:
56 `{:ok, %Tesla.Env{}}` or `{:error, error}`
57
58 """
59 @spec request(method(), Request.url(), String.t(), Request.headers(), keyword()) ::
60 {:ok, Env.t()} | {:error, any()}
61 def request(method, url, body, headers, options) when is_binary(url) do
62 uri = URI.parse(url)
63 adapter_opts = Connection.options(uri, options[:adapter] || [])
64 options = put_in(options[:adapter], adapter_opts)
65 params = options[:params] || []
66 request = build_request(method, headers, options, url, body, params)
67
68 adapter = Application.get_env(:tesla, :adapter)
69 client = Tesla.client([Tesla.Middleware.FollowRedirects], adapter)
70
71 pid = Process.whereis(adapter_opts[:pool])
72
73 pool_alive? =
74 if adapter == Tesla.Adapter.Gun && pid do
75 Process.alive?(pid)
76 else
77 false
78 end
79
80 request_opts =
81 adapter_opts
82 |> Enum.into(%{})
83 |> Map.put(:env, Pleroma.Config.get([:env]))
84 |> Map.put(:pool_alive?, pool_alive?)
85
86 response = request(client, request, request_opts)
87
88 Connection.after_request(adapter_opts)
89
90 response
91 end
92
93 @spec request(Client.t(), keyword(), map()) :: {:ok, Env.t()} | {:error, any()}
94 def request(%Client{} = client, request, %{env: :test}), do: request(client, request)
95
96 def request(%Client{} = client, request, %{body_as: :chunks}), do: request(client, request)
97
98 def request(%Client{} = client, request, %{pool_alive?: false}), do: request(client, request)
99
100 def request(%Client{} = client, request, %{pool: pool, timeout: timeout}) do
101 :poolboy.transaction(
102 pool,
103 &Pleroma.Pool.Request.execute(&1, client, request, timeout),
104 timeout
105 )
106 end
107
108 @spec request(Client.t(), keyword()) :: {:ok, Env.t()} | {:error, any()}
109 def request(client, request), do: Tesla.request(client, request)
110
111 defp build_request(method, headers, options, url, body, params) do
112 Builder.new()
113 |> Builder.method(method)
114 |> Builder.headers(headers)
115 |> Builder.opts(options)
116 |> Builder.url(url)
117 |> Builder.add_param(:body, :body, body)
118 |> Builder.add_param(:query, :query, params)
119 |> Builder.convert_to_keyword()
120 end
121 end