1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- require 'whatsapp_sdk'
- class Whatsapp::Client
- attr_reader :access_token, :client
- def initialize(access_token:)
- @access_token = access_token
- raise ArgumentError, __("The required parameter 'access_token' is missing.") if access_token.nil?
- @client = WhatsappSdk::Api::Client.new access_token
- end
- def log_request(response: nil)
- return if response.empty?
- Rails.logger.error "WhatsApp Client: remote response: #{response}"
- end
- def handle_error(response:)
- return if !response.error
- log_request response: response.raw_response
- message = response.error.message
- if response.error.respond_to?(:raw_data_response) &&
- (details = response.error.raw_data_response&.dig('error_data', 'details'))
- message = "#{message}: #{details}"
- end
- raise CloudAPIError.new(message, response.error)
- end
- class CloudAPIError < StandardError
- attr_reader :original_error
- def initialize(message, original_error)
- super(message)
- @original_error = original_error
- end
- def retryable?
- return true if !original_error
-
- recoverable_errors = [
- 130_472,
- 131_021,
- 131_026,
- 131_047,
- 131_051,
- 131_052,
- 131_053,
- ]
- recoverable_errors.include?(original_error.code)
- end
- end
- protected
- def with_tmpfile(prefix:, &)
- Tempfile.create(prefix, &)
- end
- end
|