lib/gitlab/request.rb in gitlab-3.2.0 vs lib/gitlab/request.rb in gitlab-3.3.0

- old
+ new

@@ -7,11 +7,11 @@ include HTTParty format :json headers 'Accept' => 'application/json' parser Proc.new { |body, _| parse(body) } - attr_accessor :private_token + attr_accessor :private_token, :endpoint # Converts the response body to an ObjectifiedHash. def self.parse(body) body = decode(body) @@ -32,27 +32,31 @@ raise Error::Parsing.new "The response is not a valid JSON" end end def get(path, options={}) + set_httparty_config(options) set_private_token_header(options) - validate self.class.get(path, options) + validate self.class.get(@endpoint + path, options) end def post(path, options={}) + set_httparty_config(options) set_private_token_header(options, path) - validate self.class.post(path, options) + validate self.class.post(@endpoint + path, options) end def put(path, options={}) + set_httparty_config(options) set_private_token_header(options) - validate self.class.put(path, options) + validate self.class.put(@endpoint + path, options) end def delete(path, options={}) + set_httparty_config(options) set_private_token_header(options) - validate self.class.delete(path, options) + validate self.class.delete(@endpoint + path, options) end # Checks the response code for common errors. # Returns parsed response for successful requests. def validate(response) @@ -74,12 +78,12 @@ # Sets a base_uri and default_params for requests. # @raise [Error::MissingCredentials] if endpoint not set. def set_request_defaults(endpoint, private_token, sudo=nil) raise Error::MissingCredentials.new("Please set an endpoint to API") unless endpoint @private_token = private_token + @endpoint = endpoint - self.class.base_uri endpoint self.class.default_params :sudo => sudo self.class.default_params.delete(:sudo) if sudo.nil? end private @@ -88,9 +92,17 @@ # @raise [Error::MissingCredentials] if private_token not set. def set_private_token_header(options, path=nil) unless path == '/session' raise Error::MissingCredentials.new("Please set a private_token for user") unless @private_token options[:headers] = {'PRIVATE-TOKEN' => @private_token} + end + end + + # Set HTTParty configuration + # @see https://github.com/jnunemaker/httparty + def set_httparty_config(options) + if self.httparty + options.merge!(self.httparty) end end def error_message(response) "Server responded with code #{response.code}, message: #{response.parsed_response.message}. " \