Class: Async::HTTP::Faraday::Adapter
- Inherits:
-
Faraday::Adapter
- Object
- Faraday::Adapter
- Async::HTTP::Faraday::Adapter
- Defined in:
- lib/async/http/faraday/adapter.rb
Constant Summary collapse
- CONNECTION_EXCEPTIONS =
[ Errno::EADDRNOTAVAIL, Errno::ECONNABORTED, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::EHOSTUNREACH, Errno::EINVAL, Errno::ENETUNREACH, Errno::EPIPE, IOError, SocketError ].freeze
Instance Method Summary collapse
- #call(env) ⇒ Object
- #client_for(endpoint) ⇒ Object
- #close ⇒ Object
- #host_key(endpoint) ⇒ Object
-
#initialize(*arguments, timeout: nil, **options, &block) ⇒ Adapter
constructor
A new instance of Adapter.
- #make_client(endpoint) ⇒ Object
- #proxy_client_for(proxy_endpoint, endpoint) ⇒ Object
Constructor Details
#initialize(*arguments, timeout: nil, **options, &block) ⇒ Adapter
Returns a new instance of Adapter.
49 50 51 52 53 54 55 56 57 |
# File 'lib/async/http/faraday/adapter.rb', line 49 def initialize(*arguments, timeout: nil, **, &block) super(*arguments, **) @timeout = timeout @clients = {} @options = end |
Instance Method Details
#call(env) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# 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., 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) ⇒ Object
73 74 75 76 77 78 79 |
# 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 ⇒ Object
90 91 92 93 94 95 96 97 |
# 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) ⇒ Object
63 64 65 66 67 68 69 70 71 |
# 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) ⇒ Object
59 60 61 |
# 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) ⇒ Object
81 82 83 84 85 86 87 88 |
# 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 |