class Google::Apis::Core::HttpCommand

Command for HTTP request/response.

Constants

RETRIABLE_ERRORS

Attributes

body[RW]

Request body @return [#read]

connection[RW]

HTTP Client @return [Hurley::Client]

header[RW]

HTTP headers @return [Hurley::Header]

method[RW]

HTTP method @return [symbol]

options[RW]

Request options @return [Google::Apis::RequestOptions]

params[RW]

Path params for URL Template @return [Hash]

query[RW]

Query params @return [Hash]

url[RW]

HTTP request URL @return [String, Addressable::URI]

Public Class Methods

new(method, url, body: nil) click to toggle source

@param [symbol] method

HTTP method

@param [String,Addressable::URI, Addressable::Template] url

HTTP URL or template

@param [String, read] body

Request body
# File lib/google/apis/core/http_command.rb, line 73
def initialize(method, url, body: nil)
  self.options = 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

Public Instance Methods

apply_request_options(req) click to toggle source

Update the request with any specified options. @param [Hurley::Request] req

HTTP request

@return [void]

# File lib/google/apis/core/http_command.rb, line 298
def apply_request_options(req)
  if options.authorization.respond_to?(:apply!)
    options.authorization.apply!(req.header)
  elsif options.authorization.is_a?(String)
    req.header[:authorization] = sprintf('Bearer %s', options.authorization)
  end
  req.header.update(header)
  req.options.timeout = options.timeout_sec
  req.options.open_timeout = options.open_timeout_sec
end
authorization_refreshable?() click to toggle source

Check if attached credentials can be automatically refreshed @return [Boolean]

# File lib/google/apis/core/http_command.rb, line 136
def authorization_refreshable?
  options.authorization.respond_to?(:apply!)
end
check_status(status, header = nil, body = nil, message = nil) click to toggle source

Check the response and raise error if needed

@param [Fixnum] status

HTTP status code of response

@param @param [Hurley::Header] header

HTTP response headers

@param [String] body

HTTP response body

@param [String] message

Error message text

@return [void] @raise [Google::Apis::ServerError] An error occurred on the server and the request can be retried @raise [Google::Apis::ClientError] The request is invalid and should not be retried without modification @raise [Google::Apis::AuthorizationError] Authorization is required

# File lib/google/apis/core/http_command.rb, line 198
def check_status(status, header = nil, body = nil, message = nil)
  # TODO: 304 Not Modified depends on context...
  case status
  when 200...300
    nil
  when 301, 302, 303, 307
    message ||= sprintf('Redirect to %s', header[:location])
    raise Google::Apis::RedirectError.new(message, status_code: status, header: header, body: body)
  when 401
    message ||= 'Unauthorized'
    raise Google::Apis::AuthorizationError.new(message, status_code: status, header: header, body: body)
  when 304, 400, 402...500
    message ||= 'Invalid request'
    raise Google::Apis::ClientError.new(message, status_code: status, header: header, body: body)
  when 500...600
    message ||= 'Server error'
    raise Google::Apis::ServerError.new(message, status_code: status, header: header, body: body)
  else
    logger.warn(sprintf('Encountered unexpected status code %s', status))
    message ||= 'Unknown error'
    raise Google::Apis::TransmissionError.new(message, status_code: status, header: header, body: body)
  end
end
decode_response_body(_content_type, body) click to toggle source

Process the actual response body. Intended to be overridden by subclasses

@param [String] _content_type

Content type of body

@param [String, read] body

Response body

@return [Object]

# File lib/google/apis/core/http_command.rb, line 229
def decode_response_body(_content_type, body)
  body
end
error(err, rethrow: false, &block) click to toggle source

Process an error response @param [StandardError] err

Error object

@param [Boolean] rethrow

True if error should be raised again after handling

@return [void] @yield [nil, err] if block given @raise [StandardError] if no block

# 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, nil| ... } click to toggle source

Execute the command, retrying as necessary

@param [Hurley::Client] client

HTTP client

@yield [result, err] Result or error if block supplied @return [Object] @raise [Google::Apis::ServerError] An error occurred on the server and the request can be retried @raise [Google::Apis::ClientError] The request is invalid and should not be retried without modification @raise [Google::Apis::AuthorizationError] Authorization is required

# File lib/google/apis/core/http_command.rb, line 93
def execute(client)
  prepare!
  begin
    Retriable.retriable tries: options.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 && authorization_refreshable? ? 2 : 1)
      Retriable.retriable tries: auth_tries,
                          on: [Google::Apis::AuthorizationError],
                          on_retry: proc { |*| refresh_authorization } 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
