1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.HTTP do
7 Wrapper for `Tesla.request/2`.
10 alias Pleroma.HTTP.Connection
11 alias Pleroma.HTTP.Request
12 alias Pleroma.HTTP.RequestBuilder, as: Builder
19 @type method() :: :get | :post | :put | :delete | :head
24 See `Pleroma.HTTP.request/5`
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)
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)
36 Performs POST request.
38 See `Pleroma.HTTP.request/5`
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)
46 Builds and performs http request.
49 `method` - :get, :post, :put, :delete, :head
52 `headers` - a keyworld list of headers, e.g. `[{"content-type", "text/plain"}]`
53 `options` - custom, per-request middleware or adapter options
56 `{:ok, %Tesla.Env{}}` or `{:error, error}`
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
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)
68 adapter = Application.get_env(:tesla, :adapter)
69 client = Tesla.client([Tesla.Middleware.FollowRedirects], adapter)
71 pid = Process.whereis(adapter_opts[:pool])
74 if adapter == Tesla.Adapter.Gun && pid do
83 |> Map.put(:env, Pleroma.Config.get([:env]))
84 |> Map.put(:pool_alive?, pool_alive?)
86 response = request(client, request, request_opts)
88 Connection.after_request(adapter_opts)
93 @spec request(Client.t(), keyword(), map()) :: {:ok, Env.t()} | {:error, any()}
94 def request(%Client{} = client, request, %{env: :test}), do: request(client, request)
96 def request(%Client{} = client, request, %{body_as: :chunks}), do: request(client, request)
98 def request(%Client{} = client, request, %{pool_alive?: false}), do: request(client, request)
100 def request(%Client{} = client, request, %{pool: pool, timeout: timeout}) do
101 :poolboy.transaction(
103 &Pleroma.Pool.Request.execute(&1, client, request, timeout),
108 @spec request(Client.t(), keyword()) :: {:ok, Env.t()} | {:error, any()}
109 def request(client, request), do: Tesla.request(client, request)
111 defp build_request(method, headers, options, url, body, params) do
113 |> Builder.method(method)
114 |> Builder.headers(headers)
115 |> Builder.opts(options)
117 |> Builder.add_param(:body, :body, body)
118 |> Builder.add_param(:query, :query, params)
119 |> Builder.convert_to_keyword()