lib/kentaa/api/request.rb in kentaa-api-0.4.0 vs lib/kentaa/api/request.rb in kentaa-api-0.5.0

- old
+ new

@@ -1,7 +1,8 @@ # frozen_string_literal: true +require 'logger' require 'net/http' require 'uri' module Kentaa module Api @@ -32,10 +33,12 @@ def request(http_method, path, params = {}, body = {}) uri = URI.parse(File.join(config.api_url, path)) uri.query = URI.encode_www_form(params) unless params.empty? + logger.debug("[Kentaa-API] Request: #{http_method.upcase} #{uri}") if config.debug? + case http_method when :get request = Net::HTTP::Get.new(uri) when :post request = Net::HTTP::Post.new(uri) @@ -65,14 +68,20 @@ IOError, SocketError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::OpenTimeout, Net::ProtocolError, Net::ReadTimeout, OpenSSL::SSL::SSLError => e raise Kentaa::Api::Exception, e.message end + logger.debug("[Kentaa-API] Response: #{response.http_code}, body: #{response.body}") if config.debug? + if response.error? raise Kentaa::Api::RequestError, response end response + end + + def logger + @logger ||= Logger.new($stdout) end end end end