lib/paddle/client.rb in paddle-2.2.1 vs lib/paddle/client.rb in paddle-2.3.0

- old
+ new

@@ -1,67 +1,62 @@ +require "faraday" + module Paddle class Client class << self def connection - @connection ||= Faraday.new(Paddle.config.url) do |conn| - conn.request :authorization, :Bearer, Paddle.config.api_key - - conn.headers = { - "User-Agent" => "paddle/v#{VERSION} (github.com/deanpcmad/paddle)", - "Paddle-Version" => Paddle.config.version.to_s - } - - conn.request :json - conn.response :json - end + @connection ||= create_connection end - def get_request(url, params: {}, headers: {}) - handle_response connection.get(url, params, headers) + handle_response(connection.get(url, params, headers)) end def post_request(url, body: {}, headers: {}) - handle_response connection.post(url, body, headers) + handle_response(connection.post(url, body, headers)) end def patch_request(url, body:, headers: {}) - handle_response connection.patch(url, body, headers) + handle_response(connection.patch(url, body, headers)) end def delete_request(url, headers: {}) - handle_response connection.delete(url, headers) + handle_response(connection.delete(url, headers)) end - def handle_response(response) - case response.status - when 400 - raise Error, "Error 400: Your request was malformed. '#{response.body["error"]["code"]}'" - when 401 - raise Error, "Error 401: You did not supply valid authentication credentials. '#{response.body["error"]}'" - when 403 - raise Error, "Error 403: You are not allowed to perform that action. '#{response.body["error"]["code"]}'" - when 404 - raise Error, "Error 404: No results were found for your request. '#{response.body["error"]["code"]}'" - when 409 - raise Error, "Error 409: Your request was a conflict. '#{response.body["error"]["code"]}'" - when 429 - raise Error, "Error 429: Your request exceeded the API rate limit. '#{response.body["error"]["code"]}'" - when 500 - raise Error, "Error 500: We were unable to perform the request due to server-side problems. '#{response.body["error"]["code"]}'" - when 503 - raise Error, "Error 503: You have been rate limited for sending more than 20 requests per second. '#{response.body["error"]["code"]}'" - when 501 - raise Error, "Error 501: This resource has not been implemented. '#{response.body["error"]["code"]}'" - when 204 - return true - end + private - if response.body && response.body["error"] - raise Error, "Error #{response.body["error"]["code"]} - #{response.body["errors"]["message"]}" + def create_connection + Faraday.new(Paddle.config.url) do |conn| + conn.request :authorization, :Bearer, Paddle.config.api_key + conn.headers = default_headers + conn.request :json + conn.response :json end + end - response + def default_headers + { + "User-Agent" => "paddle/v#{VERSION} (github.com/deanpcmad/paddle)", + "Paddle-Version" => Paddle.config.version.to_s + } + end + + def handle_response(response) + return true if response.status == 204 + return response unless error?(response) + + raise_error(response) + end + + def error?(response) + [ 400, 401, 403, 404, 409, 429, 500, 501, 503 ].include?(response.status) || + response.body&.key?("error") + end + + def raise_error(response) + error = Paddle::ErrorFactory.create(response.body, response.status) + raise error if error end end end end