akkoma/lib/pleroma/http/request_builder.ex

103 lines
2.7 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
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
@mix_env Mix.env()
2020-02-11 00:12:57 -07:00
@doc """
Creates new request
"""
2020-02-11 00:12:57 -07:00
@spec new(Request.t()) :: Request.t()
def new(%Request{} = request \\ %Request{}), do: request
@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}
2020-02-11 00:12:57 -07:00
@doc """
Specify the request method when building a request
"""
2020-02-11 00:12:57 -07:00
@spec url(Request.t(), Request.url()) :: Request.t()
def url(request, u), do: %{request | url: u}
@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 = maybe_add_user_agent(headers, @mix_env)
2020-02-11 00:12:57 -07:00
%{request | headers: headers_list}
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}
@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}
2020-02-11 00:12:57 -07:00
def add_param(request, :body, :body, value), do: %{request | body: value}
def add_param(request, :body, key, value) do
request
2020-02-11 00:12:57 -07:00
|> Map.put(:body, Multipart.new())
|> Map.update!(
:body,
2020-02-11 00:12:57 -07:00
&Multipart.add_field(
&1,
key,
Jason.encode!(value),
2020-02-11 00:12:57 -07:00
headers: [{"content-type", "application/json"}]
)
)
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))
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))
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
defp maybe_add_user_agent(headers, :test) do
with true <- Pleroma.Config.get([:http, :send_user_agent]) do
[{"user-agent", Pleroma.Application.user_agent()} | headers]
else
_ ->
headers
end
end
defp maybe_add_user_agent(headers, _),
do: [{"user-agent", Pleroma.Application.user_agent()} | headers]
end