http: rework connection timeouts to match hackney docs, enforce 1 second max TCP...
[akkoma] / lib / pleroma / http / http.ex
index f12f33566e6dd2104c6513426330340dde70c28a..c5f720bc9f756292f0c99d2c44500911ca19062f 100644 (file)
@@ -27,18 +27,29 @@ defmodule Pleroma.HTTP do
 
   """
   def request(method, url, body \\ "", headers \\ [], options \\ []) do
-    options =
-      process_request_options(options)
-      |> process_sni_options(url)
-
-    %{}
-    |> Builder.method(method)
-    |> Builder.headers(headers)
-    |> Builder.opts(options)
-    |> Builder.url(url)
-    |> Builder.add_param(:body, :body, body)
-    |> Enum.into([])
-    |> (&Tesla.request(Connection.new(), &1)).()
+    try do
+      options =
+        process_request_options(options)
+        |> process_sni_options(url)
+
+      params = Keyword.get(options, :params, [])
+
+      %{}
+      |> Builder.method(method)
+      |> Builder.headers(headers)
+      |> Builder.opts(options)
+      |> Builder.url(url)
+      |> Builder.add_param(:body, :body, body)
+      |> Builder.add_param(:query, :query, params)
+      |> Enum.into([])
+      |> (&Tesla.request(Connection.new(options), &1)).()
+    rescue
+      e ->
+        {:error, e}
+    catch
+      :exit, e ->
+        {:error, e}
+    end
   end
 
   defp process_sni_options(options, nil), do: options
@@ -56,7 +67,6 @@ defmodule Pleroma.HTTP do
   def process_request_options(options) do
     config = Application.get_env(:pleroma, :http, [])
     proxy = Keyword.get(config, :proxy_url, nil)
-    options = options ++ [adapter: [pool: :default]]
 
     case proxy do
       nil -> options