class Google::Apis::Core::ApiCommand
Command for executing most basic API request with JSON requests/responses
Constants
- FIELDS_PARAM
- JSON_CONTENT_TYPE
- RATE_LIMIT_ERRORS
Attributes
Request body to serialize @return [Object]
JSON serializer for request objects @return [Google::Apis::Core::JsonRepresentation]
Class to instantiate when de-serializing responses @return [Object]
JSON serializer for response objects @return [Google::Apis::Core::JsonRepresentation]
Public Instance Methods
Check the response and raise error if needed
@param [Fixnum] status
HTTP status code of response
@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/api_command.rb, line 91 def check_status(status, header = nil, body = nil, message = nil) case status when 400, 402...500 error = parse_error(body) if error message = sprintf('%s: %s', error['reason'], error['message']) raise Google::Apis::RateLimitError.new(message, status_code: status, header: header, body: body) if RATE_LIMIT_ERRORS.include?(error['reason']) end super(status, header, body, message) else super(status, header, body, message) end end
Deserialize the response body if present
@param [String] content_type
Content type of body
@param [String, read] body
Response body
@return [Object]
Response object
noinspection RubyUnusedLocalVariable
# 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
Serialize the request body
@return [void]
# 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
Private Instance Methods
Convert field names from ruby conventions to original names in JSON
@param [String] fields
Value of 'fields' param
@return [String]
Updated header value
# File lib/google/apis/core/api_command.rb, line 127 def normalize_fields_param(fields) # TODO: Generate map of parameter names during code gen. Small possibility that camelization fails fields.gsub(/:/, '').gsub(/\w+/) do |str| str.gsub(/(?:^|_)([a-z])/){ Regexp.last_match.begin(0) == 0 ? $1 : $1.upcase } end end
Attempt to parse a JSON error message, returning the first found error @param [String] body
HTTP response body
@return [Hash]
# File lib/google/apis/core/api_command.rb, line 114 def parse_error(body) hash = JSON.load(body) hash['error']['errors'].first rescue nil end