Class: ComplyanceSDK::HTTP::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/complyance_sdk/http/client.rb

Overview

HTTP client for making requests to the Complyance API

Constant Summary collapse

DEFAULT_TIMEOUT =

Default timeout in seconds

30

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Initialize a new HTTP client

Parameters:



19
20
21
22
# File 'lib/complyance_sdk/http/client.rb', line 19

def initialize(config)
  @config = config
  @connection = build_connection
end

Instance Method Details

#get(path, params = {}, headers = {}) ⇒ Hash

Make a GET request to the API

Parameters:

  • path (String)

    The API path

  • params (Hash) (defaults to: {})

    Query parameters

  • headers (Hash) (defaults to: {})

    Additional headers

Returns:

  • (Hash)

    The response data



72
73
74
75
76
77
78
79
80
81
# File 'lib/complyance_sdk/http/client.rb', line 72

def get(path, params = {}, headers = {})
  response = @connection.get(path) do |req|
    req.headers.merge!(headers)
    req.params.merge!(params)
  end

  handle_response(response)
rescue Faraday::Error => e
  handle_faraday_error(e)
end

#post(path, payload = {}, headers = {}) ⇒ Hash

Make a POST request to the API

Parameters:

  • path (String)

    The API path

  • payload (Hash) (defaults to: {})

    The request payload

  • headers (Hash) (defaults to: {})

    Additional headers

Returns:

  • (Hash)

    The response data



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/complyance_sdk/http/client.rb', line 30

def post(path, payload = {}, headers = {})
  # Get the full URL
  base_url = ComplyanceSDK::Models::Environment.base_url(@config.environment)
  full_url = "#{base_url}#{path}"
  
  # Log request details
  puts "\n🌐 === API REQUEST ==="
  puts "šŸ“ URL: #{full_url}"
  puts "šŸ“‹ Headers: #{headers.merge('Content-Type' => 'application/json')}"
  puts "šŸ“¦ Payload:"
  puts JSON.pretty_generate(payload)
  puts "========================\n"
  
  response = @connection.post(path) do |req|
    req.headers.merge!(headers)
    req.headers["Content-Type"] = "application/json"
    req.body = payload.to_json
  end

  # Log response details
  puts "\nšŸ“„ === API RESPONSE ==="
  puts "šŸ“Š Status: #{response.status}"
  puts "šŸ“‹ Headers: #{response.headers}"
  puts "šŸ“¦ Body:"
  puts JSON.pretty_generate(response.body) if response.body
  puts "========================\n"

  handle_response(response)
rescue Faraday::Error => e
  puts "\nāŒ === API ERROR ==="
  puts "🚨 Error: #{e.class.name}"
  puts "šŸ“ Message: #{e.message}"
  puts "===================\n"
  handle_faraday_error(e)
end