distraction.party/lib/pleroma/reverse_proxy/client.ex

39 lines
1.3 KiB
Elixir
Raw Normal View History

# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
2019-07-09 16:54:13 +00:00
defmodule Pleroma.ReverseProxy.Client do
2020-02-11 07:12:57 +00:00
@type status :: pos_integer()
@type header_name :: String.t()
@type header_value :: String.t()
@type headers :: [{header_name(), header_value()}]
@callback request(atom(), String.t(), headers(), String.t(), list()) ::
{:ok, status(), headers(), reference() | map()}
| {:ok, status(), headers()}
2019-07-09 16:54:13 +00:00
| {:ok, reference()}
| {:error, term()}
2020-02-11 07:12:57 +00:00
@callback stream_body(map()) :: {:ok, binary(), map()} | :done | {:error, atom() | String.t()}
2019-07-09 16:54:13 +00:00
@callback close(reference() | pid() | map()) :: :ok
2020-02-11 07:12:57 +00:00
def request(method, url, headers, body \\ "", opts \\ []) do
client().request(method, url, headers, body, opts)
2019-07-09 16:54:13 +00:00
end
def stream_body(ref), do: client().stream_body(ref)
def close(ref), do: client().close(ref)
defp client do
2020-02-11 07:12:57 +00:00
:tesla
|> Application.get_env(:adapter)
|> client()
2019-07-09 16:54:13 +00:00
end
2020-02-11 07:12:57 +00:00
defp client(Tesla.Adapter.Hackney), do: Pleroma.ReverseProxy.Client.Hackney
defp client(Tesla.Adapter.Gun), do: Pleroma.ReverseProxy.Client.Tesla
defp client(_), do: Pleroma.Config.get!(Pleroma.ReverseProxy.Client)
2019-07-09 16:54:13 +00:00
end