Class: Google::Apis::Core::HttpCommand
- Inherits:
-
Object
- Object
- Google::Apis::Core::HttpCommand
- Includes:
- Logging
- Defined in:
- lib/google/apis/core/http_command.rb
Overview
Command for HTTP request/response.
Direct Known Subclasses
Constant Summary collapse
- RETRIABLE_ERRORS =
[Google::Apis::ServerError, Google::Apis::RateLimitError, Google::Apis::TransmissionError]
Instance Attribute Summary collapse
-
#body ⇒ #read
Request body.
-
#connection ⇒ Hurley::Client
HTTP Client.
-
#header ⇒ Hurley::Header
HTTP headers.
-
#method ⇒ symbol
HTTP method.
-
#options ⇒ Google::Apis::RequestOptions
Request options.
-
#params ⇒ Hash
Path params for URL Template.
-
#query ⇒ Hash
Query params.
-
#url ⇒ String, Addressable::URI
HTTP request URL.
Instance Method Summary collapse
- #allow_form_encoding? ⇒ Boolean
-
#apply_request_options(req)
Update the request with any specified options.
-
#authorization_refreshable? ⇒ Boolean
Check if attached credentials can be automatically refreshed.
-
#check_status(status, header = nil, body = nil, message = nil)
Check the response and raise error if needed.
-
#decode_response_body(_content_type, body) ⇒ Object
Process the actual response body.
-
#error(err, rethrow: false) {|nil, err| ... }
Process an error response.
-
#execute(client) {|result, err| ... } ⇒ Object
Execute the command, retrying as necessary.
-
#initialize(method, url, body: nil) ⇒ HttpCommand
constructor
A new instance of HttpCommand.
-
#process_response(status, header, body) ⇒ Object
Check the response and either decode body or raise error.
-
#success(result) {|result, nil| ... } ⇒ Object
Process a success response.
Methods included from Logging
Constructor Details
#initialize(method, url, body: nil) ⇒ HttpCommand
Returns a new instance of HttpCommand.
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/google/apis/core/http_command.rb', line 73 def initialize(method, url, body: nil) self. = Google::Apis::RequestOptions.default.dup self.url = url self.url = Addressable::Template.new(url) if url.is_a?(String) self.method = method self.header = Hurley::Header.new self.body = body self.query = {} self.params = {} end |
Instance Attribute Details
#body ⇒ #read
Request body
49 50 51 |
# File 'lib/google/apis/core/http_command.rb', line 49 def body @body end |
#connection ⇒ Hurley::Client
HTTP Client
57 58 59 |
# File 'lib/google/apis/core/http_command.rb', line 57 def connection @connection end |
#header ⇒ Hurley::Header
HTTP headers
45 46 47 |
# File 'lib/google/apis/core/http_command.rb', line 45 def header @header end |
#method ⇒ symbol
HTTP method
53 54 55 |
# File 'lib/google/apis/core/http_command.rb', line 53 def method @method end |
#options ⇒ Google::Apis::RequestOptions
Request options
37 38 39 |
# File 'lib/google/apis/core/http_command.rb', line 37 def @options end |
#params ⇒ Hash
Path params for URL Template
65 66 67 |
# File 'lib/google/apis/core/http_command.rb', line 65 def params @params end |
#query ⇒ Hash
Query params
61 62 63 |
# File 'lib/google/apis/core/http_command.rb', line 61 def query @query end |
#url ⇒ String, Addressable::URI
HTTP request URL
41 42 43 |
# File 'lib/google/apis/core/http_command.rb', line 41 def url @url end |
Instance Method Details
#allow_form_encoding? ⇒ Boolean
309 310 311 |
# File 'lib/google/apis/core/http_command.rb', line 309 def allow_form_encoding? [:post, :put].include?(method) && body.nil? end |
#apply_request_options(req)
This method returns an undefined value.
Update the request with any specified options.
298 299 300 301 302 303 304 305 306 307 |
# File 'lib/google/apis/core/http_command.rb', line 298 def (req) if ..respond_to?(:apply!) ..apply!(req.header) elsif ..is_a?(String) req.header[:authorization] = sprintf('Bearer %s', .) end req.header.update(header) req..timeout = .timeout_sec req..open_timeout = .open_timeout_sec end |
#authorization_refreshable? ⇒ Boolean
Check if attached credentials can be automatically refreshed
136 137 138 |
# File 'lib/google/apis/core/http_command.rb', line 136 def ..respond_to?(:apply!) end |
#check_status(status, header = nil, body = nil, message = nil)
This method returns an undefined value.
Check the response and raise error if needed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/google/apis/core/http_command.rb', line 198 def check_status(status, header = nil, body = nil, = nil) # TODO: 304 Not Modified depends on context... case status when 200...300 nil when 301, 302, 303, 307 ||= sprintf('Redirect to %s', header[:location]) raise Google::Apis::RedirectError.new(, status_code: status, header: header, body: body) when 401 ||= 'Unauthorized' raise Google::Apis::AuthorizationError.new(, status_code: status, header: header, body: body) when 304, 400, 402...500 ||= 'Invalid request' raise Google::Apis::ClientError.new(, status_code: status, header: header, body: body) when 500...600 ||= 'Server error' raise Google::Apis::ServerError.new(, status_code: status, header: header, body: body) else logger.warn(sprintf('Encountered unexpected status code %s', status)) ||= 'Unknown error' raise Google::Apis::TransmissionError.new(, status_code: status, header: header, body: body) end end |
#decode_response_body(_content_type, body) ⇒ Object
Process the actual response body. Intended to be overridden by subclasses
229 230 231 |
# File 'lib/google/apis/core/http_command.rb', line 229 def decode_response_body(_content_type, body) body end |
#error(err, rethrow: false) {|nil, err| ... }
This method returns an undefined value.
Process an error response
252 253 254 255 256 257 |
# File 'lib/google/apis/core/http_command.rb', line 252 def error(err, rethrow: false, &block) logger.debug { sprintf('Error - %s', PP.pp(err, '')) } err = Google::Apis::TransmissionError.new(err) if err.is_a?(Hurley::ClientError) || err.is_a?(SocketError) block.call(nil, err) if block_given? fail err if rethrow || block.nil? end |
#execute(client) {|result, err| ... } ⇒ Object
Execute the command, retrying as necessary
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/google/apis/core/http_command.rb', line 93 def execute(client) prepare! begin Retriable.retriable tries: .retries + 1, base_interval: 1, multiplier: 2, on: RETRIABLE_ERRORS do |try| # This 2nd level retriable only catches auth errors, and supports 1 retry, which allows # auth to be re-attempted without having to retry all sorts of other failures like # NotFound, etc auth_tries = (try == 1 && ? 2 : 1) Retriable.retriable tries: auth_tries, on: [Google::Apis::AuthorizationError], on_retry: proc { |*| } do execute_once(client).tap do |result| if block_given? yield result, nil end end end end rescue => e if block_given? yield nil, e else raise e end end ensure release! end |
#process_response(status, header, body) ⇒ Object
Check the response and either decode body or raise error
178 179 180 181 |
# File 'lib/google/apis/core/http_command.rb', line 178 def process_response(status, header, body) check_status(status, header, body) decode_response_body(header[:content_type], body) end |
#success(result) {|result, nil| ... } ⇒ Object
Process a success response
238 239 240 241 242 |
# File 'lib/google/apis/core/http_command.rb', line 238 def success(result, &block) logger.debug { sprintf('Success - %s', PP.pp(result, '')) } block.call(result, nil) if block_given? result end |