Class: Google::Apis::Core::ApiCommand

Inherits:
HttpCommand show all
Defined in:
lib/google/apis/core/api_command.rb

Overview

Command for executing most basic API request with JSON requests/responses

Direct Known Subclasses

DownloadCommand

Constant Summary collapse

JSON_CONTENT_TYPE =
'application/json'
FIELDS_PARAM =
'fields'
ERROR_REASON_MAPPING =
{
  'rateLimitExceeded' => Google::Apis::RateLimitError,
  'userRateLimitExceeded' => Google::Apis::RateLimitError,
  'projectNotLinked' => Google::Apis::ProjectNotLinkedError
}

Constants inherited from HttpCommand

HttpCommand::RETRIABLE_ERRORS

Instance Attribute Summary collapse

Attributes inherited from HttpCommand

#body, #connection, #header, #method, #options, #params, #query, #url

Instance Method Summary collapse

Methods inherited from HttpCommand

#apply_request_options, #authorization_refreshable?, #do_retry, #error, #execute, #process_response, #set_api_version_header, #success

Methods included from Logging

#logger

Constructor Details

#initialize(method, url, body: nil, client_version: nil) ⇒ ApiCommand

Returns a new instance of ApiCommand.

Parameters:

  • method (symbol)

    HTTP method

  • url (String, Addressable::URI, Addressable::Template)

    HTTP URL or template

  • body (String, #read) (defaults to: nil)

    Request body



62
63
64
65
# File 'lib/google/apis/core/api_command.rb', line 62

def initialize(method, url, body: nil, client_version: nil)
  super(method, url, body: body)
  self.client_version = client_version || Core::VERSION
end

Instance Attribute Details

#client_versionString

Client library version.

Returns:

  • (String)


54
55
56
# File 'lib/google/apis/core/api_command.rb', line 54

def client_version
  @client_version
end

#request_objectObject

Request body to serialize

Returns:

  • (Object)


42
43
44
# File 'lib/google/apis/core/api_command.rb', line 42

def request_object
  @request_object
end

#request_representationGoogle::Apis::Core::JsonRepresentation

JSON serializer for request objects



38
39
40
# File 'lib/google/apis/core/api_command.rb', line 38

def request_representation
  @request_representation
end

#response_classObject

Class to instantiate when de-serializing responses

Returns:

  • (Object)


50
51
52
# File 'lib/google/apis/core/api_command.rb', line 50

def response_class
  @response_class
end

#response_representationGoogle::Apis::Core::JsonRepresentation

JSON serializer for response objects



46
47
48
# File 'lib/google/apis/core/api_command.rb', line 46

def response_representation
  @response_representation
end

Instance Method Details

#allow_form_encoding?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/google/apis/core/api_command.rb', line 141

def allow_form_encoding?
  request_representation.nil? && super
end

#check_status(status, header = nil, body = nil, message = nil)

This method returns an undefined value.

Check the response and raise error if needed

Parameters:

  • status (Fixnum)

    HTTP status code of response

  • header (Hash) (defaults to: nil)

    HTTP response headers

  • body (String) (defaults to: nil)

    HTTP response body

  • message (String) (defaults to: nil)

    Error message text

Raises:



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/google/apis/core/api_command.rb', line 122

def check_status(status, header = nil, body = nil, message = nil)
  case status
  when 400, 402...500
    reason, message = parse_error(body)
    if reason
      message = sprintf('%s: %s', reason, message)
      raise ERROR_REASON_MAPPING[reason].new(
        message,
        status_code: status,
        header: header,
        body: body
      ) if ERROR_REASON_MAPPING.key?(reason)
    end
    super(status, header, body, message)
  else
    super(status, header, body, message)
  end
end

#decode_response_body(content_type, body) ⇒ Object

Deserialize the response body if present

noinspection RubyUnusedLocalVariable

Parameters:

  • content_type (String)

    Content type of body

  • body (String, #read)

    Response body

Returns:

  • (Object)

    Response object



97
98
99
100
101
102
103
104
105
106
# File 'lib/google/apis/core/api_command.rb', line 97

def decode_response_body(content_type, body)
  return super unless response_representation
  return super if options && options.skip_deserialization
  return super if content_type.nil?
  return nil unless content_type.start_with?(JSON_CONTENT_TYPE)
  body = "{}" if body.empty?
  instance = response_class.new
  response_representation.new(instance).from_json(body, unwrap: response_class)
  instance
end

#prepare!

This method returns an undefined value.

Serialize the request body



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/google/apis/core/api_command.rb', line 70

def prepare!
  set_api_client_header
  set_user_project_header
  if options&.api_format_version
    header['X-Goog-Api-Format-Version'] = options.api_format_version.to_s
  end
  query[FIELDS_PARAM] = normalize_fields_param(query[FIELDS_PARAM]) if query.key?(FIELDS_PARAM)
  if request_representation && request_object
    header['Content-Type'] ||= JSON_CONTENT_TYPE
    if options && options.skip_serialization
      self.body = request_object
    else
      self.body = request_representation.new(request_object).to_json(user_options: { skip_undefined: true })
    end
  end
  super
end