Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

143 рядки
3.4KB

  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. defmodule Pleroma.HTTP.RequestBuilder do
  5. @moduledoc """
  6. Helper functions for building Tesla requests
  7. """
  8. @doc """
  9. Specify the request method when building a request
  10. ## Parameters
  11. - request (Map) - Collected request options
  12. - m (atom) - Request method
  13. ## Returns
  14. Map
  15. """
  16. @spec method(map(), atom) :: map()
  17. def method(request, m) do
  18. Map.put_new(request, :method, m)
  19. end
  20. @doc """
  21. Specify the request method when building a request
  22. ## Parameters
  23. - request (Map) - Collected request options
  24. - u (String) - Request URL
  25. ## Returns
  26. Map
  27. """
  28. @spec url(map(), String.t()) :: map()
  29. def url(request, u) do
  30. Map.put_new(request, :url, u)
  31. end
  32. @doc """
  33. Add headers to the request
  34. """
  35. @spec headers(map(), list(tuple)) :: map()
  36. def headers(request, header_list) do
  37. header_list =
  38. if Pleroma.Config.get([:http, :send_user_agent]) do
  39. header_list ++ [{"User-Agent", Pleroma.Application.user_agent()}]
  40. else
  41. header_list
  42. end
  43. Map.put_new(request, :headers, header_list)
  44. end
  45. @doc """
  46. Add custom, per-request middleware or adapter options to the request
  47. """
  48. @spec opts(map(), Keyword.t()) :: map()
  49. def opts(request, options) do
  50. Map.put_new(request, :opts, options)
  51. end
  52. @doc """
  53. Add optional parameters to the request
  54. ## Parameters
  55. - request (Map) - Collected request options
  56. - definitions (Map) - Map of parameter name to parameter location.
  57. - options (KeywordList) - The provided optional parameters
  58. ## Returns
  59. Map
  60. """
  61. @spec add_optional_params(map(), %{optional(atom) => atom}, keyword()) :: map()
  62. def add_optional_params(request, _, []), do: request
  63. def add_optional_params(request, definitions, [{key, value} | tail]) do
  64. case definitions do
  65. %{^key => location} ->
  66. request
  67. |> add_param(location, key, value)
  68. |> add_optional_params(definitions, tail)
  69. _ ->
  70. add_optional_params(request, definitions, tail)
  71. end
  72. end
  73. @doc """
  74. Add optional parameters to the request
  75. ## Parameters
  76. - request (Map) - Collected request options
  77. - location (atom) - Where to put the parameter
  78. - key (atom) - The name of the parameter
  79. - value (any) - The value of the parameter
  80. ## Returns
  81. Map
  82. """
  83. @spec add_param(map(), atom, atom, any()) :: map()
  84. def add_param(request, :query, :query, values), do: Map.put(request, :query, values)
  85. def add_param(request, :body, :body, value), do: Map.put(request, :body, value)
  86. def add_param(request, :body, key, value) do
  87. request
  88. |> Map.put_new_lazy(:body, &Tesla.Multipart.new/0)
  89. |> Map.update!(
  90. :body,
  91. &Tesla.Multipart.add_field(
  92. &1,
  93. key,
  94. Jason.encode!(value),
  95. headers: [{:"Content-Type", "application/json"}]
  96. )
  97. )
  98. end
  99. def add_param(request, :file, name, path) do
  100. request
  101. |> Map.put_new_lazy(:body, &Tesla.Multipart.new/0)
  102. |> Map.update!(:body, &Tesla.Multipart.add_file(&1, path, name: name))
  103. end
  104. def add_param(request, :form, name, value) do
  105. request
  106. |> Map.update(:body, %{name => value}, &Map.put(&1, name, value))
  107. end
  108. def add_param(request, location, key, value) do
  109. Map.update(request, location, [{key, value}], &(&1 ++ [{key, value}]))
  110. end
  111. end