lib/gitlab/request.rb in gitlab-3.5.0 vs lib/gitlab/request.rb in gitlab-3.6.0

- old
+ new

@@ -5,22 +5,22 @@ # @private class Request include HTTParty format :json headers 'Accept' => 'application/json' - parser Proc.new { |body, _| parse(body) } + parser proc { |body, _| parse(body) } attr_accessor :private_token, :endpoint # Converts the response body to an ObjectifiedHash. def self.parse(body) body = decode(body) if body.is_a? Hash ObjectifiedHash.new body elsif body.is_a? Array - body.collect! { |e| ObjectifiedHash.new(e) } + PaginatedResponse.new(body.collect! { |e| ObjectifiedHash.new(e) }) elsif body true elsif !body false elsif body.nil? @@ -30,15 +30,13 @@ end end # Decodes a JSON response into Ruby object. def self.decode(response) - begin - JSON.load response - rescue JSON::ParserError - raise Error::Parsing.new "The response is not a valid JSON" - end + JSON.load response + rescue JSON::ParserError + raise Error::Parsing.new "The response is not a valid JSON" end def get(path, options={}) set_httparty_config(options) set_authorization_header(options) @@ -65,30 +63,33 @@ # Checks the response code for common errors. # Returns parsed response for successful requests. def validate(response) case response.code - when 400; raise Error::BadRequest.new error_message(response) - when 401; raise Error::Unauthorized.new error_message(response) - when 403; raise Error::Forbidden.new error_message(response) - when 404; raise Error::NotFound.new error_message(response) - when 405; raise Error::MethodNotAllowed.new error_message(response) - when 409; raise Error::Conflict.new error_message(response) - when 422; raise Error::Unprocessable.new error_message(response) - when 500; raise Error::InternalServerError.new error_message(response) - when 502; raise Error::BadGateway.new error_message(response) - when 503; raise Error::ServiceUnavailable.new error_message(response) + when 400 then fail Error::BadRequest.new error_message(response) + when 401 then fail Error::Unauthorized.new error_message(response) + when 403 then fail Error::Forbidden.new error_message(response) + when 404 then fail Error::NotFound.new error_message(response) + when 405 then fail Error::MethodNotAllowed.new error_message(response) + when 409 then fail Error::Conflict.new error_message(response) + when 422 then fail Error::Unprocessable.new error_message(response) + when 500 then fail Error::InternalServerError.new error_message(response) + when 502 then fail Error::BadGateway.new error_message(response) + when 503 then fail Error::ServiceUnavailable.new error_message(response) end - response.parsed_response + parsed = response.parsed_response + parsed.client = self if parsed.respond_to?(:client=) + parsed.parse_headers!(response.headers) if parsed.respond_to?(:parse_headers!) + parsed end # Sets a base_uri and default_params for requests. # @raise [Error::MissingCredentials] if endpoint not set. def set_request_defaults(sudo=nil) + self.class.default_params sudo: sudo raise Error::MissingCredentials.new("Please set an endpoint to API") unless @endpoint - self.class.default_params :sudo => sudo self.class.default_params.delete(:sudo) if sudo.nil? end private @@ -96,23 +97,21 @@ # @raise [Error::MissingCredentials] if private_token and auth_token are not set. def set_authorization_header(options, path=nil) unless path == '/session' raise Error::MissingCredentials.new("Please provide a private_token or auth_token for user") unless @private_token if @private_token.length <= 20 - options[:headers] = {'PRIVATE-TOKEN' => @private_token} + options[:headers] = { 'PRIVATE-TOKEN' => @private_token } else - options[:headers] = {'Authorization' => "Bearer #{@private_token}"} + options[:headers] = { 'Authorization' => "Bearer #{@private_token}" } end end end # Set HTTParty configuration # @see https://github.com/jnunemaker/httparty def set_httparty_config(options) - if self.httparty - options.merge!(self.httparty) - end + options.merge!(httparty) if httparty end def error_message(response) parsed_response = response.parsed_response message = parsed_response.message || parsed_response.error @@ -125,16 +124,15 @@ # Handle error response message in case of nested hashes def handle_error(message) case message when Gitlab::ObjectifiedHash message.to_h.sort.map do |key, val| - "'#{key}' #{(val.is_a?(Hash) ? val.sort.map { |k,v| "(#{k}: #{v.join(' ')})"} : val).join(' ')}" + "'#{key}' #{(val.is_a?(Hash) ? val.sort.map { |k, v| "(#{k}: #{v.join(' ')})" } : val).join(' ')}" end.join(', ') when Array message.join(' ') else message end end - end end