logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

request_builder.ex (2673B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.HTTP.RequestBuilder do
  5. @moduledoc """
  6. Helper functions for building Tesla requests
  7. """
  8. alias Pleroma.HTTP.Request
  9. alias Tesla.Multipart
  10. @doc """
  11. Creates new request
  12. """
  13. @spec new(Request.t()) :: Request.t()
  14. def new(%Request{} = request \\ %Request{}), do: request
  15. @doc """
  16. Specify the request method when building a request
  17. """
  18. @spec method(Request.t(), Request.method()) :: Request.t()
  19. def method(request, m), do: %{request | method: m}
  20. @doc """
  21. Specify the request method when building a request
  22. """
  23. @spec url(Request.t(), Request.url()) :: Request.t()
  24. def url(request, u), do: %{request | url: u}
  25. @doc """
  26. Add headers to the request
  27. """
  28. @spec headers(Request.t(), Request.headers()) :: Request.t()
  29. def headers(request, headers) do
  30. headers_list =
  31. with true <- Pleroma.Config.get([:http, :send_user_agent]),
  32. nil <- Enum.find(headers, fn {key, _val} -> String.downcase(key) == "user-agent" end) do
  33. [{"user-agent", Pleroma.Application.user_agent()} | headers]
  34. else
  35. _ ->
  36. headers
  37. end
  38. %{request | headers: headers_list}
  39. end
  40. @doc """
  41. Add custom, per-request middleware or adapter options to the request
  42. """
  43. @spec opts(Request.t(), keyword()) :: Request.t()
  44. def opts(request, options), do: %{request | opts: options}
  45. @doc """
  46. Add optional parameters to the request
  47. """
  48. @spec add_param(Request.t(), atom(), atom() | String.t(), any()) :: Request.t()
  49. def add_param(request, :query, :query, values), do: %{request | query: values}
  50. def add_param(request, :body, :body, value), do: %{request | body: value}
  51. def add_param(request, :body, key, value) when is_binary(key) do
  52. request
  53. |> Map.put(:body, Multipart.new())
  54. |> Map.update!(
  55. :body,
  56. &Multipart.add_field(
  57. &1,
  58. key,
  59. Jason.encode!(value),
  60. headers: [{"content-type", "application/json"}]
  61. )
  62. )
  63. end
  64. def add_param(request, :file, name, path) do
  65. request
  66. |> Map.put(:body, Multipart.new())
  67. |> Map.update!(:body, &Multipart.add_file(&1, path, name: name))
  68. end
  69. def add_param(request, :form, name, value) do
  70. Map.update(request, :body, %{name => value}, &Map.put(&1, name, value))
  71. end
  72. def add_param(request, location, key, value) do
  73. Map.update(request, location, [{key, value}], &(&1 ++ [{key, value}]))
  74. end
  75. def convert_to_keyword(request) do
  76. request
  77. |> Map.from_struct()
  78. |> Enum.into([])
  79. end
  80. end