Class: Faraday::Adapter::Libuv

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

Instance Method Summary collapse

Methods inherited from Faraday::Adapter

#perform_request

Constructor Details

#initialize(app, connection_options = {}) ⇒ Libuv

Returns a new instance of Libuv.



10
11
12
13
# File 'lib/faraday/adapter/libuv.rb', line 10

def initialize(app, connection_options = {})
  @connection_options = connection_options
  super(app)
end

Instance Method Details

#call(env) ⇒ Object



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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/faraday/adapter/libuv.rb', line 15

def call(env)
  super

  opts = {}
  if env[:url].scheme == 'https' && ssl = env[:ssl]
    tls_opts = opts[:tls_options] = {}

    # opts[:ssl_verify_peer] = !!ssl.fetch(:verify, true)
    # TODO:: Need to provide verify callbacks

    tls_opts[:cert_chain] = ssl[:ca_path] if ssl[:ca_path]
    tls_opts[:client_ca] = ssl[:ca_file] if ssl[:ca_file]
    #tls_opts[:client_cert] = ssl[:client_cert] if ssl[:client_cert]
    #tls_opts[:client_key]  = ssl[:client_key]  if ssl[:client_key]
    #tls_opts[:certificate] = ssl[:certificate] if ssl[:certificate]
    tls_opts[:private_key] = ssl[:private_key] if ssl[:private_key]
  end

  if (req = env[:request])
    opts[:inactivity_timeout] = (req[:timeout] * 1000) if req[:timeout]
  end

  if proxy = env[:request][:proxy]
    opts[:proxy] = {
      host: proxy[:uri].host,
      port: proxy[:uri].port,
      username: proxy[:user],
      password: proxy[:password]
    }
  end

  error = nil
  thread = reactor
  if thread.running?
    error = perform_request(env, opts)
  else
    # Pretty much here for testing
    thread.run {
      error = perform_request(env, opts)
    }
  end

  # Re-raise the error out of the event loop
  # Really this is only required for tests as this will always run on the reactor
  raise error if error
  @app.call env
rescue ::CoroutineRejection => err
  if err.value == :timeout
    raise Error::TimeoutError, err
  else
    raise Error::ConnectionFailed, err
  end
end

#read_body(env) ⇒ Object

TODO: support streaming requests



70
71
72
# File 'lib/faraday/adapter/libuv.rb', line 70

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