lib/gitlab/request.rb in gitlab-2.2.0 vs lib/gitlab/request.rb in gitlab-3.0.0

- old
+ new

@@ -2,36 +2,32 @@ module Gitlab # @private class Request include HTTParty - format :json + format :json headers 'Accept' => 'application/json' - parser Proc.new {|body| parse(body)} + parser Proc.new { |body, _| parse(body) } # 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)} + body.collect! { |e| ObjectifiedHash.new(e) } else raise Error::Parsing.new "Couldn't parse a response body" end end # Decodes a JSON response into Ruby object. def self.decode(response) begin - if MultiJson.respond_to?(:adapter) - MultiJson.load response - else - MultiJson.decode response - end - rescue MultiJson::DecodeError + JSON.load response + rescue JSON::ParserError raise Error::Parsing.new "The response is not a valid JSON" end end def get(path, options={}) @@ -51,30 +47,39 @@ end # Checks the response code for common errors. # Returns parsed response for successful requests. def validate(response) - message = "Server responsed with code #{response.code}" case response.code - when 400; raise Error::BadRequest.new message - when 401; raise Error::Unauthorized.new message - when 403; raise Error::Forbidden.new message - when 404; raise Error::NotFound.new message - when 500; raise Error::InternalServerError.new message - when 502; raise Error::BadGateway.new message - when 503; raise Error::ServiceUnavailable.new message + 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 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) end response.parsed_response end - # Sets a base_uri and private_token parameter for requests. + # Sets a base_uri and default_params for requests. # @raise [Error::MissingCredentials] if endpoint or private_token not set. - def set_request_defaults(endpoint, private_token) + def set_request_defaults(endpoint, private_token, sudo=nil) raise Error::MissingCredentials.new("Please set an endpoint to API") unless endpoint raise Error::MissingCredentials.new("Please set a private_token for user") unless private_token self.class.base_uri endpoint - self.class.default_params :private_token => private_token + self.class.default_params :private_token => private_token, :sudo => sudo + self.class.default_params.delete(:sudo) if sudo.nil? + end + + private + + def error_message(response) + "Server responded with code #{response.code}, message: #{response.parsed_response.message}. " \ + "Request URI: #{response.request.base_uri}#{response.request.path}" end end end