Skip to content

Commit

Permalink
Fix Parsing of HTTPX::ErrorResponse and Add Hooks for Advanced Logging
Browse files Browse the repository at this point in the history
There are usecases when users of the SDK need to perform custom logic before the request is sent or response is parsed, so they can add context to exception monitoring platforms.

This change also adds the following two hooks:
- `Client#before_submitting_request`
- `Client#before_parsing_response`
  • Loading branch information
obahareth committed Dec 27, 2023
1 parent d3502d5 commit ff33f2f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
24 changes: 19 additions & 5 deletions lib/zatca/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# This wraps the API described here:
# https://sandbox.zatca.gov.sa/IntegrationSandbox
class ZATCA::Client
attr_accessor :before_submitting_request, :before_parsing_response

# API URLs are not present in developer portal, they can only be found in a PDF
# called Fatoora Portal User Manual, here:
# https://zatca.gov.sa/en/E-Invoicing/Introduction/Guidelines/Documents/Fatoora%20portal%20user%20manual.pdf
Expand All @@ -20,7 +22,16 @@ class ZATCA::Client
DEFAULT_API_VERSION = "V2".freeze
LANGUAGES = %w[ar en].freeze

def initialize(username:, password:, language: "ar", version: DEFAULT_API_VERSION, environment: :production, verbose: false)
def initialize(
username:,
password:,
language: "ar",
version: DEFAULT_API_VERSION,
environment: :production,
verbose: false,
before_submitting_request: nil,
before_parsing_response: nil
)
raise "Invalid language: #{language}, Please use one of: #{LANGUAGES}" unless LANGUAGES.include?(language)

@username = username
Expand All @@ -30,6 +41,9 @@ def initialize(username:, password:, language: "ar", version: DEFAULT_API_VERSIO
@verbose = verbose

@base_url = ENVIRONMENTS_TO_URLS_MAP[environment.to_sym] || PRODUCTION_BASE_URL

@before_submitting_request = before_submitting_request
@before_parsing_response = before_parsing_response
end

# Reporting API
Expand Down Expand Up @@ -133,6 +147,7 @@ def request(method:, path:, body: {}, headers: {}, authenticated: true)
url = "#{@base_url}/#{path}"
headers = default_headers.merge(headers)

before_submitting_request&.call(method, url, body, headers)
log("Requesting #{method} #{url} with\n\nbody: #{body}\n\nheaders: #{headers}\n")

client = if authenticated
Expand All @@ -142,14 +157,13 @@ def request(method:, path:, body: {}, headers: {}, authenticated: true)
end

response = client.send(method, url, json: body, headers: headers)
before_parsing_response&.call(response)
log("Raw response: #{response}")

if response.instance_of?(HTTPX::ErrorResponse)
return {
message: response.to_s,
response_headers: response.response&.headers&.to_s,
response_body: response.response&.body&.to_s,
status: response.response&.status
message: response.error&.message,
details: response.to_s
}
end

Expand Down
2 changes: 1 addition & 1 deletion lib/zatca/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module ZATCA
VERSION = "1.0.2"
VERSION = "1.1.0"
end

0 comments on commit ff33f2f

Please sign in to comment.