replace faraday with typhoeus in http_client

This commit is contained in:
astra 2024-10-08 21:01:17 -04:00
parent 4468b8b8e1
commit 931dbdb655
4 changed files with 28 additions and 40 deletions

View file

@ -128,6 +128,16 @@ module DiasporaFederation
# @return [String] user agent
attr_reader :http_user_agent
# Outbound HTTP proxy
#
# @overload http_proxy
# @return [String] http proxy
# @overload http_proxy=
# @example
# config.http_proxy = AppConfig.environment.http_proxy
# @param [String] value http proxy
attr_accessor :http_proxy
# Configure the federation library
#
# @example

View file

@ -20,7 +20,8 @@ module DiasporaFederation
method: :post,
verbose: DiasporaFederation.http_verbose,
cainfo: DiasporaFederation.certificate_authorities,
forbid_reuse: true
forbid_reuse: true,
proxy: DiasporaFederation.http_proxy
}
end

View file

@ -1,10 +1,9 @@
# frozen_string_literal: true
require "faraday"
require "faraday/follow_redirects"
require "typhoeus"
module DiasporaFederation
# A wrapper for {https://github.com/lostisland/faraday Faraday}
# HTTP GET wrapper
#
# @see Discovery::Discovery
# @see Federation::Fetcher
@ -12,32 +11,21 @@ module DiasporaFederation
# Perform a GET request
#
# @param [String] uri the URI
# @return [Faraday::Response] the response
# @return [Typhoeus::Response] the response
def self.get(uri)
connection.get(uri)
end
# Gets the Faraday connection
#
# @return [Faraday::Connection] the response
def self.connection
create_default_connection unless @connection
@connection.dup
end
private_class_method def self.create_default_connection
options = {
request: {timeout: DiasporaFederation.http_timeout},
ssl: {ca_file: DiasporaFederation.certificate_authorities}
opts = {
followlocation: false,
timeout: DiasporaFederation.http_timeout,
method: :get,
verbose: DiasporaFederation.http_verbose,
cainfo: DiasporaFederation.certificate_authorities,
forbid_reuse: true,
proxy: DiasporaFederation.http_proxy,
headers: {"User-Agent" => DiasporaFederation.http_user_agent}
}
@connection = Faraday::Connection.new(options) do |builder|
builder.use Faraday::FollowRedirects::Middleware, limit: DiasporaFederation.http_redirect_limit
builder.adapter Faraday.default_adapter
end
@connection.headers["User-Agent"] = DiasporaFederation.http_user_agent
req = Typhoeus::Request.new(uri, opts)
req.run
req.response
end
end
end

View file

@ -34,7 +34,7 @@ module DiasporaFederation
.to_return(status: 302, headers: {"Location" => "http://www.example.com"})
expect { HttpClient.get("http://www.example.com") }
.to raise_error Faraday::FollowRedirects::RedirectLimitReached
.to_return(success: false, return_code: :too_many_redirects)
end
it "uses the gem name as User-Agent" do
@ -44,16 +44,5 @@ module DiasporaFederation
HttpClient.get("http://www.example.com")
end
end
describe ".connection" do
it "returns a new connection every time" do
expect(HttpClient.connection).to be_a Faraday::Connection
end
it "returns a new connection every time" do
connection1 = HttpClient.connection
expect(HttpClient.connection).to_not be(connection1)
end
end
end
end