1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.ReverseProxy.Client.Tesla do
6 @behaviour Pleroma.ReverseProxy.Client
8 @type headers() :: [{String.t(), String.t()}]
9 @type status() :: pos_integer()
11 @spec request(atom(), String.t(), headers(), String.t(), keyword()) ::
12 {:ok, status(), headers}
13 | {:ok, status(), headers, map()}
14 | {:error, atom() | String.t()}
18 def request(method, url, headers, body, opts \\ []) do
21 opts = Keyword.put(opts, :body_as, :chunks)
23 with {:ok, response} <-
31 if is_map(response.body) and method != :head do
32 {:ok, response.status, response.headers, response.body}
34 conn_pid = response.opts[:adapter][:conn]
35 ConnectionPool.release_conn(conn_pid)
36 {:ok, response.status, response.headers}
39 {:error, error} -> {:error, error}
44 @spec stream_body(map()) ::
45 {:ok, binary(), map()} | {:error, atom() | String.t()} | :done | no_return()
46 def stream_body(%{pid: pid, fin: true}) do
47 ConnectionPool.release_conn(pid)
51 def stream_body(client) do
52 case read_chunk!(client) do
54 {:ok, body, Map.put(client, :fin, true)}
64 defp read_chunk!(%{pid: pid, stream: stream, opts: opts}) do
65 adapter = check_adapter()
66 adapter.read_chunk(pid, stream, opts)
70 @spec close(map) :: :ok | no_return()
71 def close(%{pid: pid}) do
72 ConnectionPool.release_conn(pid)
76 adapter = Application.get_env(:tesla, :adapter)