Class: Faraday::Adapter::Excon

Inherits:
Faraday::Adapter show all
Defined in:
lib/faraday/adapter/excon.rb

Overview

Excon adapter.

Constant Summary

Constants inherited from Faraday::Adapter

CONTENT_LENGTH

Instance Attribute Summary

Attributes included from Parallelism

#supports_parallel

Attributes included from DependencyLoader

#load_error

Instance Method Summary collapse

Methods inherited from Faraday::Adapter

#close, #connection, #initialize

Methods included from Faraday::AutoloadHelper

#all_loaded_constants, #autoload_all, #load_autoloaded_constants

Methods included from Parallelism

#inherited, #supports_parallel?

Methods included from DependencyLoader

#dependency, #inherited, #loaded?, #new

Methods included from MiddlewareRegistry

#fetch_middleware, #load_middleware, #lookup_middleware, #middleware_mutex, #register_middleware, #unregister_middleware

Constructor Details

This class inherits a constructor from Faraday::Adapter

Instance Method Details

#build_connection(env) ⇒ Object


9
10
11
12
# File 'lib/faraday/adapter/excon.rb', line 9

def build_connection(env)
  opts = opts_from_env(env)
  ::Excon.new(env[:url].to_s, opts.merge(@connection_options))
end

#call(env) ⇒ Object


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/faraday/adapter/excon.rb', line 14

def call(env)
  super

  req_opts = {
    method: env[:method].to_s.upcase,
    headers: env[:request_headers],
    body: read_body(env)
  }

  req = env[:request]
  if req&.stream_response?
    total = 0
    req_opts[:response_block] = lambda do |chunk, _remain, _total|
      req.on_data.call(chunk, total += chunk.size)
    end
  end

  resp = connection(env) { |http| http.request(req_opts) }
  save_response(env, resp.status.to_i, resp.body, resp.headers,
                resp.reason_phrase)

  @app.call(env)
rescue ::Excon::Errors::SocketError => e
  raise Faraday::TimeoutError, e if e.message.match?(/\btimeout\b/)

  raise Faraday::SSLError, e if e.message.match?(/\bcertificate\b/)

  raise Faraday::ConnectionFailed, e
rescue ::Excon::Errors::Timeout => e
  raise Faraday::TimeoutError, e
end

#read_body(env) ⇒ Object

TODO: support streaming requests


47
48
49
# File 'lib/faraday/adapter/excon.rb', line 47

def read_body(env)
  env[:body].respond_to?(:read) ? env[:body].read : env[:body]
end