akkoma/lib/pleroma/http/request_builder.ex

114 lines
3 KiB
Elixir
Raw Normal View History

# Pleroma: A lightweight social networking server
2018-12-31 08:41:47 -07:00
# Copyright © 2017-2019 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
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 =
if Pleroma.Config.get([:http, :send_user_agent]) do
[{"user-agent", Pleroma.Application.user_agent()} | headers]
else
2020-02-11 00:12:57 -07:00
headers
end
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}
2020-02-11 00:12:57 -07:00
# NOTE: isn't used anywhere
@doc """
Add optional parameters to the request
"""
2020-02-11 00:12:57 -07:00
@spec add_optional_params(Request.t(), %{optional(atom) => atom}, keyword()) :: map()
def add_optional_params(request, _, []), do: request
def add_optional_params(request, definitions, [{key, value} | tail]) do
case definitions do
%{^key => location} ->
request
|> add_param(location, key, value)
|> add_optional_params(definitions, tail)
_ ->
add_optional_params(request, definitions, tail)
end
end
@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
end