aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--config/config.exs1
-rw-r--r--config/test.exs1
-rw-r--r--lib/pleroma/http/connection.ex22
-rw-r--r--lib/pleroma/http/http.ex16
-rw-r--r--lib/pleroma/http/request_builder.ex126
-rw-r--r--lib/pleroma/web/websub/websub.ex14
-rw-r--r--mix.exs1
-rw-r--r--mix.lock3
8 files changed, 175 insertions, 9 deletions
diff --git a/config/config.exs b/config/config.exs
index 12f47389c..8d2fdd40d 100644
--- a/config/config.exs
+++ b/config/config.exs
@@ -72,6 +72,7 @@ config :mime, :types, %{
config :pleroma, :websub, Pleroma.Web.Websub
config :pleroma, :ostatus, Pleroma.Web.OStatus
config :pleroma, :httpoison, Pleroma.HTTP
+config :tesla, adapter: Tesla.Adapter.Hackney
# Configures http settings, upstream proxy etc.
config :pleroma, :http, proxy_url: nil
diff --git a/config/test.exs b/config/test.exs
index 3aaed1b2c..e259a9c65 100644
--- a/config/test.exs
+++ b/config/test.exs
@@ -26,6 +26,7 @@ config :pbkdf2_elixir, rounds: 1
config :pleroma, :websub, Pleroma.Web.WebsubMock
config :pleroma, :ostatus, Pleroma.Web.OStatusMock
config :pleroma, :httpoison, HTTPoisonMock
+config :tesla, adapter: Tesla.Mock
try do
import_config "test.secret.exs"
diff --git a/lib/pleroma/http/connection.ex b/lib/pleroma/http/connection.ex
new file mode 100644
index 000000000..12667b663
--- /dev/null
+++ b/lib/pleroma/http/connection.ex
@@ -0,0 +1,22 @@
+defmodule Pleroma.HTTP.Connection do
+ @hackney_options [pool: :default]
+
+ @doc """
+ Configure a client connection
+
+ # Returns
+
+ Tesla.Env.client
+ """
+ @spec new(Keyword.t()) :: Tesla.Env.client()
+ def new(opts \\ []) do
+ Tesla.client([], {Tesla.Adapter.Hackney, hackney_options(opts)})
+ end
+
+ # fetch Hackney options
+ #
+ defp hackney_options(opts \\ []) do
+ options = Keyword.get(opts, :adapter, [])
+ @hackney_options ++ options
+ end
+end
diff --git a/lib/pleroma/http/http.ex b/lib/pleroma/http/http.ex
index e64266ae7..93ac9d62b 100644
--- a/lib/pleroma/http/http.ex
+++ b/lib/pleroma/http/http.ex
@@ -1,12 +1,21 @@
defmodule Pleroma.HTTP do
require HTTPoison
+ alias Pleroma.HTTP.Connection
+ alias Pleroma.HTTP.RequestBuilder, as: Builder
def request(method, url, body \\ "", headers \\ [], options \\ []) do
options =
process_request_options(options)
|> process_sni_options(url)
- HTTPoison.request(method, url, body, headers, options)
+ %{}
+ |> 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)).()
end
defp process_sni_options(options, url) do
@@ -22,7 +31,7 @@ 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 ++ [hackney: [pool: :default]]
+ options = options ++ [adapter: [pool: :default]]
case proxy do
nil -> options
@@ -30,7 +39,8 @@ defmodule Pleroma.HTTP do
end
end
- def get(url, headers \\ [], options \\ []), do: request(:get, url, "", headers, options)
+ def get(url, headers \\ [], options \\ []),
+ do: request(:get, url, "", headers, options)
def post(url, body, headers \\ [], options \\ []),
do: request(:post, url, body, headers, options)
diff --git a/lib/pleroma/http/request_builder.ex b/lib/pleroma/http/request_builder.ex
new file mode 100644
index 000000000..5aee2b8ae
--- /dev/null
+++ b/lib/pleroma/http/request_builder.ex
@@ -0,0 +1,126 @@
+defmodule Pleroma.HTTP.RequestBuilder do
+ @moduledoc """
+ Helper functions for building Tesla requests
+ """
+
+ @doc """
+ Specify the request method when building a request
+
+ ## Parameters
+
+ - request (Map) - Collected request options
+ - m (atom) - Request method
+
+ ## Returns
+
+ Map
+ """
+ @spec method(map(), atom) :: map()
+ def method(request, m) do
+ Map.put_new(request, :method, m)
+ end
+
+ @doc """
+ Specify the request method when building a request
+
+ ## Parameters
+
+ - request (Map) - Collected request options
+ - u (String) - Request URL
+
+ ## Returns
+
+ Map
+ """
+ @spec url(map(), String.t()) :: map()
+ def url(request, u) do
+ Map.put_new(request, :url, u)
+ end
+
+ @doc """
+ Add headers to the request
+ """
+ @spec headers(map(), list(tuple)) :: map()
+ def headers(request, h) do
+ Map.put_new(request, :headers, h)
+ end
+
+ @doc """
+ Add custom, per-request middleware or adapter options to the request
+ """
+ @spec opts(map(), Keyword.t()) :: map()
+ def opts(request, options) do
+ Map.put_new(request, :opts, options)
+ end
+
+ @doc """
+ Add optional parameters to the request
+
+ ## Parameters
+
+ - request (Map) - Collected request options
+ - definitions (Map) - Map of parameter name to parameter location.
+ - options (KeywordList) - The provided optional parameters
+
+ ## Returns
+
+ Map
+ """
+ @spec add_optional_params(map(), %{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
+
+ ## Parameters
+
+ - request (Map) - Collected request options
+ - location (atom) - Where to put the parameter
+ - key (atom) - The name of the parameter
+ - value (any) - The value of the parameter
+
+ ## Returns
+
+ Map
+ """
+ @spec add_param(map(), atom, atom, any()) :: map()
+ def add_param(request, :body, :body, value), do: Map.put(request, :body, value)
+
+ def add_param(request, :body, key, value) do
+ request
+ |> Map.put_new_lazy(:body, &Tesla.Multipart.new/0)
+ |> Map.update!(
+ :body,
+ &Tesla.Multipart.add_field(&1, key, Poison.encode!(value),
+ headers: [{:"Content-Type", "application/json"}]
+ )
+ )
+ end
+
+ def add_param(request, :file, name, path) do
+ request
+ |> Map.put_new_lazy(:body, &Tesla.Multipart.new/0)
+ |> Map.update!(:body, &Tesla.Multipart.add_file(&1, path, name: name))
+ end
+
+ def add_param(request, :form, name, value) do
+ request
+ |> Map.update(: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
+end
diff --git a/lib/pleroma/web/websub/websub.ex b/lib/pleroma/web/websub/websub.ex
index 905d8d658..ed1a99d8d 100644
--- a/lib/pleroma/web/websub/websub.ex
+++ b/lib/pleroma/web/websub/websub.ex
@@ -173,7 +173,7 @@ defmodule Pleroma.Web.Websub do
def gather_feed_data(topic, getter \\ &@httpoison.get/1) do
with {:ok, response} <- getter.(topic),
- status_code when status_code in 200..299 <- response.status_code,
+ status_code when status_code in 200..299 <- response.status,
body <- response.body,
doc <- XML.parse_document(body),
uri when not is_nil(uri) <- XML.string_from_xpath("/feed/author[1]/uri", doc),
@@ -221,7 +221,7 @@ defmodule Pleroma.Web.Websub do
task = Task.async(websub_checker)
- with {:ok, %{status_code: 202}} <-
+ with {:ok, %{status: 202}} <-
poster.(websub.hub, {:form, data}, "Content-type": "application/x-www-form-urlencoded"),
{:ok, websub} <- Task.yield(task, timeout) do
{:ok, websub}
@@ -257,7 +257,7 @@ defmodule Pleroma.Web.Websub do
signature = sign(secret || "", xml)
Logger.info(fn -> "Pushing #{topic} to #{callback}" end)
- with {:ok, %{status_code: code}} <-
+ with {:ok, %{status: code}} <-
@httpoison.post(
callback,
xml,
@@ -265,9 +265,11 @@ defmodule Pleroma.Web.Websub do
{"Content-Type", "application/atom+xml"},
{"X-Hub-Signature", "sha1=#{signature}"}
],
- timeout: 10000,
- recv_timeout: 20000,
- hackney: [pool: :default]
+ adapter: [
+ timeout: 10000,
+ recv_timeout: 20000,
+ pool: :default
+ ]
) do
Logger.info(fn -> "Pushed to #{callback}, code #{code}" end)
{:ok, code}
diff --git a/mix.exs b/mix.exs
index 9ffcf5928..1a28b6710 100644
--- a/mix.exs
+++ b/mix.exs
@@ -56,6 +56,7 @@ defmodule Pleroma.Mixfile do
{:calendar, "~> 0.17.4"},
{:cachex, "~> 3.0.2"},
{:httpoison, "~> 1.2.0"},
+ {:tesla, "~> 1.2"},
{:jason, "~> 1.0"},
{:mogrify, "~> 0.6.1"},
{:ex_aws, "~> 2.0"},
diff --git a/mix.lock b/mix.lock
index c0fa892a5..4c70061d3 100644
--- a/mix.lock
+++ b/mix.lock
@@ -17,6 +17,7 @@
"eternal": {:hex, :eternal, "1.2.0", "e2a6b6ce3b8c248f7dc31451aefca57e3bdf0e48d73ae5043229380a67614c41", [:mix], [], "hexpm"},
"ex_aws": {:hex, :ex_aws, "2.1.0", "b92651527d6c09c479f9013caa9c7331f19cba38a650590d82ebf2c6c16a1d8a", [:mix], [{:configparser_ex, "~> 2.0", [hex: :configparser_ex, repo: "hexpm", optional: true]}, {:hackney, "1.6.3 or 1.6.5 or 1.7.1 or 1.8.6 or ~> 1.9", [hex: :hackney, repo: "hexpm", optional: true]}, {:jsx, "~> 2.8", [hex: :jsx, repo: "hexpm", optional: true]}, {:poison, ">= 1.2.0", [hex: :poison, repo: "hexpm", optional: true]}, {:sweet_xml, "~> 0.6", [hex: :sweet_xml, repo: "hexpm", optional: true]}, {:xml_builder, "~> 0.1.0", [hex: :xml_builder, repo: "hexpm", optional: true]}], "hexpm"},
"ex_aws_s3": {:hex, :ex_aws_s3, "2.0.1", "9e09366e77f25d3d88c5393824e613344631be8db0d1839faca49686e99b6704", [:mix], [{:ex_aws, "~> 2.0", [hex: :ex_aws, repo: "hexpm", optional: false]}, {:sweet_xml, ">= 0.0.0", [hex: :sweet_xml, repo: "hexpm", optional: true]}], "hexpm"},
+ "ex_doc": {:hex, :ex_doc, "0.19.1", "519bb9c19526ca51d326c060cb1778d4a9056b190086a8c6c115828eaccea6cf", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.7", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm"},
"ex_machina": {:hex, :ex_machina, "2.2.0", "fec496331e04fc2db2a1a24fe317c12c0c4a50d2beb8ebb3531ed1f0d84be0ed", [:mix], [{:ecto, "~> 2.1", [hex: :ecto, repo: "hexpm", optional: true]}], "hexpm"},
"gettext": {:hex, :gettext, "0.15.0", "40a2b8ce33a80ced7727e36768499fc9286881c43ebafccae6bab731e2b2b8ce", [:mix], [], "hexpm"},
"hackney": {:hex, :hackney, "1.13.0", "24edc8cd2b28e1c652593833862435c80661834f6c9344e84b6a2255e7aeef03", [:rebar3], [{:certifi, "2.3.1", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "5.1.2", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"},
@@ -25,6 +26,7 @@
"idna": {:hex, :idna, "5.1.2", "e21cb58a09f0228a9e0b95eaa1217f1bcfc31a1aaa6e1fdf2f53a33f7dbd9494", [:rebar3], [{:unicode_util_compat, "0.3.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm"},
"jason": {:hex, :jason, "1.0.0", "0f7cfa9bdb23fed721ec05419bcee2b2c21a77e926bce0deda029b5adc716fe2", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"},
"makeup": {:hex, :makeup, "0.5.5", "9e08dfc45280c5684d771ad58159f718a7b5788596099bdfb0284597d368a882", [:mix], [{:nimble_parsec, "~> 0.4", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"},
+ "makeup_elixir": {:hex, :makeup_elixir, "0.10.0", "0f09c2ddf352887a956d84f8f7e702111122ca32fbbc84c2f0569b8b65cbf7fa", [:mix], [{:makeup, "~> 0.5.5", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm"},
"meck": {:hex, :meck, "0.8.9", "64c5c0bd8bcca3a180b44196265c8ed7594e16bcc845d0698ec6b4e577f48188", [:rebar3], [], "hexpm"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm"},
"mime": {:hex, :mime, "1.3.0", "5e8d45a39e95c650900d03f897fbf99ae04f60ab1daa4a34c7a20a5151b7a5fe", [:mix], [], "hexpm"},
@@ -45,6 +47,7 @@
"postgrex": {:hex, :postgrex, "0.13.5", "3d931aba29363e1443da167a4b12f06dcd171103c424de15e5f3fc2ba3e6d9c5", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 1.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm"},
"ranch": {:hex, :ranch, "1.3.2", "e4965a144dc9fbe70e5c077c65e73c57165416a901bd02ea899cfd95aa890986", [:rebar3], [], "hexpm"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], [], "hexpm"},
+ "tesla": {:hex, :tesla, "1.2.1", "864783cc27f71dd8c8969163704752476cec0f3a51eb3b06393b3971dc9733ff", [:mix], [{:exjsx, ">= 3.0.0", [hex: :exjsx, repo: "hexpm", optional: true]}, {:fuse, "~> 2.4", [hex: :fuse, repo: "hexpm", optional: true]}, {:hackney, "~> 1.6", [hex: :hackney, repo: "hexpm", optional: true]}, {:ibrowse, "~> 4.4.0", [hex: :ibrowse, repo: "hexpm", optional: true]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: true]}, {:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:poison, ">= 1.0.0", [hex: :poison, repo: "hexpm", optional: true]}], "hexpm"},
"trailing_format_plug": {:hex, :trailing_format_plug, "0.0.7", "64b877f912cf7273bed03379936df39894149e35137ac9509117e59866e10e45", [:mix], [{:plug, "> 0.12.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},
"tzdata": {:hex, :tzdata, "0.5.17", "50793e3d85af49736701da1a040c415c97dc1caf6464112fd9bd18f425d3053b", [:mix], [{:hackney, "~> 1.0", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.3.1", "a1f612a7b512638634a603c8f401892afbf99b8ce93a45041f8aaca99cadb85e", [:rebar3], [], "hexpm"},