execute_once(client) click to toggle source

Execute the command once.

@private @param [Hurley::Client] client

HTTP client

@return [Object] @raise [Google::Apis::ServerError] An error occurred on the server and the request can be retried @raise [Google::Apis::ClientError] The request is invalid and should not be retried without modification @raise [Google::Apis::AuthorizationError] Authorization is required

# File lib/google/apis/core/http_command.rb, line 268
def execute_once(client)
  body.rewind if body.respond_to?(:rewind)
  begin
    logger.debug { sprintf('Sending HTTP %s %s', method, url) }
    response = client.send(method, url, body) do |req|
      # Temporary workaround for Hurley bug where the connection preference
      # is ignored and it uses nested anyway
      unless form_encoded?
        req.url.query_class = Hurley::Query::Flat
        query.each do | k, v|
         req.url.query[k] = normalize_query_value(v)
        end
      end
      # End workaround
      apply_request_options(req)
    end
    logger.debug { response.status_code }
    logger.debug { response.inspect }
    response = process_response(response.status_code, response.header, response.body)
    success(response)
  rescue => e
    logger.debug { sprintf('Caught error %s', e) }
    error(e, rethrow: true)
  end
end
prepare!() click to toggle source

Prepare the request (e.g. calculate headers, serialize data, etc) before sending

@private @return [void]

# File lib/google/apis/core/http_command.rb, line 144
def prepare!
  header.update(options.header) if options && options.header
  self.url = url.expand(params) if url.is_a?(Addressable::Template)
  url.query_values = query.merge(url.query_values || {})

  if [:post, :put].include?(method)  && body.nil?
    @form_encoded = true
    self.body = Addressable::URI.form_encode(url.query_values(Array))
    self.header['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
    self.url.query_values = {}
  else
    @form_encoded = false
  end
end
process_response(status, header, body) click to toggle source

Check the response and either decode body or raise error

@param [Fixnum] status

HTTP status code of response

@param [Hurley::Header] header

Response headers

@param [String, read] body

Response body

@return [Object]

Response object

@raise [Google::Apis::ServerError] An error occurred on the server and the request can be retried @raise [Google::Apis::ClientError] The request is invalid and should not be retried without modification @raise [Google::Apis::AuthorizationError] Authorization is required

# 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
refresh_authorization() click to toggle source

Refresh the authorization authorization after a 401 error

@private @return [void]

# File lib/google/apis/core/http_command.rb, line 129
def refresh_authorization
  # Handled implicitly by auth lib, here in case need to override
  logger.debug('Retrying after authentication failure')
end
release!() click to toggle source

Release any resources used by this command @private @return [void]

# File lib/google/apis/core/http_command.rb, line 162
def release!
end
success(result, &block) click to toggle source

Process a success response @param [Object] result

Result object

@return [Object] result if no block given @yield [result, nil] if block given

# 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

Private Instance Methods

form_encoded?() click to toggle source
# File lib/google/apis/core/http_command.rb, line 311
def form_encoded?
  @form_encoded
end
normalize_query_value(v) click to toggle source
# File lib/google/apis/core/http_command.rb, line 315
def normalize_query_value(v)
  case v
  when Array
    v.map { |v2| normalize_query_value(v2) }
  when nil
    nil
  else
    v.to_s
  end
end