Class: Faraday::Adapter::EMHttp

Inherits:
Faraday::Adapter show all
Includes:
Options
Defined in:
lib/faraday/adapter/em_http.rb

Overview

EventMachine adapter. This adapter is useful for either asynchronous requests when in an EM reactor loop, or for making parallel requests in synchronous code.

Defined Under Namespace

Modules: Options Classes: Manager

Constant Summary

Constants inherited from Faraday::Adapter

CONTENT_LENGTH

Instance Attribute Summary

Attributes included from Parallelism

#supports_parallel

Attributes included from DependencyLoader

#load_error

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Options

#configure_compression, #configure_proxy, #configure_socket, #configure_ssl, #configure_timeout, #connection_config, #read_body, #request_config, #request_options

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

Class Method Details

.setup_parallel_manager(_options = nil) ⇒ Manager

Returns:


112
113
114
# File 'lib/faraday/adapter/em_http.rb', line 112

def self.setup_parallel_manager(_options = nil)
  Manager.new
end

Instance Method Details

#call(env) ⇒ Object


116
117
118
119
120
# File 'lib/faraday/adapter/em_http.rb', line 116

def call(env)
  super
  perform_request env
  @app.call env
end

#create_request(env) ⇒ Object


190
191
192
193
194
# File 'lib/faraday/adapter/em_http.rb', line 190

def create_request(env)
  EventMachine::HttpRequest.new(
    env[:url], connection_config(env).merge(@connection_options)
  )
end

#error_message(client) ⇒ Object


196
197
198
# File 'lib/faraday/adapter/em_http.rb', line 196

def error_message(client)
  client.error || 'request failed'
end

#parallel?(env) ⇒ Boolean

Returns:

  • (Boolean)

220
221
222
# File 'lib/faraday/adapter/em_http.rb', line 220

def parallel?(env)
  !!env[:parallel_manager]
end

#perform_request(env) ⇒ Object


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/faraday/adapter/em_http.rb', line 122

def perform_request(env)
  if parallel?(env)
    manager = env[:parallel_manager]
    manager.add do
      perform_single_request(env)
        .callback { env[:response].finish(env) }
    end
  elsif EventMachine.reactor_running?
    # EM is running: instruct upstream that this is an async request
    env[:parallel_manager] = true
    perform_single_request(env)
      .callback { env[:response].finish(env) }
      .errback do
        # TODO: no way to communicate the error in async mode
        raise NotImplementedError
      end
  else
    error = nil
    # start EM, block until request is completed
    EventMachine.run do
      perform_single_request(env)
        .callback { EventMachine.stop }
        .errback do |client|
          error = error_message(client)
          EventMachine.stop
        end
    end
    raise_error(error) if error
  end
rescue EventMachine::Connectify::CONNECTError => e
  if e.message.include?('Proxy Authentication Required')
    raise Faraday::ConnectionFailed,
          %(407 "Proxy Authentication Required ")
  end

  raise Faraday::ConnectionFailed, e
rescue StandardError => e
  if defined?(::OpenSSL::SSL::SSLError) && \
     e.is_a?(::OpenSSL::SSL::SSLError)
    raise Faraday::SSLError, e
  end

  raise
end

#perform_single_request(env) ⇒ Object

TODO: reuse the connection to support pipelining


168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/faraday/adapter/em_http.rb', line 168

def perform_single_request(env)
  req = create_request(env)
  req = req.setup_request(env[:method], request_config(env))
  req.callback do |client|
    if env[:request].stream_response?
      warn "Streaming downloads for #{self.class.name} " \
        'are not yet implemented.'
      env[:request].on_data.call(
        client.response,
        client.response.bytesize
      )
    end
    status = client.response_header.status
    reason = client.response_header.http_reason
    save_response(env, status, client.response, nil, reason) do |headers|
      client.response_header.each do |name, value|
        headers[name.to_sym] = value
      end
    end
  end
end

#raise_error(msg) ⇒ Object

Raises:

  • (error_class)

200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/faraday/adapter/em_http.rb', line 200

def raise_error(msg)
  error_class = Faraday::ClientError
  if timeout_message?(msg)
    error_class = Faraday::TimeoutError
    msg = 'request timed out'
  elsif msg == Errno::ECONNREFUSED
    error_class = Faraday::ConnectionFailed
    msg = 'connection refused'
  elsif msg == 'connection closed by server'
    error_class = Faraday::ConnectionFailed
  end
  raise error_class, msg
end

#timeout_message?(msg) ⇒ Boolean

Returns:

  • (Boolean)

214
215
216
217
# File 'lib/faraday/adapter/em_http.rb', line 214

def timeout_message?(msg)
  msg == Errno::ETIMEDOUT ||
    (msg.is_a?(String) && msg.include?('timeout error'))
end