2018-12-23 13:04:54 -07:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-01 04:11:47 -07:00
|
|
|
# Copyright © 2017-2023 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 13:04:54 -07:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-11-30 22:26:59 -07:00
|
|
|
defmodule Pleroma.HTTP.RequestBuilder do
|
|
|
|
@moduledoc """
|
|
|
|
Helper functions for building Tesla requests
|
|
|
|
"""
|
|
|
|
|
2020-02-11 00:12:57 -07:00
|
|
|
alias Pleroma.HTTP.Request
|
|
|
|
alias Tesla.Multipart
|
2018-11-30 22:26:59 -07:00
|
|
|
|
2020-02-11 00:12:57 -07:00
|
|
|
@doc """
|
|
|
|
Creates new request
|
2018-11-30 22:26:59 -07:00
|
|
|
"""
|
2020-02-11 00:12:57 -07:00
|
|
|
@spec new(Request.t()) :: Request.t()
|
|
|
|
def new(%Request{} = request \\ %Request{}), do: request
|
2018-11-30 22:26:59 -07:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Specify the request method when building a request
|
2020-02-11 00:12:57 -07:00
|
|
|
"""
|
|
|
|
@spec method(Request.t(), Request.method()) :: Request.t()
|
|
|
|
def method(request, m), do: %{request | method: m}
|
2018-11-30 22:26:59 -07:00
|
|
|
|
2020-02-11 00:12:57 -07:00
|
|
|
@doc """
|
|
|
|
Specify the request method when building a request
|
2018-11-30 22:26:59 -07:00
|
|
|
"""
|
2020-02-11 00:12:57 -07:00
|
|
|
@spec url(Request.t(), Request.url()) :: Request.t()
|
|
|
|
def url(request, u), do: %{request | url: u}
|
2018-11-30 22:26:59 -07:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Add headers to the request
|
|
|
|
"""
|
2020-02-11 00:12:57 -07:00
|
|
|
@spec headers(Request.t(), Request.headers()) :: Request.t()
|
|
|
|
def headers(request, headers) do
|
|
|
|
headers_list =
|
2020-08-01 01:04:25 -06:00
|
|
|
with true <- Pleroma.Config.get([:http, :send_user_agent]),
|
|
|
|
nil <- Enum.find(headers, fn {key, _val} -> String.downcase(key) == "user-agent" end) do
|
2020-03-03 02:46:53 -07:00
|
|
|
[{"user-agent", Pleroma.Application.user_agent()} | headers]
|
2019-05-17 14:28:58 -06:00
|
|
|
else
|
2020-08-01 01:04:25 -06:00
|
|
|
_ ->
|
|
|
|
headers
|
2019-05-17 14:28:58 -06:00
|
|
|
end
|
|
|
|
|
2020-02-11 00:12:57 -07:00
|
|
|
%{request | headers: headers_list}
|
2018-11-30 22:26:59 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Add custom, per-request middleware or adapter options to the request
|
|
|
|
"""
|
2020-02-11 00:12:57 -07:00
|
|
|
@spec opts(Request.t(), keyword()) :: Request.t()
|
|
|
|
def opts(request, options), do: %{request | opts: options}
|
2018-11-30 22:26:59 -07:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Add optional parameters to the request
|
|
|
|
"""
|
2020-02-11 00:12:57 -07:00
|
|
|
@spec add_param(Request.t(), atom(), atom(), any()) :: Request.t()
|
|
|
|
def add_param(request, :query, :query, values), do: %{request | query: values}
|
2019-01-15 12:31:13 -07:00
|
|
|
|
2020-02-11 00:12:57 -07:00
|
|
|
def add_param(request, :body, :body, value), do: %{request | body: value}
|
2018-11-30 22:26:59 -07:00
|
|
|
|
|
|
|
def add_param(request, :body, key, value) do
|
|
|
|
request
|
2020-02-11 00:12:57 -07:00
|
|
|
|> Map.put(:body, Multipart.new())
|
2018-11-30 22:26:59 -07:00
|
|
|
|> Map.update!(
|
|
|
|
:body,
|
2020-02-11 00:12:57 -07:00
|
|
|
&Multipart.add_field(
|
2019-01-15 12:31:13 -07:00
|
|
|
&1,
|
|
|
|
key,
|
|
|
|
Jason.encode!(value),
|
2020-02-11 00:12:57 -07:00
|
|
|
headers: [{"content-type", "application/json"}]
|
2018-11-30 22:26:59 -07:00
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_param(request, :file, name, path) do
|
|
|
|
request
|
2020-02-11 00:12:57 -07:00
|
|
|
|> Map.put(:body, Multipart.new())
|
|
|
|
|> Map.update!(:body, &Multipart.add_file(&1, path, name: name))
|
2018-11-30 22:26:59 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_param(request, :form, name, value) do
|
2020-02-11 00:12:57 -07:00
|
|
|
Map.update(request, :body, %{name => value}, &Map.put(&1, name, value))
|
2018-11-30 22:26:59 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_param(request, location, key, value) do
|
|
|
|
Map.update(request, location, [{key, value}], &(&1 ++ [{key, value}]))
|
|
|
|
end
|
2020-02-11 00:12:57 -07:00
|
|
|
|
|
|
|
def convert_to_keyword(request) do
|
|
|
|
request
|
|
|
|
|> Map.from_struct()
|
|
|
|
|> Enum.into([])
|
|
|
|
end
|
2018-11-30 22:26:59 -07:00
|
|
|
end
|