Class: WhatsAppCloudApi::Client
- Inherits:
-
Object
- Object
- WhatsAppCloudApi::Client
- Defined in:
- lib/whatsapp_cloud_api/client.rb
Constant Summary collapse
- DEFAULT_BASE_URL =
'https://graph.facebook.com'- DEFAULT_GRAPH_VERSION =
'v24.0'- KAPSO_PROXY_PATTERN =
/kapso\.ai/
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#debug ⇒ Object
readonly
Returns the value of attribute debug.
-
#graph_version ⇒ Object
readonly
Returns the value of attribute graph_version.
-
#kapso_api_key ⇒ Object
readonly
Returns the value of attribute kapso_api_key.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#max_retries ⇒ Object
readonly
Returns the value of attribute max_retries.
-
#open_timeout ⇒ Object
readonly
Returns the value of attribute open_timeout.
-
#retry_delay ⇒ Object
readonly
Returns the value of attribute retry_delay.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Instance Method Summary collapse
- #calls ⇒ Object
- #contacts ⇒ Object
- #conversations ⇒ Object
-
#fetch(url, options = {}) ⇒ Object
Fetch with automatic auth headers (for absolute URLs).
-
#initialize(access_token: nil, kapso_api_key: nil, base_url: nil, graph_version: nil, logger: nil, debug: nil, timeout: nil, open_timeout: nil, max_retries: nil, retry_delay: nil) ⇒ Client
constructor
A new instance of Client.
- #kapso_proxy? ⇒ Boolean
- #media ⇒ Object
-
#messages ⇒ Object
Resource accessors with lazy initialization.
- #phone_numbers ⇒ Object
-
#raw_request(method, url, options = {}) ⇒ Object
Raw HTTP method without automatic error handling (for media downloads, etc.).
-
#request(method, path, options = {}) ⇒ Object
Main request method with retry logic and error handling.
- #templates ⇒ Object
Constructor Details
#initialize(access_token: nil, kapso_api_key: nil, base_url: nil, graph_version: nil, logger: nil, debug: nil, timeout: nil, open_timeout: nil, max_retries: nil, retry_delay: nil) ⇒ Client
Returns a new instance of Client.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/whatsapp_cloud_api/client.rb', line 17 def initialize(access_token: nil, kapso_api_key: nil, base_url: nil, graph_version: nil, logger: nil, debug: nil, timeout: nil, open_timeout: nil, max_retries: nil, retry_delay: nil) # Validation unless access_token || kapso_api_key raise Errors::ConfigurationError, 'Must provide either access_token or kapso_api_key' end @access_token = access_token @kapso_api_key = kapso_api_key @base_url = normalize_base_url(base_url || DEFAULT_BASE_URL) @graph_version = graph_version || DEFAULT_GRAPH_VERSION @kapso_proxy = detect_kapso_proxy(@base_url) # Configuration with defaults config = WhatsAppCloudApi.configuration @logger = logger || WhatsAppCloudApi.logger @debug = debug.nil? ? config.debug : debug @timeout = timeout || config.timeout @open_timeout = open_timeout || config.open_timeout @max_retries = max_retries || config.max_retries @retry_delay = retry_delay || config.retry_delay # Initialize HTTP client @http_client = build_http_client # Initialize resource endpoints @messages = nil @media = nil @templates = nil @phone_numbers = nil @calls = nil @conversations = nil @contacts = nil end |
Instance Attribute Details
#access_token ⇒ Object (readonly)
Returns the value of attribute access_token.
14 15 16 |
# File 'lib/whatsapp_cloud_api/client.rb', line 14 def access_token @access_token end |
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
14 15 16 |
# File 'lib/whatsapp_cloud_api/client.rb', line 14 def base_url @base_url end |
#debug ⇒ Object (readonly)
Returns the value of attribute debug.
14 15 16 |
# File 'lib/whatsapp_cloud_api/client.rb', line 14 def debug @debug end |
#graph_version ⇒ Object (readonly)
Returns the value of attribute graph_version.
14 15 16 |
# File 'lib/whatsapp_cloud_api/client.rb', line 14 def graph_version @graph_version end |
#kapso_api_key ⇒ Object (readonly)
Returns the value of attribute kapso_api_key.
14 15 16 |
# File 'lib/whatsapp_cloud_api/client.rb', line 14 def kapso_api_key @kapso_api_key end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
14 15 16 |
# File 'lib/whatsapp_cloud_api/client.rb', line 14 def logger @logger end |
#max_retries ⇒ Object (readonly)
Returns the value of attribute max_retries.
14 15 16 |
# File 'lib/whatsapp_cloud_api/client.rb', line 14 def max_retries @max_retries end |
#open_timeout ⇒ Object (readonly)
Returns the value of attribute open_timeout.
14 15 16 |
# File 'lib/whatsapp_cloud_api/client.rb', line 14 def open_timeout @open_timeout end |
#retry_delay ⇒ Object (readonly)
Returns the value of attribute retry_delay.
14 15 16 |
# File 'lib/whatsapp_cloud_api/client.rb', line 14 def retry_delay @retry_delay end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
14 15 16 |
# File 'lib/whatsapp_cloud_api/client.rb', line 14 def timeout @timeout end |
Instance Method Details
#calls ⇒ Object
71 72 73 |
# File 'lib/whatsapp_cloud_api/client.rb', line 71 def calls @calls ||= Resources::Calls.new(self) end |
#contacts ⇒ Object
79 80 81 |
# File 'lib/whatsapp_cloud_api/client.rb', line 79 def contacts @contacts ||= Resources::Contacts.new(self) end |
#conversations ⇒ Object
75 76 77 |
# File 'lib/whatsapp_cloud_api/client.rb', line 75 def conversations @conversations ||= Resources::Conversations.new(self) end |
#fetch(url, options = {}) ⇒ Object
Fetch with automatic auth headers (for absolute URLs)
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/whatsapp_cloud_api/client.rb', line 139 def fetch(url, = {}) headers = build_headers([:headers] || {}) method = [:method] || 'GET' log_request(method, url, headers, [:body]) if debug response = @http_client.run_request(method.downcase.to_sym, url, [:body], headers) log_response(response) if debug if response.success? response else handle_error_response(response) end end |
#kapso_proxy? ⇒ Boolean
83 84 85 |
# File 'lib/whatsapp_cloud_api/client.rb', line 83 def kapso_proxy? @kapso_proxy end |
#media ⇒ Object
59 60 61 |
# File 'lib/whatsapp_cloud_api/client.rb', line 59 def media @media ||= Resources::Media.new(self) end |
#messages ⇒ Object
Resource accessors with lazy initialization
55 56 57 |
# File 'lib/whatsapp_cloud_api/client.rb', line 55 def @messages ||= Resources::Messages.new(self) end |
#phone_numbers ⇒ Object
67 68 69 |
# File 'lib/whatsapp_cloud_api/client.rb', line 67 def phone_numbers @phone_numbers ||= Resources::PhoneNumbers.new(self) end |
#raw_request(method, url, options = {}) ⇒ Object
Raw HTTP method without automatic error handling (for media downloads, etc.)
126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/whatsapp_cloud_api/client.rb', line 126 def raw_request(method, url, = {}) headers = build_headers([:headers] || {}) log_request(method, url, headers, [:body]) if debug response = @http_client.run_request(method.to_sym, url, [:body], headers) log_response(response) if debug response end |
#request(method, path, options = {}) ⇒ Object
Main request method with retry logic and error handling
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/whatsapp_cloud_api/client.rb', line 88 def request(method, path, = {}) method = method.to_s.upcase body = [:body] query = [:query] custom_headers = [:headers] || {} response_type = [:response_type] || :auto url = build_url(path, query) headers = build_headers(custom_headers) # Log request if debugging log_request(method, url, headers, body) if debug retries = 0 begin response = @http_client.run_request(method.downcase.to_sym, url, body, headers) # Log response if debugging log_response(response) if debug # Handle response based on type requested handle_response(response, response_type) rescue Faraday::Error => e retries += 1 if retries <= max_retries && retryable_error?(e) sleep(retry_delay * retries) retry else raise Errors::GraphApiError.new( message: "Network error: #{e.}", http_status: 0, category: :server ) end end end |