1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.HTTP do
10 alias Pleroma.HTTP.Connection
11 alias Pleroma.HTTP.RequestBuilder, as: Builder
16 Builds and perform http request.
19 `method` - :get, :post, :put, :delete
22 `headers` - a keyworld list of headers, e.g. `[{"content-type", "text/plain"}]`
23 `options` - custom, per-request middleware or adapter options
26 `{:ok, %Tesla.Env{}}` or `{:error, error}`
29 def request(method, url, body \\ "", headers \\ [], options \\ []) do
31 process_request_options(options)
32 |> process_sni_options(url)
35 |> Builder.method(method)
36 |> Builder.headers(headers)
37 |> Builder.opts(options)
39 |> Builder.add_param(:body, :body, body)
41 |> (&Tesla.request(Connection.new(), &1)).()
44 defp process_sni_options(options, nil), do: options
46 defp process_sni_options(options, url) do
48 host = uri.host |> to_charlist()
51 "https" -> options ++ [ssl: [server_name_indication: host]]
56 def process_request_options(options) do
57 config = Application.get_env(:pleroma, :http, [])
58 proxy = Keyword.get(config, :proxy_url, nil)
62 _ -> options ++ [proxy: proxy]
69 See `Pleroma.HTTP.request/5`
71 def get(url, headers \\ [], options \\ []),
72 do: request(:get, url, "", headers, options)
75 Performs POST request.
77 See `Pleroma.HTTP.request/5`
79 def post(url, body, headers \\ [], options \\ []),
80 do: request(:post, url, body, headers, options)