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