Use finch everywhere (#33)
[akkoma] / lib / pleroma / http / request_builder.ex
index 491acd0f9fbcc67fb282efdf23ea1efa3ab1f50d..4cd75d3a008a597ea2c5c9b62021aac5cc5ae7da 100644 (file)
@@ -1,5 +1,5 @@
 # Pleroma: A lightweight social networking server
-# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
 # SPDX-License-Identifier: AGPL-3.0-only
 
 defmodule Pleroma.HTTP.RequestBuilder do
@@ -10,6 +10,8 @@ defmodule Pleroma.HTTP.RequestBuilder do
   alias Pleroma.HTTP.Request
   alias Tesla.Multipart
 
+  @mix_env Mix.env()
+
   @doc """
   Creates new request
   """
@@ -33,12 +35,7 @@ defmodule Pleroma.HTTP.RequestBuilder do
   """
   @spec headers(Request.t(), Request.headers()) :: Request.t()
   def headers(request, headers) do
-    headers_list =
-      if Pleroma.Config.get([:http, :send_user_agent]) do
-        headers ++ [{"user-agent", Pleroma.Application.user_agent()}]
-      else
-        headers
-      end
+    headers_list = maybe_add_user_agent(headers, @mix_env)
 
     %{request | headers: headers_list}
   end
@@ -49,26 +46,6 @@ defmodule Pleroma.HTTP.RequestBuilder do
   @spec opts(Request.t(), keyword()) :: Request.t()
   def opts(request, options), do: %{request | opts: options}
 
-  # NOTE: isn't used anywhere
-  @doc """
-  Add optional parameters to the request
-
-  """
-  @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
   """
@@ -110,4 +87,16 @@ defmodule Pleroma.HTTP.RequestBuilder do
     |> 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