class Async::HTTP::Faraday::Adapter

Constants

CONNECTION_EXCEPTIONS

Public Class Methods

new(*arguments, timeout: nil, **options, &block) click to toggle source
Calls superclass method
# File lib/async/http/faraday/adapter.rb, line 49
def initialize(*arguments, timeout: nil, **options, &block)
        super(*arguments, **options)
        
        @timeout = timeout
        
        @clients = {}
        
        @options = options
end

Public Instance Methods

call(env) click to toggle source
Calls superclass method
# File lib/async/http/faraday/adapter.rb, line 99
def call(env)
        super
        
        Sync do
                endpoint = Endpoint.new(env.url)
                
                if proxy = env.request.proxy
                        proxy_endpoint = Endpoint.new(proxy.uri)
                        client = self.proxy_client_for(proxy_endpoint, endpoint)
                else
                        client = self.client_for(endpoint)
                end
                
                if body = env.body
                        body = Body::Buffered.wrap(body)
                end
                
                if headers = env.request_headers
                        headers = ::Protocol::HTTP::Headers[headers]
                end
                
                method = env.method.upcase
                
                request = ::Protocol::HTTP::Request.new(endpoint.scheme, endpoint.authority, method, endpoint.path, nil, headers, body)
                
                with_timeout do
                        response = client.call(request)
                        
                        save_response(env, response.status, response.read, response.headers)
                end
        end
        
        return @app.call(env)
rescue Errno::ETIMEDOUT, Async::TimeoutError => e
        raise ::Faraday::TimeoutError, e
rescue OpenSSL::SSL::SSLError => e
        raise ::Faraday::SSLError, e
rescue *CONNECTION_EXCEPTIONS => e
        raise ::Faraday::ConnectionFailed, e
end
client_for(endpoint) click to toggle source
# File lib/async/http/faraday/adapter.rb, line 73
def client_for(endpoint)
        key = host_key(endpoint)
        
        @clients.fetch(key) do
                @clients[key] = make_client(endpoint)
        end
end
close() click to toggle source
# File lib/async/http/faraday/adapter.rb, line 90
def close
        # The order of operations here is to avoid a race condition between iterating over clients (#close may yield) and creating new clients.
        clients = @clients.values
        
        @clients.clear
        
        clients.each(&:close)
end
host_key(endpoint) click to toggle source
# File lib/async/http/faraday/adapter.rb, line 63
def host_key(endpoint)
        url = endpoint.url.dup
        
        url.path = ""
        url.fragment = nil
        url.query = nil
        
        return url
end
make_client(endpoint) click to toggle source
# File lib/async/http/faraday/adapter.rb, line 59
def make_client(endpoint)
        Client.new(endpoint, **@connection_options)
end
proxy_client_for(proxy_endpoint, endpoint) click to toggle source
# File lib/async/http/faraday/adapter.rb, line 81
def proxy_client_for(proxy_endpoint, endpoint)
        key = [host_key(proxy_endpoint), host_key(endpoint)]
        
        @clients.fetch(key) do
                client = client_for(proxy_endpoint)
                @clients[key] = client.proxied_client(endpoint)
        end
end

Private Instance Methods

with_timeout(task: Async::Task.current) { || ... } click to toggle source
# File lib/async/http/faraday/adapter.rb, line 142
def with_timeout(task: Async::Task.current)
        if @timeout
                task.with_timeout(@timeout, ::Faraday::TimeoutError) do
                        yield
                end
        else
                yield
        end
end