summaryrefslogtreecommitdiff
path: root/lib/pleroma/http/request_builder.ex
blob: 631c927af2b35a9e9d34bba6555658099b4a998f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.HTTP.RequestBuilder do
  @moduledoc """
  Helper functions for building Tesla requests
  """

  alias Pleroma.HTTP.Request
  alias Tesla.Multipart

  @doc """
  Creates new request
  """
  @spec new(Request.t()) :: Request.t()
  def new(%Request{} = request \\ %Request{}), do: request

  @doc """
  Specify the request method when building a request
  """
  @spec method(Request.t(), Request.method()) :: Request.t()
  def method(request, m), do: %{request | method: m}

  @doc """
  Specify the request method when building a request
  """
  @spec url(Request.t(), Request.url()) :: Request.t()
  def url(request, u), do: %{request | url: u}

  @doc """
  Add headers to the request
  """
  @spec headers(Request.t(), Request.headers()) :: Request.t()
  def headers(request, headers) do
    headers_list =
      with true <- Pleroma.Config.get([:http, :send_user_agent]),
           nil <- Enum.find(headers, fn {key, _val} -> String.downcase(key) == "user-agent" end) do
        [{"user-agent", Pleroma.Application.user_agent()} | headers]
      else
        _ ->
          headers
      end

    %{request | headers: headers_list}
  end

  @doc """
  Add custom, per-request middleware or adapter options to the request
  """
  @spec opts(Request.t(), keyword()) :: Request.t()
  def opts(request, options), do: %{request | opts: options}

  @doc """
  Add optional parameters to the request
  """
  @spec add_param(Request.t(), atom(), atom(), any()) :: Request.t()
  def add_param(request, :query, :query, values), do: %{request | query: values}

  def add_param(request, :body, :body, value), do: %{request | body: value}

  def add_param(request, :body, key, value) do
    request
    |> Map.put(:body, Multipart.new())
    |> Map.update!(
      :body,
      &Multipart.add_field(
        &1,
        key,
        Jason.encode!(value),
        headers: [{"content-type", "application/json"}]
      )
    )
  end

  def add_param(request, :file, name, path) do
    request
    |> Map.put(:body, Multipart.new())
    |> Map.update!(:body, &Multipart.add_file(&1, path, name: name))
  end

  def add_param(request, :form, name, value) do
    Map.update(request, :body, %{name => value}, &Map.put(&1, name, value))
  end

  def add_param(request, location, key, value) do
    Map.update(request, location, [{key, value}], &(&1 ++ [{key, value}]))
  end

  def convert_to_keyword(request) do
    request
    |> Map.from_struct()
    |> Enum.into([])
  end
end