Class: Google::Apis::Core::ApiCommand
- Inherits:
-
HttpCommand
- Object
- HttpCommand
- Google::Apis::Core::ApiCommand
- Defined in:
- lib/google/apis/core/api_command.rb
Overview
Command for executing most basic API request with JSON requests/responses
Direct Known Subclasses
Constant Summary collapse
- JSON_CONTENT_TYPE =
'application/json'- FIELDS_PARAM =
'fields'- ERROR_REASON_MAPPING =
{ 'rateLimitExceeded' => Google::Apis::RateLimitError, 'userRateLimitExceeded' => Google::Apis::RateLimitError, 'sharingRateLimitExceeded' => Google::Apis::RateLimitError, 'projectNotLinked' => Google::Apis::ProjectNotLinkedError }
Constants inherited from HttpCommand
Instance Attribute Summary collapse
-
#client_version ⇒ String
Client library version.
-
#request_object ⇒ Object
Request body to serialize.
-
#request_representation ⇒ Google::Apis::Core::JsonRepresentation
JSON serializer for request objects.
-
#response_class ⇒ Object
Class to instantiate when de-serializing responses.
-
#response_representation ⇒ Google::Apis::Core::JsonRepresentation
JSON serializer for response objects.
Attributes inherited from HttpCommand
#body, #connection, #header, #method, #options, #params, #query, #url
Instance Method Summary collapse
- #allow_form_encoding? ⇒ Boolean
-
#check_status(status, header = nil, body = nil, message = nil)
Check the response and raise error if needed.
-
#decode_response_body(content_type, body) ⇒ Object
Deserialize the response body if present.
-
#initialize(method, url, body: nil, client_version: nil) ⇒ ApiCommand
constructor
A new instance of ApiCommand.
-
#prepare!
Serialize the request body.
Methods inherited from HttpCommand
#apply_request_options, #authorization_refreshable?, #do_retry, #error, #execute, #process_response, #set_api_version_header, #success
Methods included from Logging
Constructor Details
#initialize(method, url, body: nil, client_version: nil) ⇒ ApiCommand
Returns a new instance of ApiCommand.
63 64 65 66 |
# File 'lib/google/apis/core/api_command.rb', line 63 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_version ⇒ String
Client library version.
55 56 57 |
# File 'lib/google/apis/core/api_command.rb', line 55 def client_version @client_version end |
#request_object ⇒ Object
Request body to serialize
43 44 45 |
# File 'lib/google/apis/core/api_command.rb', line 43 def request_object @request_object end |
#request_representation ⇒ Google::Apis::Core::JsonRepresentation
JSON serializer for request objects
39 40 41 |
# File 'lib/google/apis/core/api_command.rb', line 39 def request_representation @request_representation end |
#response_class ⇒ Object
Class to instantiate when de-serializing responses
51 52 53 |
# File 'lib/google/apis/core/api_command.rb', line 51 def response_class @response_class end |
#response_representation ⇒ Google::Apis::Core::JsonRepresentation
JSON serializer for response objects
47 48 49 |
# File 'lib/google/apis/core/api_command.rb', line 47 def response_representation @response_representation end |
Instance Method Details
#allow_form_encoding? ⇒ Boolean
142 143 144 |
# File 'lib/google/apis/core/api_command.rb', line 142 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
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/google/apis/core/api_command.rb', line 123 def check_status(status, header = nil, body = nil, = nil) case status when 400, 402...500 reason, = parse_error(body) if reason = sprintf('%s: %s', reason, ) raise ERROR_REASON_MAPPING[reason].new( , status_code: status, header: header, body: body ) if ERROR_REASON_MAPPING.key?(reason) end super(status, header, body, ) else super(status, header, body, ) end end |
#decode_response_body(content_type, body) ⇒ Object
Deserialize the response body if present
noinspection RubyUnusedLocalVariable
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/google/apis/core/api_command.rb', line 98 def decode_response_body(content_type, body) return super unless response_representation return super if && .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
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/google/apis/core/api_command.rb', line 71 def prepare! set_api_client_header set_user_project_header if &.api_format_version header['X-Goog-Api-Format-Version'] = .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 && .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 |