lib/olaf/service_client.rb
#
# We're using RestClient under the hood, and this API obeys
# RestClient conventions.  Specifically:
#
# * for result code between 200 and 207 a response object will be
#   returned.
#
# * for result code 301, 302 or 307 the redirection will be
#   followed if the request is a get or a head.
#
# * for result code 303 the redirection will be followed and the
#   request transformed into a get.
#
# * for other cases an exception holding the response will be
#   raised.
#
# You can call .body or .to_s or .code on the response object.
#
# TODO: change this interface.  The RestClient interface may not
# be optimal for HTTP requests and is clearly a little odd for
# non-HTTP requests.  Either way, it should be basically the
# same for HTTP and non-HTTP.
#
We're using RestClient under the hood, and this API obeys
RestClient conventions.  Specifically.

for result code between 200 and 207 a response object will be
returned.

for result code 301, 302 or 307 the redirection will be
followed if the request is a get or a head.

for result code 303 the redirection will be followed and the
request transformed into a get.

for other cases an exception holding the response will be
raised.

You can call .body or .to_s or .code on the response object.

change this interface.  The RestClient interface may not
be optimal for HTTP requests and is clearly a little odd for
non-HTTP requests.  Either way, it should be basically the
same for HTTP and non-HTTP.
We ' re using RestClient under the hood and this API obeys RestClient conventions . Specifically . for result code between 200 and 207 a response object will be returned . for result code 301 302 or 307 the redirection will be followed if the request is a get or a head . for result code 303 the redirection will be followed and the request transformed into a get . for other cases an exception holding the response will be raised . You can call . body or . to_s or . code on the response object . change this interface . The RestClient interface may not be optimal for HTTP requests and is clearly a little odd for non - HTTP requests . Either way it should be basically the same for HTTP and non - HTTP .
def perform_request(verb, path, params = {},
                        context = { :headers => { :accept => ACCEPT }})
      unless HTTP_VERBS.include?(verb)
        raise "#{verb.inspect} is not one of #{HTTP_VERBS.inspect}!"
      end
      do_setup
      context = context.convert_keys_to_symbols  
      headers = collect_headers(context)
      path, params = normalize_path_and_params(path, params) if path
      if @resource[:url]
        request_result = http_request(verb, path, params, headers)
      else
        request_result = local_request(verb, path, params, context, headers)
      end
      body = request_result[:body]
      body = body.join("") if body.respond_to?(:each)
      got_error = request_result[:status] < 200 || request_result[:status] >= 400
      if got_error
        self.class.logger.error "ServiceClient got error: #{request_result[:status]}"
      end
      if context[:raise] && got_error
        error_hash = JSON.parse(body) rescue {}
        raise Olaf::Error.new  :http_response_code => request_result[:status],
          :error_code => error_hash.fetch('error_code', 1000),
          :request_guid => headers[Http::REQUEST_GUID_HEADER],
          :message => "ServiceClient request failed: #{verb} #{path} message #{error_hash.fetch('message', 'unknown')}"
      else
        json = JSON.parse(body) rescue nil
        body = json || body
      end
      if got_error
        return_data = nil
      else
        object = create_data_object_from_body(context, body)
        return_data = object ? object : body
      end
      return return_data if context[:raise]
      return return_data, request_result
    end
def perform_request ( verb , path , params = { } , context = { :headers => { :accept => ACCEPT } } ) unless HTTP_VERBS . include? ( verb ) raise "#{verb.inspect} is not one of #{HTTP_VERBS.inspect}!" end do_setup context = context . convert_keys_to_symbols headers = collect_headers ( context ) path , params = normalize_path_and_params ( path , params ) if path if @resource [ :url ] request_result = http_request ( verb , path , params , headers ) else request_result = local_request ( verb , path , params , context , headers ) end body = request_result [ :body ] body = body . join ( "" ) if body . respond_to? ( :each ) got_error = request_result [ :status ] < 200 || request_result [ :status ] >= 400 if got_error self . class . logger . error "ServiceClient got error: #{request_result[:status]}" end if context [ :raise ] && got_error error_hash = JSON . parse ( body ) rescue { } raise Olaf :: Error . new :http_response_code => request_result [ :status ] , :error_code => error_hash . fetch ( 'error_code' , 1000 ) , :request_guid => headers [ Http :: REQUEST_GUID_HEADER ] , :message => "ServiceClient request failed: #{verb} #{path} message #{error_hash.fetch('message', 'unknown')}" else json = JSON . parse ( body ) rescue nil body = json || body end if got_error return_data = nil else object = create_data_object_from_body ( context , body ) return_data = object ? object : body end return return_data if context [ :raise ] return return_data , request_result end
We're using RestClient under the hood, and this API obeys
RestClient conventions.
We ' re using RestClient under the hood and this API obeys RestClient conventions .
# recursively # If no service URL, perform the request locally # Note: we don't set reason field because we try to rethrow the original exception here # "Raise" doesn't just mean raise.  It means to return # only one parameter if there was no error.
verb
path
params
context