Merge branch 'exclude-reblogs-from-admin-api-by-default' into 'develop'
[akkoma] / lib / pleroma / http / request_builder.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.HTTP.RequestBuilder do
6 @moduledoc """
7 Helper functions for building Tesla requests
8 """
9
10 @doc """
11 Specify the request method when building a request
12
13 ## Parameters
14
15 - request (Map) - Collected request options
16 - m (atom) - Request method
17
18 ## Returns
19
20 Map
21 """
22 @spec method(map(), atom) :: map()
23 def method(request, m) do
24 Map.put_new(request, :method, m)
25 end
26
27 @doc """
28 Specify the request method when building a request
29
30 ## Parameters
31
32 - request (Map) - Collected request options
33 - u (String) - Request URL
34
35 ## Returns
36
37 Map
38 """
39 @spec url(map(), String.t()) :: map()
40 def url(request, u) do
41 Map.put_new(request, :url, u)
42 end
43
44 @doc """
45 Add headers to the request
46 """
47 @spec headers(map(), list(tuple)) :: map()
48 def headers(request, header_list) do
49 header_list =
50 if Pleroma.Config.get([:http, :send_user_agent]) do
51 header_list ++ [{"User-Agent", Pleroma.Application.user_agent()}]
52 else
53 header_list
54 end
55
56 Map.put_new(request, :headers, header_list)
57 end
58
59 @doc """
60 Add custom, per-request middleware or adapter options to the request
61 """
62 @spec opts(map(), Keyword.t()) :: map()
63 def opts(request, options) do
64 Map.put_new(request, :opts, options)
65 end
66
67 @doc """
68 Add optional parameters to the request
69
70 ## Parameters
71
72 - request (Map) - Collected request options
73 - definitions (Map) - Map of parameter name to parameter location.
74 - options (KeywordList) - The provided optional parameters
75
76 ## Returns
77
78 Map
79 """
80 @spec add_optional_params(map(), %{optional(atom) => atom}, keyword()) :: map()
81 def add_optional_params(request, _, []), do: request
82
83 def add_optional_params(request, definitions, [{key, value} | tail]) do
84 case definitions do
85 %{^key => location} ->
86 request
87 |> add_param(location, key, value)
88 |> add_optional_params(definitions, tail)
89
90 _ ->
91 add_optional_params(request, definitions, tail)
92 end
93 end
94
95 @doc """
96 Add optional parameters to the request
97
98 ## Parameters
99
100 - request (Map) - Collected request options
101 - location (atom) - Where to put the parameter
102 - key (atom) - The name of the parameter
103 - value (any) - The value of the parameter
104
105 ## Returns
106
107 Map
108 """
109 @spec add_param(map(), atom, atom, any()) :: map()
110 def add_param(request, :query, :query, values), do: Map.put(request, :query, values)
111
112 def add_param(request, :body, :body, value), do: Map.put(request, :body, value)
113
114 def add_param(request, :body, key, value) do
115 request
116 |> Map.put_new_lazy(:body, &Tesla.Multipart.new/0)
117 |> Map.update!(
118 :body,
119 &Tesla.Multipart.add_field(
120 &1,
121 key,
122 Jason.encode!(value),
123 headers: [{:"Content-Type", "application/json"}]
124 )
125 )
126 end
127
128 def add_param(request, :file, name, path) do
129 request
130 |> Map.put_new_lazy(:body, &Tesla.Multipart.new/0)
131 |> Map.update!(:body, &Tesla.Multipart.add_file(&1, path, name: name))
132 end
133
134 def add_param(request, :form, name, value) do
135 request
136 |> Map.update(:body, %{name => value}, &Map.put(&1, name, value))
137 end
138
139 def add_param(request, location, key, value) do
140 Map.update(request, location, [{key, value}], &(&1 ++ [{key, value}]))
141 end
142 end