-
Notifications
You must be signed in to change notification settings - Fork 12
/
http.rb
55 lines (45 loc) · 1.43 KB
/
http.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
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
45
46
47
48
49
50
51
52
53
54
55
# frozen_string_literal: true
module Faraday
class Adapter
# HTTP.rb adapter.
class HTTP < Faraday::Adapter
# Takes the environment and performs the request.
#
# @param env [Faraday::Env] the request environment.
#
# @return [Faraday::Response] the response.
def call(env)
super
perform_request(env)
@app.call(env)
end
private
def perform_request(env)
conn = setup_connection(env)
resp = conn.request env[:method], env[:url], body: env[:body]
save_response(env, resp.code, resp.body.to_s, resp.headers, resp.status.reason)
rescue ::HTTP::TimeoutError
raise Faraday::TimeoutError, $ERROR_INFO
rescue ::HTTP::ConnectionError
raise Faraday::ConnectionFailed, $ERROR_INFO
end
def setup_connection(env)
conn = ::HTTP
request_config(conn, env[:request]) if env[:request]
conn.headers(env.request_headers)
end
def request_config(conn, config)
if (timeout = config[:timeout])
conn = conn.timeout(connect: timeout, read: timeout, write: timeout)
end
if (timeout = config[:open_timeout])
conn = conn.timeout(connect: timeout, write: timeout)
end
if (proxy = config[:proxy])
conn = conn.via(proxy.uri.host, proxy.uri.port, proxy.user, proxy.password)
end
conn
end
end
end
end