|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module LightsparkClient |
| 4 | + class Client |
| 5 | + include LightsparkClient::Mutations::Invoices |
| 6 | + include LightsparkClient::Queries::Invoices |
| 7 | + include LightsparkClient::Queries::Transactions |
| 8 | + |
| 9 | + DEFAULT_API_URL = "https://api.lightspark.com/graphql/server/2023-09-13" |
| 10 | + DEFAULT_ERROR_MESSAGE = "An error occurred while processing the request" |
| 11 | + LIGHTSPARK_ERRORS = { |
| 12 | + "400" => "You may have malformed your GraphQL request (for example, forgot to include the query field in the payload)", |
| 13 | + "401" => "The token/token_id pair is not valid and we cannot authenticate your account in the request.", |
| 14 | + "402" => "Your account might be on hold because of billing issues, or you are trying to use a feature that is not in your plan.", |
| 15 | + "403" => "Your account might be on hold because of suspicious activity.", |
| 16 | + "429" => "Your account sent too many requests in a short period of time and was rate limited.", |
| 17 | + "5xx" => "The server is experiencing a problem. Please try again later." |
| 18 | + } |
| 19 | + LOGGER_TAG = "LightsparkClient" |
| 20 | + |
| 21 | + def initialize(client_id:, client_secret:, api_url: DEFAULT_API_URL, logger: nil) |
| 22 | + @client_id = client_id |
| 23 | + @client_secret = client_secret |
| 24 | + @api_url = api_url |
| 25 | + @logger = logger |
| 26 | + end |
| 27 | + |
| 28 | + private |
| 29 | + |
| 30 | + def request(payload) |
| 31 | + log("Requesting Lightspark API") |
| 32 | + log("Payload: #{payload}") |
| 33 | + |
| 34 | + response = Typhoeus.post( |
| 35 | + @api_url, |
| 36 | + body: payload.to_json, |
| 37 | + headers: request_headers |
| 38 | + ) |
| 39 | + |
| 40 | + handle_response(response) |
| 41 | + end |
| 42 | + |
| 43 | + def request_headers |
| 44 | + token = Base64.encode64("#{@client_id}:#{@client_secret}").gsub("\n", "") |
| 45 | + |
| 46 | + { |
| 47 | + "Content-Type" => "application/json", |
| 48 | + "Authorization" => "Basic #{token}", |
| 49 | + } |
| 50 | + end |
| 51 | + |
| 52 | + def log(message, level = :info) |
| 53 | + return unless @logger && looger.respond_to?(:tagged) && @logger.respond_to?(level) |
| 54 | + |
| 55 | + @logger.send(:tagged, LOGGER_TAG) { @logger.send(level, message) } |
| 56 | + end |
| 57 | + |
| 58 | + def handle_response(response) |
| 59 | + handle_status(response) |
| 60 | + |
| 61 | + response_body = parse_response_body(response) |
| 62 | + |
| 63 | + handle_errors(response_body) |
| 64 | + |
| 65 | + log("Request to Lightspark API was successful") |
| 66 | + |
| 67 | + response_body["data"] |
| 68 | + end |
| 69 | + |
| 70 | + def handle_status(response) |
| 71 | + return response if response.success? |
| 72 | + |
| 73 | + status = response.code |
| 74 | + status = "5xx" if status >= 500 |
| 75 | + |
| 76 | + message = LIGHTSPARK_ERRORS[status.to_s] |
| 77 | + message ||= DEFAULT_ERROR_MESSAGE |
| 78 | + |
| 79 | + log("Request failed with status: #{response.code}. Message: #{message}", :error) |
| 80 | + |
| 81 | + raise LightsparkClient::Errors::ClientError, message |
| 82 | + end |
| 83 | + |
| 84 | + def handle_errors(body) |
| 85 | + return if body["errors"].nil? || body["errors"].empty? |
| 86 | + |
| 87 | + message = body["errors"].map { |error| error["message"] }.join(", ") |
| 88 | + |
| 89 | + log("Request failed with errors: #{message}", :error) |
| 90 | + |
| 91 | + raise LightsparkClient::Errors::ClientError, message |
| 92 | + end |
| 93 | + |
| 94 | + def parse_response_body(response) |
| 95 | + JSON.parse(response.body) |
| 96 | + rescue StandardError |
| 97 | + raise LightsparkClient::Errors::ClientError, "An error occurred while parsing the response" |
| 98 | + end |
| 99 | + end |
| 100 | +end |
0 commit comments