lib/spark_api/request.rb in spark_api-1.1.2 vs lib/spark_api/request.rb in spark_api-1.2.0

- old
+ new

@@ -22,11 +22,11 @@ # * options - Resource request options as specified being supported via and api resource # :returns: # Hash of the json results as documented in the api. # :raises: # SparkApi::ClientError or subclass if the request failed. - def post(path, body={}, options={}) + def post(path, body = nil, options={}) request(:post, path, body, options) end # Perform an HTTP PUT request # @@ -35,11 +35,11 @@ # * options - Resource request options as specified being supported via and api resource # :returns: # Hash of the json results as documented in the api. # :raises: # SparkApi::ClientError or subclass if the request failed. - def put(path, body={}, options={}) + def put(path, body = nil, options={}) request(:put, path, body, options) end # Perform an HTTP DELETE request # @@ -62,17 +62,17 @@ end attempts = 0 begin request_opts = {} request_opts.merge!(options) - post_data = body.nil? ? nil : {"D" => body }.to_json request_path = "/#{version}#{path}" start_time = Time.now SparkApi.logger.debug("#{method.to_s.upcase} Request: #{request_path}") - if post_data.nil? + if [:get, :delete, :head].include?(method.to_sym) response = authenticator.request(method, request_path, nil, request_opts) else + post_data = process_request_body(body) SparkApi.logger.debug("#{method.to_s.upcase} Data: #{post_data}") response = authenticator.request(method, request_path, post_data, request_opts) end request_time = Time.now - start_time SparkApi.logger.info("[#{(request_time * 1000).to_i}ms] Api: #{method.to_s.upcase} #{request_path}") @@ -87,9 +87,22 @@ # No luck authenticating... KABOOM! SparkApi.logger.error("Authentication failed or server is sending us expired tokens, nothing we can do here.") raise end response.body + rescue Faraday::Error::ConnectionFailed => e + if self.ssl_verify && e.message =~ /certificate verify failed/ + SparkApi.logger.error(SparkApi::Errors.ssl_verification_error) + end + raise e + end + + def process_request_body(body) + if body.is_a?(Hash) + body.empty? ? "{}" : {"D" => body }.to_json + else + body + end end end end