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
Defined Under Namespace
Modules: RedactingPPMethods Classes: RedactingPP, RedactingSingleLine
Constant Summary collapse
- RETRIABLE_ERRORS =
[Google::Apis::ServerError, Google::Apis::RateLimitError, Google::Apis::TransmissionError, Google::Apis::RequestTimeOutError]
Instance Attribute Summary collapse
-
#body ⇒ #read
Request body.
-
#connection ⇒ HTTPClient
HTTP Client.
-
#header ⇒ Hash
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_header)
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.
- #do_retry(func, client) ⇒ Object
-
#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.
-
#set_api_version_header(api_version)
Set the API version header for the service if not empty.
-
#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.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/google/apis/core/http_command.rb', line 80 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 = Hash.new self.body = body self.query = {} self.params = {} @opencensus_span = nil if OPENCENSUS_AVAILABLE logger.warn 'OpenCensus support is now deprecated. ' + 'Please refer https://github.com/googleapis/google-api-ruby-client#tracing for migrating to use OpenTelemetry.' end end |
Instance Attribute Details
#body ⇒ #read
Request body
56 57 58 |
# File 'lib/google/apis/core/http_command.rb', line 56 def body @body end |
#connection ⇒ HTTPClient
HTTP Client
64 65 66 |
# File 'lib/google/apis/core/http_command.rb', line 64 def connection @connection end |
#header ⇒ Hash
HTTP headers
52 53 54 |
# File 'lib/google/apis/core/http_command.rb', line 52 def header @header end |
#method ⇒ symbol
HTTP method
60 61 62 |
# File 'lib/google/apis/core/http_command.rb', line 60 def method @method end |
#options ⇒ Google::Apis::RequestOptions
Request options
44 45 46 |
# File 'lib/google/apis/core/http_command.rb', line 44 def @options end |
#params ⇒ Hash
Path params for URL Template
72 73 74 |
# File 'lib/google/apis/core/http_command.rb', line 72 def params @params end |
#query ⇒ Hash
Query params
68 69 70 |
# File 'lib/google/apis/core/http_command.rb', line 68 def query @query end |
#url ⇒ String, Addressable::URI
HTTP request URL
48 49 50 |
# File 'lib/google/apis/core/http_command.rb', line 48 def url @url end |
Instance Method Details
#allow_form_encoding? ⇒ Boolean
347 348 349 |
# File 'lib/google/apis/core/http_command.rb', line 347 def allow_form_encoding? [:post, :put].include?(method) && body.nil? end |
#apply_request_options(req_header)
This method returns an undefined value.
Update the request with any specified options.
338 339 340 341 342 343 344 345 |
# File 'lib/google/apis/core/http_command.rb', line 338 def (req_header) if ..respond_to?(:apply!) ..apply!(req_header) elsif ..is_a?(String) req_header['Authorization'] = sprintf('Bearer %s', .) end req_header.update(header) end |
#authorization_refreshable? ⇒ Boolean
Check if attached credentials can be automatically refreshed
158 159 160 |
# File 'lib/google/apis/core/http_command.rb', line 158 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
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/google/apis/core/http_command.rb', line 225 def check_status(status, header = nil, body = nil, = nil) # TODO: 304 Not Modified depends on context... case status when 200...300, 308 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 429 ||= 'Rate limit exceeded' raise Google::Apis::RateLimitError.new(, status_code: status, header: header, body: body) when 408 ||= 'Request time out' raise Google::Apis::RequestTimeOutError.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
262 263 264 |
# File 'lib/google/apis/core/http_command.rb', line 262 def decode_response_body(_content_type, body) body end |
#do_retry(func, client) ⇒ Object
116 117 118 119 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 |
# File 'lib/google/apis/core/http_command.rb', line 116 def do_retry func, client begin Retriable.retriable tries: .retries + 1, max_elapsed_time: .max_elapsed_time, base_interval: .base_interval, max_interval: .max_interval, multiplier: .multiplier, 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, Signet::AuthorizationError, Signet::RemoteServerError, Signet::UnexpectedStatusError], on_retry: proc { |*| } do send(func, 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 end |
#error(err, rethrow: false) {|nil, err| ... }
This method returns an undefined value.
Process an error response
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/google/apis/core/http_command.rb', line 285 def error(err, rethrow: false, &block) logger.debug { sprintf('Error - %s', PP.pp(err, '')) } if err.is_a?(HTTPClient::BadResponseError) begin res = err.res raise Google::Apis::TransmissionError.new(err) if res.nil? check_status(res.status.to_i, res.header, res.body) rescue Google::Apis::Error => e err = e end elsif err.is_a?(HTTPClient::TimeoutError) || err.is_a?(SocketError) || err.is_a?(HTTPClient::KeepAliveDisconnected) || err.is_a?(Errno::ECONNREFUSED) || err.is_a?(Errno::ETIMEDOUT) err = Google::Apis::TransmissionError.new(err) end 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
106 107 108 109 110 111 112 113 114 |
# File 'lib/google/apis/core/http_command.rb', line 106 def execute(client, &block) prepare! opencensus_begin_span do_retry :execute_once, client, &block ensure opencensus_end_span @http_res = nil release! end |
#process_response(status, header, body) ⇒ Object
Check the response and either decode body or raise error
206 207 208 209 |
# File 'lib/google/apis/core/http_command.rb', line 206 def process_response(status, header, body) check_status(status, header, body) decode_response_body(header['Content-Type'].first, body) end |
#set_api_version_header(api_version)
This method returns an undefined value.
Set the API version header for the service if not empty.
353 354 355 |
# File 'lib/google/apis/core/http_command.rb', line 353 def set_api_version_header api_version self.header['X-Goog-Api-Version'] = api_version unless api_version.empty? end |
#success(result) {|result, nil| ... } ⇒ Object
Process a success response
271 272 273 274 275 |
# File 'lib/google/apis/core/http_command.rb', line 271 def success(result, &block) logger.debug { sprintf('Success - %s', safe_pretty_representation(result)) } block.call(result, nil) if block_given? result end |