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
32 process_request_options(options)
33 |> process_sni_options(url)
35 params = Keyword.get(options, :params, [])
38 |> Builder.method(method)
39 |> Builder.headers(headers)
40 |> Builder.opts(options)
42 |> Builder.add_param(:body, :body, body)
43 |> Builder.add_param(:query, :query, params)
45 |> (&Tesla.request(Connection.new(options), &1)).()
55 defp process_sni_options(options, nil), do: options
57 defp process_sni_options(options, url) do
59 host = uri.host |> to_charlist()
62 "https" -> options ++ [ssl: [server_name_indication: host]]
67 def process_request_options(options) do
68 config = Application.get_env(:pleroma, :http, [])
69 proxy = Keyword.get(config, :proxy_url, nil)
73 _ -> options ++ [proxy: proxy]
80 See `Pleroma.HTTP.request/5`
82 def get(url, headers \\ [], options \\ []),
83 do: request(:get, url, "", headers, options)
86 Performs POST request.
88 See `Pleroma.HTTP.request/5`
90 def post(url, body, headers \\ [], options \\ []),
91 do: request(:post, url, body, headers, options)