You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
3.2 KiB
111 lines
3.2 KiB
![]()
6 years ago
|
# Pleroma: A lightweight social networking server
|
||
![]()
5 years ago
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
||
![]()
6 years ago
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||
|
|
||
![]()
7 years ago
|
defmodule Pleroma.HTTP do
|
||
![]()
6 years ago
|
@moduledoc """
|
||
![]()
5 years ago
|
Wrapper for `Tesla.request/2`.
|
||
![]()
6 years ago
|
"""
|
||
|
|
||
![]()
4 years ago
|
alias Pleroma.HTTP.AdapterHelper
|
||
![]()
5 years ago
|
alias Pleroma.HTTP.Request
|
||
![]()
6 years ago
|
alias Pleroma.HTTP.RequestBuilder, as: Builder
|
||
![]()
5 years ago
|
alias Tesla.Client
|
||
|
alias Tesla.Env
|
||
|
|
||
|
require Logger
|
||
![]()
6 years ago
|
|
||
![]()
6 years ago
|
@type t :: __MODULE__
|
||
![]()
4 years ago
|
@type method() :: :get | :post | :put | :delete | :head
|
||
![]()
6 years ago
|
|
||
![]()
6 years ago
|
@doc """
|
||
![]()
5 years ago
|
Performs GET request.
|
||
|
|
||
|
See `Pleroma.HTTP.request/5`
|
||
|
"""
|
||
|
@spec get(Request.url() | nil, Request.headers(), keyword()) ::
|
||
|
nil | {:ok, Env.t()} | {:error, any()}
|
||
|
def get(url, headers \\ [], options \\ [])
|
||
|
def get(nil, _, _), do: nil
|
||
|
def get(url, headers, options), do: request(:get, url, "", headers, options)
|
||
|
|
||
![]()
4 years ago
|
@spec head(Request.url(), Request.headers(), keyword()) :: {:ok, Env.t()} | {:error, any()}
|
||
|
def head(url, headers \\ [], options \\ []), do: request(:head, url, "", headers, options)
|
||
|
|
||
![]()
5 years ago
|
@doc """
|
||
|
Performs POST request.
|
||
|
|
||
|
See `Pleroma.HTTP.request/5`
|
||
|
"""
|
||
|
@spec post(Request.url(), String.t(), Request.headers(), keyword()) ::
|
||
|
{:ok, Env.t()} | {:error, any()}
|
||
|
def post(url, body, headers \\ [], options \\ []),
|
||
|
do: request(:post, url, body, headers, options)
|
||
|
|
||
|
@doc """
|
||
|
Builds and performs http request.
|
||
![]()
6 years ago
|
|
||
|
# Arguments:
|
||
![]()
4 years ago
|
`method` - :get, :post, :put, :delete, :head
|
||
![]()
5 years ago
|
`url` - full url
|
||
|
`body` - request body
|
||
![]()
6 years ago
|
`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}`
|
||
|
|
||
![]()
6 years ago
|
"""
|
||
![]()
4 years ago
|
@spec request(method(), Request.url(), String.t(), Request.headers(), keyword()) ::
|
||
![]()
5 years ago
|
{:ok, Env.t()} | {:error, any()}
|
||
|
def request(method, url, body, headers, options) when is_binary(url) do
|
||
![]()
5 years ago
|
uri = URI.parse(url)
|
||
![]()
4 years ago
|
adapter_opts = AdapterHelper.options(uri, options || [])
|
||
![]()
5 years ago
|
|
||
![]()
4 years ago
|
options = put_in(options[:adapter], adapter_opts)
|
||
|
params = options[:params] || []
|
||
|
request = build_request(method, headers, options, url, body, params)
|
||
|
|
||
|
adapter = Application.get_env(:tesla, :adapter)
|
||
|
|
||
|
client = Tesla.client(adapter_middlewares(adapter), adapter)
|
||
|
|
||
|
maybe_limit(
|
||
|
fn ->
|
||
|
request(client, request)
|
||
|
end,
|
||
|
adapter,
|
||
|
adapter_opts
|
||
|
)
|
||
![]()
6 years ago
|
end
|
||
|
|
||
![]()
5 years ago
|
@spec request(Client.t(), keyword()) :: {:ok, Env.t()} | {:error, any()}
|
||
|
def request(client, request), do: Tesla.request(client, request)
|
||
![]()
7 years ago
|
|
||
![]()
5 years ago
|
defp build_request(method, headers, options, url, body, params) do
|
||
|
Builder.new()
|
||
|
|> Builder.method(method)
|
||
|
|> Builder.headers(headers)
|
||
|
|> Builder.opts(options)
|
||
|
|> Builder.url(url)
|
||
|
|> Builder.add_param(:body, :body, body)
|
||
|
|> Builder.add_param(:query, :query, params)
|
||
|
|> Builder.convert_to_keyword()
|
||
![]()
7 years ago
|
end
|
||
![]()
4 years ago
|
|
||
|
@prefix Pleroma.Gun.ConnectionPool
|
||
|
defp maybe_limit(fun, Tesla.Adapter.Gun, opts) do
|
||
|
ConcurrentLimiter.limit(:"#{@prefix}.#{opts[:pool] || :default}", fun)
|
||
|
end
|
||
|
|
||
|
defp maybe_limit(fun, _, _) do
|
||
|
fun.()
|
||
|
end
|
||
![]()
4 years ago
|
|
||
|
defp adapter_middlewares(Tesla.Adapter.Gun) do
|
||
![]()
4 years ago
|
[Tesla.Middleware.FollowRedirects, Pleroma.Tesla.Middleware.ConnectionPool]
|
||
![]()
4 years ago
|
end
|
||
|
|
||
|
defp adapter_middlewares(_), do: []
|
||
![]()
7 years ago
|
end
|