aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorkaniini <nenolod@gmail.com>2018-12-04 18:41:00 +0000
committerkaniini <nenolod@gmail.com>2018-12-04 18:41:00 +0000
commit114b95cee20c9bb4922627e5397a70d60e905fa6 (patch)
treea188264fd550f9473ee3f1faa74d56ca9fd319fc /lib
parent4a602230e4b95e916b3df73bd88413b245e07de0 (diff)
parenta9e3e387c9798a810f82bbf92023b4d29abffbe5 (diff)
downloadpleroma-114b95cee20c9bb4922627e5397a70d60e905fa6.tar.gz
Merge branch 'feature/integration_tesla' into 'develop'
[#354] Move all http interactions to Tesla See merge request pleroma/pleroma!487
Diffstat (limited to 'lib')
-rw-r--r--lib/pleroma/http/connection.ex27
-rw-r--r--lib/pleroma/http/http.ex47
-rw-r--r--lib/pleroma/http/request_builder.ex126
-rw-r--r--lib/pleroma/uploaders/mdii.ex2
-rw-r--r--lib/pleroma/uploaders/swift/keystone.ex4
-rw-r--r--lib/pleroma/uploaders/swift/swift.ex4
-rw-r--r--lib/pleroma/web/activity_pub/activity_pub.ex2
-rw-r--r--lib/pleroma/web/mastodon_api/mastodon_api_controller.ex2
-rw-r--r--lib/pleroma/web/ostatus/ostatus.ex8
-rw-r--r--lib/pleroma/web/salmon/salmon.ex10
-rw-r--r--lib/pleroma/web/web_finger/web_finger.ex4
-rw-r--r--lib/pleroma/web/websub/websub.ex14
12 files changed, 224 insertions, 26 deletions
diff --git a/lib/pleroma/http/connection.ex b/lib/pleroma/http/connection.ex
new file mode 100644
index 000000000..5e8f2aabd
--- /dev/null
+++ b/lib/pleroma/http/connection.ex
@@ -0,0 +1,27 @@
+defmodule Pleroma.HTTP.Connection do
+ @moduledoc """
+ Connection for http-requests.
+ """
+
+ @hackney_options [pool: :default]
+ @adapter Application.get_env(:tesla, :adapter)
+
+ @doc """
+ Configure a client connection
+
+ # Returns
+
+ Tesla.Env.client
+ """
+ @spec new(Keyword.t()) :: Tesla.Env.client()
+ def new(opts \\ []) do
+ Tesla.client([], {@adapter, 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..3c0256575 100644
--- a/lib/pleroma/http/http.ex
+++ b/lib/pleroma/http/http.ex
@@ -1,14 +1,42 @@
defmodule Pleroma.HTTP do
- require HTTPoison
+ @moduledoc """
+ """
+
+ alias Pleroma.HTTP.Connection
+ alias Pleroma.HTTP.RequestBuilder, as: Builder
+
+ @doc """
+ Builds and perform http request.
+
+ # Arguments:
+ `method` - :get, :post, :put, :delete
+ `url`
+ `body`
+ `headers` - a keyworld list of headers, e.g. `[{"content-type", "text/plain"}]`
+ `options` - custom, per-request middleware or adapter options
+
+ # Returns:
+ `{:ok, %Tesla.Env{}}` or `{:error, error}`
+
+ """
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, nil), do: options
+
defp process_sni_options(options, url) do
uri = URI.parse(url)
host = uri.host |> to_charlist()
@@ -22,7 +50,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,8 +58,19 @@ defmodule Pleroma.HTTP do
end
end
- def get(url, headers \\ [], options \\ []), do: request(:get, url, "", headers, options)
+ @doc """
+ Performs GET request.
+
+ See `Pleroma.HTTP.request/5`
+ """
+ def get(url, headers \\ [], options \\ []),
+ do: request(:get, url, "", headers, options)
+
+ @doc """
+ Performs POST request.
+ See `Pleroma.HTTP.request/5`
+ """
def post(url, body, headers \\ [], options \\ []),
do: request(:post, url, body, headers, options)
end
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/uploaders/mdii.ex b/lib/pleroma/uploaders/mdii.ex
index 35d36d3e4..820cf88f5 100644
--- a/lib/pleroma/uploaders/mdii.ex
+++ b/lib/pleroma/uploaders/mdii.ex
@@ -20,7 +20,7 @@ defmodule Pleroma.Uploaders.MDII do
extension = String.split(upload.name, ".") |> List.last()
query = "#{cgi}?#{extension}"
- with {:ok, %{status_code: 200, body: body}} <- @httpoison.post(query, file_data) do
+ with {:ok, %{status: 200, body: body}} <- @httpoison.post(query, file_data) do
remote_file_name = String.split(body) |> List.first()
public_url = "#{files}/#{remote_file_name}.#{extension}"
{:ok, {:url, public_url}}
diff --git a/lib/pleroma/uploaders/swift/keystone.ex b/lib/pleroma/uploaders/swift/keystone.ex
index e578b3c61..4aed977b1 100644
--- a/lib/pleroma/uploaders/swift/keystone.ex
+++ b/lib/pleroma/uploaders/swift/keystone.ex
@@ -25,10 +25,10 @@ defmodule Pleroma.Uploaders.Swift.Keystone do
["Content-Type": "application/json"],
hackney: [:insecure]
) do
- {:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
+ {:ok, %Tesla.Env{status: 200, body: body}} ->
body["access"]["token"]["id"]
- {:ok, %HTTPoison.Response{status_code: _}} ->
+ {:ok, %Tesla.Env{status: _}} ->
""
end
end
diff --git a/lib/pleroma/uploaders/swift/swift.ex b/lib/pleroma/uploaders/swift/swift.ex
index 1e865f101..a5b3d2852 100644
--- a/lib/pleroma/uploaders/swift/swift.ex
+++ b/lib/pleroma/uploaders/swift/swift.ex
@@ -13,10 +13,10 @@ defmodule Pleroma.Uploaders.Swift.Client do
token = Pleroma.Uploaders.Swift.Keystone.get_token()
case put("#{filename}", body, "X-Auth-Token": token, "Content-Type": content_type) do
- {:ok, %HTTPoison.Response{status_code: 201}} ->
+ {:ok, %Tesla.Env{status: 201}} ->
{:ok, {:file, filename}}
- {:ok, %HTTPoison.Response{status_code: 401}} ->
+ {:ok, %Tesla.Env{status: 401}} ->
{:error, "Unauthorized, Bad Token"}
{:error, _} ->
diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex
index 7e207c620..60253a715 100644
--- a/lib/pleroma/web/activity_pub/activity_pub.ex
+++ b/lib/pleroma/web/activity_pub/activity_pub.ex
@@ -762,7 +762,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
Logger.info("Fetching #{id} via AP")
with true <- String.starts_with?(id, "http"),
- {:ok, %{body: body, status_code: code}} when code in 200..299 <-
+ {:ok, %{body: body, status: code}} when code in 200..299 <-
@httpoison.get(
id,
[Accept: "application/activity+json"],
diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
index 543fdf416..ea64f163d 100644
--- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
+++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
@@ -1168,7 +1168,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
user = user.nickname
url = String.replace(api, "{{host}}", host) |> String.replace("{{user}}", user)
- with {:ok, %{status_code: 200, body: body}} <-
+ with {:ok, %{status: 200, body: body}} <-
@httpoison.get(url, [], timeout: timeout, recv_timeout: timeout),
{:ok, data} <- Jason.decode(body) do
data2 =
diff --git a/lib/pleroma/web/ostatus/ostatus.ex b/lib/pleroma/web/ostatus/ostatus.ex
index 6a27f1730..67df354db 100644
--- a/lib/pleroma/web/ostatus/ostatus.ex
+++ b/lib/pleroma/web/ostatus/ostatus.ex
@@ -346,13 +346,15 @@ defmodule Pleroma.Web.OStatus do
def fetch_activity_from_atom_url(url) do
with true <- String.starts_with?(url, "http"),
- {:ok, %{body: body, status_code: code}} when code in 200..299 <-
+ {:ok, %{body: body, status: code}} when code in 200..299 <-
@httpoison.get(
url,
[Accept: "application/atom+xml"],
follow_redirect: true,
- timeout: 10000,
- recv_timeout: 20000
+ adapter: [
+ timeout: 10000,
+ recv_timeout: 20000
+ ]
) do
Logger.debug("Got document from #{url}, handling...")
handle_incoming(body)
diff --git a/lib/pleroma/web/salmon/salmon.ex b/lib/pleroma/web/salmon/salmon.ex
index b98ece6c9..97251c05e 100644
--- a/lib/pleroma/web/salmon/salmon.ex
+++ b/lib/pleroma/web/salmon/salmon.ex
@@ -158,14 +158,16 @@ defmodule Pleroma.Web.Salmon do
end
defp send_to_user(%{info: %{salmon: salmon}}, feed, poster) do
- with {:ok, %{status_code: code}} <-
+ with {:ok, %{status: code}} <-
poster.(
salmon,
feed,
[{"Content-Type", "application/magic-envelope+xml"}],
- timeout: 10000,
- recv_timeout: 20000,
- hackney: [pool: :default]
+ adapter: [
+ timeout: 10000,
+ recv_timeout: 20000,
+ pool: :default
+ ]
) do
Logger.debug(fn -> "Pushed to #{salmon}, code #{code}" end)
else
diff --git a/lib/pleroma/web/web_finger/web_finger.ex b/lib/pleroma/web/web_finger/web_finger.ex
index eaee3a8c6..99c65a6bf 100644
--- a/lib/pleroma/web/web_finger/web_finger.ex
+++ b/lib/pleroma/web/web_finger/web_finger.ex
@@ -220,7 +220,7 @@ defmodule Pleroma.Web.WebFinger do
end
def find_lrdd_template(domain) do
- with {:ok, %{status_code: status_code, body: body}} when status_code in 200..299 <-
+ with {:ok, %{status: status, body: body}} when status in 200..299 <-
@httpoison.get("http://#{domain}/.well-known/host-meta", [], follow_redirect: true) do
get_template_from_xml(body)
else
@@ -259,7 +259,7 @@ defmodule Pleroma.Web.WebFinger do
[Accept: "application/xrd+xml,application/jrd+json"],
follow_redirect: true
),
- {:ok, %{status_code: status_code, body: body}} when status_code in 200..299 <- response do
+ {:ok, %{status: status, body: body}} when status in 200..299 <- response do
doc = XML.parse_document(body)
if doc != :error do
diff --git a/lib/pleroma/web/websub/websub.ex b/lib/pleroma/web/websub/websub.ex
index 905d8d658..0761b5475 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 when status 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}