Class: KapsoClientRuby::Client
- Inherits:
-
Object
- Object
- KapsoClientRuby::Client
- Defined in:
- lib/kapso_client_ruby/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).
- #flows ⇒ Object
-
#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 53 |
# File 'lib/kapso_client_ruby/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 = KapsoClientRuby.configuration @logger = logger || KapsoClientRuby.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 = nil @media = nil @templates = nil @phone_numbers = nil @calls = nil @conversations = nil @contacts = nil @flows = nil end |
Instance Attribute Details
#access_token ⇒ Object (readonly)
Returns the value of attribute access_token.
14 15 16 |
# File 'lib/kapso_client_ruby/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/kapso_client_ruby/client.rb', line 14 def base_url @base_url end |
#debug ⇒ Object (readonly)
Returns the value of attribute debug.
14 15 16 |
# File 'lib/kapso_client_ruby/client.rb', line 14 def debug @debug end |
#graph_version ⇒ Object (readonly)
Returns the value of attribute graph_version.
14 15 16 |
# File 'lib/kapso_client_ruby/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/kapso_client_ruby/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/kapso_client_ruby/client.rb', line 14 def logger @logger end |
#max_retries ⇒ Object (readonly)
Returns the value of attribute max_retries.
14 15 16 |
# File 'lib/kapso_client_ruby/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/kapso_client_ruby/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/kapso_client_ruby/client.rb', line 14 def retry_delay @retry_delay end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
14 15 16 |
# File 'lib/kapso_client_ruby/client.rb', line 14 def timeout @timeout end |
Instance Method Details
#calls ⇒ Object
72 73 74 |
# File 'lib/kapso_client_ruby/client.rb', line 72 def calls @calls ||= Resources::Calls.new(self) end |
#contacts ⇒ Object
80 81 82 |
# File 'lib/kapso_client_ruby/client.rb', line 80 def contacts @contacts ||= Resources::Contacts.new(self) end |
#conversations ⇒ Object
76 77 78 |
# File 'lib/kapso_client_ruby/client.rb', line 76 def conversations @conversations ||= Resources::Conversations.new(self) end |
#fetch(url, options = {}) ⇒ Object
Fetch with automatic auth headers (for absolute URLs)
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/kapso_client_ruby/client.rb', line 144 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 |
#flows ⇒ Object
84 85 86 |
# File 'lib/kapso_client_ruby/client.rb', line 84 def flows @flows ||= Resources::Flows.new(self) end |
#kapso_proxy? ⇒ Boolean
88 89 90 |
# File 'lib/kapso_client_ruby/client.rb', line 88 def kapso_proxy? @kapso_proxy end |
#media ⇒ Object
60 61 62 |
# File 'lib/kapso_client_ruby/client.rb', line 60 def media @media ||= Resources::Media.new(self) end |
#messages ⇒ Object
Resource accessors with lazy initialization
56 57 58 |
# File 'lib/kapso_client_ruby/client.rb', line 56 def ||= Resources::Messages.new(self) end |
#phone_numbers ⇒ Object
68 69 70 |
# File 'lib/kapso_client_ruby/client.rb', line 68 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.)
131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/kapso_client_ruby/client.rb', line 131 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
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 124 125 126 127 128 |
# File 'lib/kapso_client_ruby/client.rb', line 93 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.message}", http_status: 0, category: :server ) end end end |
#templates ⇒ Object
64 65 66 |
# File 'lib/kapso_client_ruby/client.rb', line 64 def templates @templates ||= Resources::Templates.new(self) end |