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'
- RATE_LIMIT_ERRORS =
%w(rateLimitExceeded userRateLimitExceeded)
Constants inherited from HttpCommand
Instance Attribute Summary collapse
-
#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.
-
#prepare!
Serialize the request body.
Methods inherited from HttpCommand
#apply_request_options, #authorization_refreshable?, #error, #execute, #initialize, #process_response, #success
Methods included from Logging
Constructor Details
This class inherits a constructor from Google::Apis::Core::HttpCommand
Instance Attribute Details
#request_object ⇒ Object
Request body to serialize
37 38 39 |
# File 'lib/google/apis/core/api_command.rb', line 37 def request_object @request_object end |
#request_representation ⇒ Google::Apis::Core::JsonRepresentation
JSON serializer for request objects
33 34 35 |
# File 'lib/google/apis/core/api_command.rb', line 33 def request_representation @request_representation end |
#response_class ⇒ Object
Class to instantiate when de-serializing responses
45 46 47 |
# File 'lib/google/apis/core/api_command.rb', line 45 def response_class @response_class end |
#response_representation ⇒ Google::Apis::Core::JsonRepresentation
JSON serializer for response objects
41 42 43 |
# File 'lib/google/apis/core/api_command.rb', line 41 def response_representation @response_representation end |
Instance Method Details
#allow_form_encoding? ⇒ Boolean
108 109 110 |
# File 'lib/google/apis/core/api_command.rb', line 108 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
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/google/apis/core/api_command.rb', line 91 def check_status(status, header = nil, body = nil, = nil) case status when 400, 402...500 error = parse_error(body) if error = sprintf('%s: %s', error['reason'], error['message']) raise Google::Apis::RateLimitError.new(, status_code: status, header: header, body: body) if RATE_LIMIT_ERRORS.include?(error['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
68 69 70 71 72 73 74 75 |
# File 'lib/google/apis/core/api_command.rb', line 68 def decode_response_body(content_type, body) return super unless response_representation return super if content_type.nil? return nil unless content_type.start_with?(JSON_CONTENT_TYPE) 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
50 51 52 53 54 55 56 57 |
# File 'lib/google/apis/core/api_command.rb', line 50 def prepare! 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 self.body = request_representation.new(request_object).to_json(skip_undefined: true) end super end |