Class: Faraday::Adapter::EMHttp
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
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
Class Method Details
.setup_parallel_manager(_options = nil) ⇒ Manager
110
111
112
|
# File 'lib/faraday/adapter/em_http.rb', line 110
def self.setup_parallel_manager(_options = nil)
Manager.new
end
|
Instance Method Details
#call(env) ⇒ Object
114
115
116
117
118
|
# File 'lib/faraday/adapter/em_http.rb', line 114
def call(env)
super
perform_request env
@app.call env
end
|
#create_request(env) ⇒ Object
188
189
190
191
192
|
# File 'lib/faraday/adapter/em_http.rb', line 188
def create_request(env)
EventMachine::HttpRequest.new(
env[:url], connection_config(env).merge(@connection_options)
)
end
|
#error_message(client) ⇒ Object
194
195
196
|
# File 'lib/faraday/adapter/em_http.rb', line 194
def error_message(client)
client.error || 'request failed'
end
|
#parallel?(env) ⇒ Boolean
218
219
220
|
# File 'lib/faraday/adapter/em_http.rb', line 218
def parallel?(env)
!!env[:parallel_manager]
end
|
120
121
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
|
# File 'lib/faraday/adapter/em_http.rb', line 120
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?
env[:parallel_manager] = true
perform_single_request(env)
.callback { env[:response].finish(env) }
.errback do
raise NotImplementedError
end
else
error = nil
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
|
TODO: reuse the connection to support pipelining
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
# File 'lib/faraday/adapter/em_http.rb', line 166
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..status
reason = client..http_reason
save_response(env, status, client.response, nil, reason) do ||
client..each do |name, value|
[name.to_sym] = value
end
end
end
end
|
#raise_error(msg) ⇒ Object
198
199
200
201
202
203
204
205
206
207
208
209
210
|
# File 'lib/faraday/adapter/em_http.rb', line 198
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
212
213
214
215
|
# File 'lib/faraday/adapter/em_http.rb', line 212
def timeout_message?(msg)
msg == Errno::ETIMEDOUT ||
(msg.is_a?(String) && msg.include?('timeout error'))
end
|