Class: Faraday::Adapter::HTTPX

Inherits:
Faraday::Adapter show all
Includes:
RequestMixin
Defined in:
lib/httpx/adapters/faraday.rb

Defined Under Namespace

Modules: RequestMixin Classes: ParallelManager, Session

Constant Summary collapse

SSL_ERROR =
if defined?(Faraday::SSLError)
  Faraday::SSLError
else
  Faraday::Error::SSLError
end
CONNECTION_FAILED_ERROR =
if defined?(Faraday::ConnectionFailed)
  Faraday::ConnectionFailed
else
  Faraday::Error::ConnectionFailed
end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ HTTPX

Returns a new instance of HTTPX.



171
172
173
174
# File 'lib/httpx/adapters/faraday.rb', line 171

def initialize(app)
  super(app)
  @session = Session.new
end

Class Method Details

.setup_parallel_managerObject



166
167
168
# File 'lib/httpx/adapters/faraday.rb', line 166

def setup_parallel_manager
  ParallelManager.new
end

Instance Method Details

#call(env) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/httpx/adapters/faraday.rb', line 176

def call(env)
  super
  if parallel?(env)
    handler = env[:parallel_manager].enqueue(env)
    handler.on_response do |response|
      save_response(env, response.status, response.body.to_s, response.headers, response.reason) do |response_headers|
        response_headers.merge!(response.headers)
      end
    end
    return handler
  end

  request_options = build_request(env)

  session = @session.with(options_from_env(env))
  session = session.plugin(:proxy).with_proxy(proxy_options) if env.request.proxy
  response = session.__send__(*request_options)
  response.raise_for_status unless response.is_a?(::HTTPX::Response)
  save_response(env, response.status, response.body.to_s, response.headers, response.reason) do |response_headers|
    response_headers.merge!(response.headers)
  end
  @app.call(env)
rescue OpenSSL::SSL::SSLError => err
  raise SSL_ERROR, err
rescue Errno::ECONNABORTED,
       Errno::ECONNREFUSED,
       Errno::ECONNRESET,
       Errno::EHOSTUNREACH,
       Errno::EINVAL,
       Errno::ENETUNREACH,
       Errno::EPIPE => err
  raise CONNECTION_FAILED_ERROR, err
end