Class: Requests::HTTPAdapter
- Inherits:
-
Object
- Object
- Requests::HTTPAdapter
- Defined in:
- lib/requests/adapters.rb
Constant Summary collapse
- METHS =
{ 'GET' => Net::HTTP::Get, 'POST' => Net::HTTP::Post, 'PUT' => Net::HTTP::Put, 'PATCH' => Net::HTTP::Patch, 'DELETE' => Net::HTTP::Delete, 'HEAD' => Net::HTTP::Head, 'OPTIONS' => Net::HTTP::Options}.freeze
Instance Attribute Summary collapse
-
#backoff_factor ⇒ Object
Returns the value of attribute backoff_factor.
-
#max_retries ⇒ Object
Returns the value of attribute max_retries.
-
#status_forcelist ⇒ Object
Returns the value of attribute status_forcelist.
Instance Method Summary collapse
- #close_pool ⇒ Object
- #decode_body(net_resp) ⇒ Object
-
#initialize(max_retries: 0, backoff_factor: 0, status_forcelist: []) ⇒ HTTPAdapter
constructor
A new instance of HTTPAdapter.
- #send_once(meth, url, hdrs, body, timeout, proxies, verify, cert, auth) ⇒ Object
- #stream_to(meth, url, hdrs, body, timeout, proxies, verify, cert, auth, io, chunk_size: 65536, progress: nil) ⇒ Object
Constructor Details
#initialize(max_retries: 0, backoff_factor: 0, status_forcelist: []) ⇒ HTTPAdapter
Returns a new instance of HTTPAdapter.
20 21 22 23 24 25 |
# File 'lib/requests/adapters.rb', line 20 def initialize(max_retries: 0, backoff_factor: 0, status_forcelist: []) @max_retries = max_retries @backoff_factor = backoff_factor @status_forcelist = status_forcelist @pool = {} end |
Instance Attribute Details
#backoff_factor ⇒ Object
Returns the value of attribute backoff_factor.
19 20 21 |
# File 'lib/requests/adapters.rb', line 19 def backoff_factor @backoff_factor end |
#max_retries ⇒ Object
Returns the value of attribute max_retries.
19 20 21 |
# File 'lib/requests/adapters.rb', line 19 def max_retries @max_retries end |
#status_forcelist ⇒ Object
Returns the value of attribute status_forcelist.
19 20 21 |
# File 'lib/requests/adapters.rb', line 19 def status_forcelist @status_forcelist end |
Instance Method Details
#close_pool ⇒ Object
44 45 46 47 |
# File 'lib/requests/adapters.rb', line 44 def close_pool @pool.each_value { |h| (h.finish if h.started?) rescue nil } @pool.clear end |
#decode_body(net_resp) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/requests/adapters.rb', line 85 def decode_body(net_resp) raw = net_resp.body return '' if raw.nil? case net_resp['content-encoding'].to_s.downcase when 'gzip', 'x-gzip' Zlib::GzipReader.new(StringIO.new(raw)).read when 'deflate' begin Zlib::Inflate.inflate(raw) rescue Zlib::DataError Zlib::Inflate.new(-Zlib::MAX_WBITS).inflate(raw) end else raw end rescue Zlib::Error => e raise Requests::ContentDecodingError, "couldn't decode response body: #{e.}" end |
#send_once(meth, url, hdrs, body, timeout, proxies, verify, cert, auth) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/requests/adapters.rb', line 26 def send_once(meth, url, hdrs, body, timeout, proxies, verify, cert, auth) attempt = 0 loop do begin r = do_send(meth, url, hdrs, body, timeout, proxies, verify, cert, auth) if @status_forcelist.include?(r.code.to_i) && attempt < @max_retries attempt += 1 sleep(@backoff_factor * attempt) if @backoff_factor.to_f > 0 next end return r rescue Requests::ConnectionError, Requests::ConnectTimeout => e attempt += 1 raise e unless attempt <= @max_retries && idempotent?(meth) sleep(@backoff_factor * attempt) if @backoff_factor.to_f > 0 end end end |
#stream_to(meth, url, hdrs, body, timeout, proxies, verify, cert, auth, io, chunk_size: 65536, progress: nil) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/requests/adapters.rb', line 48 def stream_to(meth, url, hdrs, body, timeout, proxies, verify, cert, auth, io, chunk_size: 65536, progress: nil) u = URI.parse(url) auth.call(hdrs) if auth.respond_to?(:call) && !auth.is_a?(Requests::DigestAuth) open_t, read_t = split_timeout(timeout) http = conn_for(u, proxies, verify, cert, open_t, read_t) klass = METHS[meth] || Net::HTTP::Get req = klass.new(u.request_uri) hdrs.each { |k, v| req[k] = v } req.body = body if body net_resp = nil total = 0 begin http.request(req) do |nr| net_resp = nr dec = stream_decoder(nr) nr.read_body do |chunk| piece = dec == :raw ? chunk : dec.inflate(chunk) io.write(piece) total += piece.bytesize progress.call(total, nr.content_length) if progress end end rescue Net::OpenTimeout drop_conn(u, proxies) raise Requests::ConnectTimeout, "connect timeout: #{url}" rescue Net::ReadTimeout drop_conn(u, proxies) raise Requests::ReadTimeout, "read timeout: #{url}" rescue OpenSSL::SSL::SSLError => e drop_conn(u, proxies) raise Requests::SSLError, e. rescue EOFError, IOError, Errno::ECONNRESET, Errno::EPIPE, SocketError, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ETIMEDOUT => e drop_conn(u, proxies) raise Requests::ConnectionError, e. end net_resp end |