transferzero-sdk 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +9 -0
- data/LICENSE +7 -0
- data/README.md +286 -0
- data/Rakefile +10 -0
- data/docs/APILogsApi.md +141 -0
- data/docs/Account.md +19 -0
- data/docs/AccountDebitsApi.md +199 -0
- data/docs/AccountListResponse.md +19 -0
- data/docs/AccountMeta.md +17 -0
- data/docs/AccountResponse.md +19 -0
- data/docs/AccountValidationApi.md +70 -0
- data/docs/AccountValidationError.md +17 -0
- data/docs/AccountValidationRequest.md +25 -0
- data/docs/AccountValidationResponse.md +19 -0
- data/docs/AccountValidationResult.md +17 -0
- data/docs/AccountsApi.md +128 -0
- data/docs/ApiLog.md +33 -0
- data/docs/ApiLogListResponse.md +19 -0
- data/docs/ApiLogResponse.md +17 -0
- data/docs/Currency.md +35 -0
- data/docs/CurrencyExchange.md +37 -0
- data/docs/CurrencyExchangeListResponse.md +17 -0
- data/docs/CurrencyInfoApi.md +182 -0
- data/docs/CurrencyListResponse.md +17 -0
- data/docs/CurrencyOpposite.md +37 -0
- data/docs/Debit.md +35 -0
- data/docs/DebitListRequest.md +17 -0
- data/docs/DebitListResponse.md +19 -0
- data/docs/DebitRequest.md +17 -0
- data/docs/DebitRequestWrapper.md +17 -0
- data/docs/DebitResponse.md +17 -0
- data/docs/Document.md +35 -0
- data/docs/DocumentListResponse.md +19 -0
- data/docs/DocumentRequest.md +17 -0
- data/docs/DocumentResponse.md +17 -0
- data/docs/DocumentWebhook.md +21 -0
- data/docs/DocumentsApi.md +199 -0
- data/docs/ErrorStatus.md +19 -0
- data/docs/FieldDescription.md +19 -0
- data/docs/FieldSelectValidation.md +19 -0
- data/docs/FieldValidation.md +21 -0
- data/docs/LogsApi.md +141 -0
- data/docs/Pagination.md +25 -0
- data/docs/PaginationMeta.md +17 -0
- data/docs/PayinMethod.md +25 -0
- data/docs/PayinMethodDetails.md +21 -0
- data/docs/PayinMethodDetailsMobile.md +19 -0
- data/docs/PayinMethodDetailsNGNBank.md +17 -0
- data/docs/PayinMethodRequest.md +17 -0
- data/docs/PaymentMethod.md +27 -0
- data/docs/PaymentMethodListResponse.md +17 -0
- data/docs/PaymentMethodOpposite.md +29 -0
- data/docs/PaymentMethodsApi.md +124 -0
- data/docs/PayoutMethod.md +27 -0
- data/docs/PayoutMethodBankAccountTypeEnum.md +16 -0
- data/docs/PayoutMethodDetails.md +53 -0
- data/docs/PayoutMethodDetailsBalance.md +17 -0
- data/docs/PayoutMethodDetailsGHSBank.md +23 -0
- data/docs/PayoutMethodDetailsIBAN.md +25 -0
- data/docs/PayoutMethodDetailsMADCash.md +37 -0
- data/docs/PayoutMethodDetailsMobile.md +21 -0
- data/docs/PayoutMethodDetailsNGNBank.md +25 -0
- data/docs/PayoutMethodDetailsXOFMobile.md +23 -0
- data/docs/PayoutMethodGenderEnum.md +16 -0
- data/docs/PayoutMethodIdentityCardTypeEnum.md +16 -0
- data/docs/PayoutMethodListResponse.md +19 -0
- data/docs/PayoutMethodMobileProviderEnum.md +16 -0
- data/docs/PayoutMethodRequest.md +17 -0
- data/docs/PayoutMethodResponse.md +17 -0
- data/docs/PayoutMethodWebhook.md +21 -0
- data/docs/PayoutMethodsApi.md +334 -0
- data/docs/Recipient.md +55 -0
- data/docs/RecipientListResponse.md +19 -0
- data/docs/RecipientRequest.md +17 -0
- data/docs/RecipientResponse.md +17 -0
- data/docs/RecipientState.md +16 -0
- data/docs/RecipientWebhook.md +21 -0
- data/docs/RecipientsApi.md +212 -0
- data/docs/Sender.md +67 -0
- data/docs/SenderListResponse.md +19 -0
- data/docs/SenderRequest.md +17 -0
- data/docs/SenderResponse.md +19 -0
- data/docs/SenderResponseExisting.md +17 -0
- data/docs/SenderResponseMeta.md +17 -0
- data/docs/SenderState.md +16 -0
- data/docs/SenderWebhook.md +21 -0
- data/docs/SendersApi.md +330 -0
- data/docs/Transaction.md +47 -0
- data/docs/TransactionListResponse.md +19 -0
- data/docs/TransactionRequest.md +17 -0
- data/docs/TransactionResponse.md +19 -0
- data/docs/TransactionResponseExisting.md +19 -0
- data/docs/TransactionResponseMeta.md +17 -0
- data/docs/TransactionState.md +16 -0
- data/docs/TransactionTraits.md +19 -0
- data/docs/TransactionWebhook.md +21 -0
- data/docs/TransactionsApi.md +450 -0
- data/docs/ValidationErrorDescription.md +17 -0
- data/docs/Webhook.md +21 -0
- data/docs/WebhookDefinition.md +25 -0
- data/docs/WebhookDefinitionEventListResponse.md +17 -0
- data/docs/WebhookDefinitionListResponse.md +19 -0
- data/docs/WebhookDefinitionRequest.md +17 -0
- data/docs/WebhookDefinitionResponse.md +17 -0
- data/docs/WebhookLog.md +27 -0
- data/docs/WebhookLogListResponse.md +19 -0
- data/docs/WebhookLogMetadata.md +35 -0
- data/docs/WebhookLogMetadataRequest.md +21 -0
- data/docs/WebhookLogMetadataResponse.md +21 -0
- data/docs/WebhookLogResponse.md +17 -0
- data/docs/WebhooksApi.md +319 -0
- data/example/client.rb +473 -0
- data/git_push.sh +55 -0
- data/lib/transferzero-sdk.rb +146 -0
- data/lib/transferzero-sdk/api/account_debits_api.rb +187 -0
- data/lib/transferzero-sdk/api/account_validation_api.rb +79 -0
- data/lib/transferzero-sdk/api/accounts_api.rb +135 -0
- data/lib/transferzero-sdk/api/api_logs_api.rb +137 -0
- data/lib/transferzero-sdk/api/currency_info_api.rb +167 -0
- data/lib/transferzero-sdk/api/documents_api.rb +197 -0
- data/lib/transferzero-sdk/api/logs_api.rb +137 -0
- data/lib/transferzero-sdk/api/payment_methods_api.rb +119 -0
- data/lib/transferzero-sdk/api/payout_methods_api.rb +321 -0
- data/lib/transferzero-sdk/api/recipients_api.rb +221 -0
- data/lib/transferzero-sdk/api/senders_api.rb +322 -0
- data/lib/transferzero-sdk/api/transactions_api.rb +428 -0
- data/lib/transferzero-sdk/api/webhooks_api.rb +299 -0
- data/lib/transferzero-sdk/api_client.rb +455 -0
- data/lib/transferzero-sdk/api_error.rb +61 -0
- data/lib/transferzero-sdk/configuration.rb +166 -0
- data/lib/transferzero-sdk/models/account.rb +212 -0
- data/lib/transferzero-sdk/models/account_list_response.rb +212 -0
- data/lib/transferzero-sdk/models/account_meta.rb +202 -0
- data/lib/transferzero-sdk/models/account_response.rb +210 -0
- data/lib/transferzero-sdk/models/account_validation_error.rb +202 -0
- data/lib/transferzero-sdk/models/account_validation_request.rb +325 -0
- data/lib/transferzero-sdk/models/account_validation_response.rb +210 -0
- data/lib/transferzero-sdk/models/account_validation_result.rb +202 -0
- data/lib/transferzero-sdk/models/api_log.rb +287 -0
- data/lib/transferzero-sdk/models/api_log_list_response.rb +213 -0
- data/lib/transferzero-sdk/models/api_log_response.rb +201 -0
- data/lib/transferzero-sdk/models/currency.rb +292 -0
- data/lib/transferzero-sdk/models/currency_exchange.rb +311 -0
- data/lib/transferzero-sdk/models/currency_exchange_list_response.rb +204 -0
- data/lib/transferzero-sdk/models/currency_list_response.rb +204 -0
- data/lib/transferzero-sdk/models/currency_opposite.rb +309 -0
- data/lib/transferzero-sdk/models/debit.rb +309 -0
- data/lib/transferzero-sdk/models/debit_list_request.rb +203 -0
- data/lib/transferzero-sdk/models/debit_list_response.rb +212 -0
- data/lib/transferzero-sdk/models/debit_request.rb +201 -0
- data/lib/transferzero-sdk/models/debit_request_wrapper.rb +227 -0
- data/lib/transferzero-sdk/models/debit_response.rb +201 -0
- data/lib/transferzero-sdk/models/document.rb +335 -0
- data/lib/transferzero-sdk/models/document_list_response.rb +212 -0
- data/lib/transferzero-sdk/models/document_request.rb +201 -0
- data/lib/transferzero-sdk/models/document_response.rb +201 -0
- data/lib/transferzero-sdk/models/document_webhook.rb +243 -0
- data/lib/transferzero-sdk/models/error_status.rb +210 -0
- data/lib/transferzero-sdk/models/field_description.rb +245 -0
- data/lib/transferzero-sdk/models/field_select_validation.rb +212 -0
- data/lib/transferzero-sdk/models/field_validation.rb +221 -0
- data/lib/transferzero-sdk/models/pagination.rb +242 -0
- data/lib/transferzero-sdk/models/pagination_meta.rb +201 -0
- data/lib/transferzero-sdk/models/payin_method.rb +242 -0
- data/lib/transferzero-sdk/models/payin_method_details.rb +257 -0
- data/lib/transferzero-sdk/models/payin_method_details_mobile.rb +218 -0
- data/lib/transferzero-sdk/models/payin_method_details_ngn_bank.rb +208 -0
- data/lib/transferzero-sdk/models/payin_method_request.rb +201 -0
- data/lib/transferzero-sdk/models/payment_method.rb +257 -0
- data/lib/transferzero-sdk/models/payment_method_list_response.rb +203 -0
- data/lib/transferzero-sdk/models/payment_method_opposite.rb +275 -0
- data/lib/transferzero-sdk/models/payout_method.rb +255 -0
- data/lib/transferzero-sdk/models/payout_method_bank_account_type_enum.rb +36 -0
- data/lib/transferzero-sdk/models/payout_method_details.rb +462 -0
- data/lib/transferzero-sdk/models/payout_method_details_balance.rb +202 -0
- data/lib/transferzero-sdk/models/payout_method_details_ghs_bank.rb +249 -0
- data/lib/transferzero-sdk/models/payout_method_details_iban.rb +258 -0
- data/lib/transferzero-sdk/models/payout_method_details_mad_cash.rb +332 -0
- data/lib/transferzero-sdk/models/payout_method_details_mobile.rb +235 -0
- data/lib/transferzero-sdk/models/payout_method_details_ngn_bank.rb +263 -0
- data/lib/transferzero-sdk/models/payout_method_details_xof_mobile.rb +249 -0
- data/lib/transferzero-sdk/models/payout_method_gender_enum.rb +36 -0
- data/lib/transferzero-sdk/models/payout_method_identity_card_type_enum.rb +37 -0
- data/lib/transferzero-sdk/models/payout_method_list_response.rb +212 -0
- data/lib/transferzero-sdk/models/payout_method_mobile_provider_enum.rb +36 -0
- data/lib/transferzero-sdk/models/payout_method_request.rb +201 -0
- data/lib/transferzero-sdk/models/payout_method_response.rb +201 -0
- data/lib/transferzero-sdk/models/payout_method_webhook.rb +243 -0
- data/lib/transferzero-sdk/models/recipient.rb +406 -0
- data/lib/transferzero-sdk/models/recipient_list_response.rb +212 -0
- data/lib/transferzero-sdk/models/recipient_request.rb +201 -0
- data/lib/transferzero-sdk/models/recipient_response.rb +201 -0
- data/lib/transferzero-sdk/models/recipient_state.rb +44 -0
- data/lib/transferzero-sdk/models/recipient_webhook.rb +243 -0
- data/lib/transferzero-sdk/models/sender.rb +529 -0
- data/lib/transferzero-sdk/models/sender_list_response.rb +212 -0
- data/lib/transferzero-sdk/models/sender_request.rb +201 -0
- data/lib/transferzero-sdk/models/sender_response.rb +210 -0
- data/lib/transferzero-sdk/models/sender_response_existing.rb +201 -0
- data/lib/transferzero-sdk/models/sender_response_meta.rb +201 -0
- data/lib/transferzero-sdk/models/sender_state.rb +40 -0
- data/lib/transferzero-sdk/models/sender_webhook.rb +243 -0
- data/lib/transferzero-sdk/models/transaction.rb +370 -0
- data/lib/transferzero-sdk/models/transaction_list_response.rb +212 -0
- data/lib/transferzero-sdk/models/transaction_request.rb +201 -0
- data/lib/transferzero-sdk/models/transaction_response.rb +210 -0
- data/lib/transferzero-sdk/models/transaction_response_existing.rb +210 -0
- data/lib/transferzero-sdk/models/transaction_response_meta.rb +201 -0
- data/lib/transferzero-sdk/models/transaction_state.rb +44 -0
- data/lib/transferzero-sdk/models/transaction_traits.rb +213 -0
- data/lib/transferzero-sdk/models/transaction_webhook.rb +243 -0
- data/lib/transferzero-sdk/models/validation_error_description.rb +203 -0
- data/lib/transferzero-sdk/models/webhook.rb +238 -0
- data/lib/transferzero-sdk/models/webhook_definition.rb +256 -0
- data/lib/transferzero-sdk/models/webhook_definition_event_list_response.rb +203 -0
- data/lib/transferzero-sdk/models/webhook_definition_list_response.rb +212 -0
- data/lib/transferzero-sdk/models/webhook_definition_request.rb +201 -0
- data/lib/transferzero-sdk/models/webhook_definition_response.rb +201 -0
- data/lib/transferzero-sdk/models/webhook_log.rb +249 -0
- data/lib/transferzero-sdk/models/webhook_log_list_response.rb +213 -0
- data/lib/transferzero-sdk/models/webhook_log_metadata.rb +289 -0
- data/lib/transferzero-sdk/models/webhook_log_metadata_request.rb +224 -0
- data/lib/transferzero-sdk/models/webhook_log_metadata_response.rb +225 -0
- data/lib/transferzero-sdk/models/webhook_log_response.rb +201 -0
- data/lib/transferzero-sdk/version.rb +15 -0
- data/spec/api/account_debits_api_spec.rb +72 -0
- data/spec/api/account_validation_api_spec.rb +47 -0
- data/spec/api/accounts_api_spec.rb +58 -0
- data/spec/api/api_logs_api_spec.rb +62 -0
- data/spec/api/currency_info_api_spec.rb +68 -0
- data/spec/api/documents_api_spec.rb +72 -0
- data/spec/api/logs_api_spec.rb +62 -0
- data/spec/api/payment_methods_api_spec.rb +57 -0
- data/spec/api/payout_methods_api_spec.rb +102 -0
- data/spec/api/recipients_api_spec.rb +79 -0
- data/spec/api/senders_api_spec.rb +100 -0
- data/spec/api/transactions_api_spec.rb +122 -0
- data/spec/api/webhooks_api_spec.rb +95 -0
- data/spec/api_client_spec.rb +189 -0
- data/spec/configuration_spec.rb +42 -0
- data/spec/models/account_list_response_spec.rb +47 -0
- data/spec/models/account_meta_spec.rb +41 -0
- data/spec/models/account_response_spec.rb +47 -0
- data/spec/models/account_spec.rb +47 -0
- data/spec/models/account_validation_error_spec.rb +41 -0
- data/spec/models/account_validation_request_spec.rb +77 -0
- data/spec/models/account_validation_response_spec.rb +47 -0
- data/spec/models/account_validation_result_spec.rb +41 -0
- data/spec/models/api_log_list_response_spec.rb +47 -0
- data/spec/models/api_log_response_spec.rb +41 -0
- data/spec/models/api_log_spec.rb +89 -0
- data/spec/models/currency_exchange_list_response_spec.rb +41 -0
- data/spec/models/currency_exchange_spec.rb +101 -0
- data/spec/models/currency_list_response_spec.rb +41 -0
- data/spec/models/currency_opposite_spec.rb +101 -0
- data/spec/models/currency_spec.rb +95 -0
- data/spec/models/debit_list_request_spec.rb +41 -0
- data/spec/models/debit_list_response_spec.rb +47 -0
- data/spec/models/debit_request_spec.rb +41 -0
- data/spec/models/debit_request_wrapper_spec.rb +41 -0
- data/spec/models/debit_response_spec.rb +41 -0
- data/spec/models/debit_spec.rb +95 -0
- data/spec/models/document_list_response_spec.rb +47 -0
- data/spec/models/document_request_spec.rb +41 -0
- data/spec/models/document_response_spec.rb +41 -0
- data/spec/models/document_spec.rb +99 -0
- data/spec/models/document_webhook_spec.rb +53 -0
- data/spec/models/error_status_spec.rb +47 -0
- data/spec/models/field_description_spec.rb +51 -0
- data/spec/models/field_select_validation_spec.rb +47 -0
- data/spec/models/field_validation_spec.rb +53 -0
- data/spec/models/pagination_meta_spec.rb +41 -0
- data/spec/models/pagination_spec.rb +65 -0
- data/spec/models/payin_method_details_mobile_spec.rb +47 -0
- data/spec/models/payin_method_details_ngn_bank_spec.rb +41 -0
- data/spec/models/payin_method_details_spec.rb +53 -0
- data/spec/models/payin_method_request_spec.rb +41 -0
- data/spec/models/payin_method_spec.rb +65 -0
- data/spec/models/payment_method_list_response_spec.rb +41 -0
- data/spec/models/payment_method_opposite_spec.rb +77 -0
- data/spec/models/payment_method_spec.rb +71 -0
- data/spec/models/payout_method_bank_account_type_enum_spec.rb +35 -0
- data/spec/models/payout_method_details_balance_spec.rb +41 -0
- data/spec/models/payout_method_details_ghs_bank_spec.rb +59 -0
- data/spec/models/payout_method_details_iban_spec.rb +65 -0
- data/spec/models/payout_method_details_mad_cash_spec.rb +101 -0
- data/spec/models/payout_method_details_mobile_spec.rb +53 -0
- data/spec/models/payout_method_details_ngn_bank_spec.rb +65 -0
- data/spec/models/payout_method_details_spec.rb +149 -0
- data/spec/models/payout_method_details_xof_mobile_spec.rb +59 -0
- data/spec/models/payout_method_gender_enum_spec.rb +35 -0
- data/spec/models/payout_method_identity_card_type_enum_spec.rb +35 -0
- data/spec/models/payout_method_list_response_spec.rb +47 -0
- data/spec/models/payout_method_mobile_provider_enum_spec.rb +35 -0
- data/spec/models/payout_method_request_spec.rb +41 -0
- data/spec/models/payout_method_response_spec.rb +41 -0
- data/spec/models/payout_method_spec.rb +71 -0
- data/spec/models/payout_method_webhook_spec.rb +53 -0
- data/spec/models/recipient_list_response_spec.rb +47 -0
- data/spec/models/recipient_request_spec.rb +41 -0
- data/spec/models/recipient_response_spec.rb +41 -0
- data/spec/models/recipient_spec.rb +155 -0
- data/spec/models/recipient_state_spec.rb +35 -0
- data/spec/models/recipient_webhook_spec.rb +53 -0
- data/spec/models/sender_list_response_spec.rb +47 -0
- data/spec/models/sender_request_spec.rb +41 -0
- data/spec/models/sender_response_existing_spec.rb +41 -0
- data/spec/models/sender_response_meta_spec.rb +41 -0
- data/spec/models/sender_response_spec.rb +47 -0
- data/spec/models/sender_spec.rb +195 -0
- data/spec/models/sender_state_spec.rb +35 -0
- data/spec/models/sender_webhook_spec.rb +53 -0
- data/spec/models/transaction_list_response_spec.rb +47 -0
- data/spec/models/transaction_request_spec.rb +41 -0
- data/spec/models/transaction_response_existing_spec.rb +47 -0
- data/spec/models/transaction_response_meta_spec.rb +41 -0
- data/spec/models/transaction_response_spec.rb +47 -0
- data/spec/models/transaction_spec.rb +131 -0
- data/spec/models/transaction_state_spec.rb +35 -0
- data/spec/models/transaction_traits_spec.rb +47 -0
- data/spec/models/transaction_webhook_spec.rb +53 -0
- data/spec/models/validation_error_description_spec.rb +41 -0
- data/spec/models/webhook_definition_event_list_response_spec.rb +41 -0
- data/spec/models/webhook_definition_list_response_spec.rb +47 -0
- data/spec/models/webhook_definition_request_spec.rb +41 -0
- data/spec/models/webhook_definition_response_spec.rb +41 -0
- data/spec/models/webhook_definition_spec.rb +65 -0
- data/spec/models/webhook_log_list_response_spec.rb +47 -0
- data/spec/models/webhook_log_metadata_request_spec.rb +53 -0
- data/spec/models/webhook_log_metadata_response_spec.rb +53 -0
- data/spec/models/webhook_log_metadata_spec.rb +95 -0
- data/spec/models/webhook_log_response_spec.rb +41 -0
- data/spec/models/webhook_log_spec.rb +71 -0
- data/spec/models/webhook_spec.rb +53 -0
- data/spec/spec_helper.rb +111 -0
- data/transferzero-sdk.gemspec +46 -0
- metadata +684 -0
@@ -0,0 +1,455 @@
|
|
1
|
+
=begin
|
2
|
+
#TransferZero API
|
3
|
+
|
4
|
+
#Reference documentation for the TransferZero API V1
|
5
|
+
|
6
|
+
OpenAPI spec version: 1.0
|
7
|
+
|
8
|
+
Generated by: https://openapi-generator.tech
|
9
|
+
OpenAPI Generator version: 4.0.0-beta3
|
10
|
+
|
11
|
+
=end
|
12
|
+
|
13
|
+
require 'date'
|
14
|
+
require 'json'
|
15
|
+
require 'logger'
|
16
|
+
require 'tempfile'
|
17
|
+
require 'typhoeus'
|
18
|
+
require 'uri'
|
19
|
+
require 'openssl'
|
20
|
+
require 'securerandom'
|
21
|
+
|
22
|
+
module TransferZero
|
23
|
+
class ApiClient
|
24
|
+
DIGEST = OpenSSL::Digest::SHA512.new
|
25
|
+
|
26
|
+
# The Configuration object holding settings to be used in the API client.
|
27
|
+
attr_accessor :config
|
28
|
+
|
29
|
+
# Defines the headers to be used in HTTP requests of all API calls by default.
|
30
|
+
#
|
31
|
+
# @return [Hash]
|
32
|
+
attr_accessor :default_headers
|
33
|
+
|
34
|
+
# Initializes the ApiClient
|
35
|
+
# @option config [Configuration] Configuration for initializing the object, default to Configuration.default
|
36
|
+
def initialize(config = Configuration.default)
|
37
|
+
@config = config
|
38
|
+
@user_agent = "TransferZero-SDK/Ruby/1.0.0"
|
39
|
+
@default_headers = {
|
40
|
+
'Content-Type' => 'application/json',
|
41
|
+
'User-Agent' => @user_agent
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.default
|
46
|
+
@@default ||= ApiClient.new
|
47
|
+
end
|
48
|
+
|
49
|
+
# Call an API with given options.
|
50
|
+
#
|
51
|
+
# @return [Array<(Object, Fixnum, Hash)>] an array of 3 elements:
|
52
|
+
# the data deserialized from response body (could be nil), response status code and response headers.
|
53
|
+
def call_api(http_method, path, opts = {})
|
54
|
+
request = build_request(http_method, path, opts)
|
55
|
+
response = request.run
|
56
|
+
|
57
|
+
if @config.debugging
|
58
|
+
@config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
|
59
|
+
end
|
60
|
+
|
61
|
+
unless response.success?
|
62
|
+
if response.timed_out?
|
63
|
+
fail ApiError.new('Connection timed out')
|
64
|
+
elsif response.code == 0
|
65
|
+
# Errors from libcurl will be made visible here
|
66
|
+
fail ApiError.new(:code => 0,
|
67
|
+
:message => response.return_message)
|
68
|
+
elsif response.code == 422
|
69
|
+
fail ApiError.new(:code => response.code,
|
70
|
+
:response_headers => response.headers,
|
71
|
+
:response_body => response.body,
|
72
|
+
:validation_error => true
|
73
|
+
)
|
74
|
+
else
|
75
|
+
fail ApiError.new(:code => response.code,
|
76
|
+
:response_headers => response.headers,
|
77
|
+
:response_body => response.body,
|
78
|
+
:validation_error => false),
|
79
|
+
response.status_message
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
if opts[:return_type]
|
84
|
+
data = deserialize(response, opts[:return_type])
|
85
|
+
else
|
86
|
+
data = nil
|
87
|
+
end
|
88
|
+
return data, response.code, response.headers
|
89
|
+
end
|
90
|
+
|
91
|
+
# Builds the HTTP request
|
92
|
+
#
|
93
|
+
# @param [String] http_method HTTP method/verb (e.g. POST)
|
94
|
+
# @param [String] path URL path (e.g. /account/new)
|
95
|
+
# @option opts [Hash] :header_params Header parameters
|
96
|
+
# @option opts [Hash] :query_params Query parameters
|
97
|
+
# @option opts [Hash] :form_params Query parameters
|
98
|
+
# @option opts [Object] :body HTTP body (JSON/XML)
|
99
|
+
# @return [Typhoeus::Request] A Typhoeus Request
|
100
|
+
def build_request(http_method, path, opts = {})
|
101
|
+
url = build_request_url(path)
|
102
|
+
http_method = http_method.to_sym.downcase
|
103
|
+
|
104
|
+
header_params = @default_headers.merge(opts[:header_params] || {})
|
105
|
+
query_params = opts[:query_params] || {}
|
106
|
+
url_with_params = add_query(url, query_params)
|
107
|
+
form_params = opts[:form_params] || {}
|
108
|
+
|
109
|
+
request_nonce = SecureRandom.uuid
|
110
|
+
request_signature = sign_request([
|
111
|
+
request_nonce,
|
112
|
+
http_method.to_s.upcase,
|
113
|
+
url_with_params,
|
114
|
+
DIGEST.hexdigest(opts[:body].to_s.gsub(/[[:space:]]+/, ' ').strip)
|
115
|
+
])
|
116
|
+
|
117
|
+
header_params['Authorization-Nonce'] = request_nonce
|
118
|
+
header_params['Authorization-Signature'] = request_signature
|
119
|
+
header_params['Authorization-Key'] = @config.api_key
|
120
|
+
|
121
|
+
# set ssl_verifyhosts option based on @config.verify_ssl_host (true/false)
|
122
|
+
_verify_ssl_host = @config.verify_ssl_host ? 2 : 0
|
123
|
+
|
124
|
+
req_opts = {
|
125
|
+
:method => http_method,
|
126
|
+
:headers => header_params,
|
127
|
+
:params => {}, # removed and params added to url string
|
128
|
+
:params_encoding => @config.params_encoding,
|
129
|
+
:timeout => @config.timeout,
|
130
|
+
:ssl_verifypeer => @config.verify_ssl,
|
131
|
+
:ssl_verifyhost => _verify_ssl_host,
|
132
|
+
:sslcert => @config.cert_file,
|
133
|
+
:sslkey => @config.key_file,
|
134
|
+
:verbose => @config.debugging
|
135
|
+
}
|
136
|
+
|
137
|
+
# set custom cert, if provided
|
138
|
+
req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert
|
139
|
+
|
140
|
+
if [:post, :patch, :put, :delete].include?(http_method)
|
141
|
+
req_body = build_request_body(header_params, form_params, opts[:body])
|
142
|
+
req_opts.update :body => req_body
|
143
|
+
if @config.debugging
|
144
|
+
@config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
request = Typhoeus::Request.new(url_with_params, req_opts)
|
149
|
+
download_file(request) if opts[:return_type] == 'File'
|
150
|
+
request
|
151
|
+
end
|
152
|
+
|
153
|
+
# Check if the given MIME is a JSON MIME.
|
154
|
+
# JSON MIME examples:
|
155
|
+
# application/json
|
156
|
+
# application/json; charset=UTF8
|
157
|
+
# APPLICATION/JSON
|
158
|
+
# */*
|
159
|
+
# @param [String] mime MIME
|
160
|
+
# @return [Boolean] True if the MIME is application/json
|
161
|
+
def json_mime?(mime)
|
162
|
+
(mime == '*/*') || !(mime =~ /Application\/.*json(?!p)(;.*)?/i).nil?
|
163
|
+
end
|
164
|
+
|
165
|
+
# Parse a JSON string, can be used to parse webhook responses that were already converted to strings
|
166
|
+
#
|
167
|
+
# @param [Response] response HTTP response
|
168
|
+
# @param [String] return_type some examples: "User"
|
169
|
+
def parse_response(response, return_type)
|
170
|
+
return if (response.nil? || response.empty? || return_type.nil?)
|
171
|
+
begin
|
172
|
+
response_struct = OpenStruct.new({
|
173
|
+
headers: {},
|
174
|
+
body: response
|
175
|
+
})
|
176
|
+
deserialize(response_struct, return_type)
|
177
|
+
rescue StandardError => e
|
178
|
+
ApiError.new(e)
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
# Uses the Authorization Signature in the request headers to validate the payload
|
183
|
+
#
|
184
|
+
# @param url The full url including any query strings
|
185
|
+
# @param body The body from the request
|
186
|
+
# @param headers The request header
|
187
|
+
#
|
188
|
+
# @return boolean
|
189
|
+
#
|
190
|
+
|
191
|
+
def validate_webhook_request(url, body, headers)
|
192
|
+
nonce = headers[:'Authorization-Nonce'] || headers['Authorization-Nonce']
|
193
|
+
signature = headers[:'Authorization-Signature'] || headers['Authorization-Signature']
|
194
|
+
key = headers[:'Authorization-Key'] || headers['Authorization-Key']
|
195
|
+
|
196
|
+
return false if nonce.nil? || signature.nil? || (key != @config.api_key)
|
197
|
+
|
198
|
+
header_signature = sign_request([
|
199
|
+
nonce,
|
200
|
+
'POST',
|
201
|
+
url,
|
202
|
+
DIGEST.hexdigest(body.to_s.gsub(/[[:space:]]+/, ' ').strip)
|
203
|
+
])
|
204
|
+
|
205
|
+
header_signature == signature
|
206
|
+
end
|
207
|
+
|
208
|
+
# Deserialize the response to the given return type.
|
209
|
+
#
|
210
|
+
# @param [Response] response HTTP response
|
211
|
+
# @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]"
|
212
|
+
def deserialize(response, return_type)
|
213
|
+
body = response.body
|
214
|
+
|
215
|
+
# handle file downloading - return the File instance processed in request callbacks
|
216
|
+
# note that response body is empty when the file is written in chunks in request on_body callback
|
217
|
+
return @tempfile if return_type == 'File'
|
218
|
+
|
219
|
+
return nil if body.nil? || body.empty?
|
220
|
+
|
221
|
+
# return response body directly for String return type
|
222
|
+
return body if return_type == 'String'
|
223
|
+
|
224
|
+
# ensuring a default content type
|
225
|
+
content_type = response.headers['Content-Type'] || 'application/json'
|
226
|
+
|
227
|
+
fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
|
228
|
+
|
229
|
+
begin
|
230
|
+
data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
|
231
|
+
rescue JSON::ParserError => e
|
232
|
+
if %w(String Date DateTime).include?(return_type)
|
233
|
+
data = body
|
234
|
+
else
|
235
|
+
raise e
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
convert_to_type data, return_type
|
240
|
+
end
|
241
|
+
|
242
|
+
# Convert data to the given return type.
|
243
|
+
# @param [Object] data Data to be converted
|
244
|
+
# @param [String] return_type Return type
|
245
|
+
# @return [Mixed] Data in a particular type
|
246
|
+
def convert_to_type(data, return_type)
|
247
|
+
return nil if data.nil?
|
248
|
+
case return_type
|
249
|
+
when 'String'
|
250
|
+
data.to_s
|
251
|
+
when 'Integer'
|
252
|
+
data.to_i
|
253
|
+
when 'Float'
|
254
|
+
data.to_f
|
255
|
+
when 'Boolean'
|
256
|
+
data == true
|
257
|
+
when 'DateTime'
|
258
|
+
# parse date time (expecting ISO 8601 format)
|
259
|
+
DateTime.parse data
|
260
|
+
when 'Date'
|
261
|
+
# parse date time (expecting ISO 8601 format)
|
262
|
+
Date.parse data
|
263
|
+
when 'Object'
|
264
|
+
# generic object (usually a Hash), return directly
|
265
|
+
data
|
266
|
+
when /\AArray<(.+)>\z/
|
267
|
+
# e.g. Array<Pet>
|
268
|
+
sub_type = $1
|
269
|
+
data.map { |item| convert_to_type(item, sub_type) }
|
270
|
+
when /\AHash\<String, (.+)\>\z/
|
271
|
+
# e.g. Hash<String, Integer>
|
272
|
+
sub_type = $1
|
273
|
+
{}.tap do |hash|
|
274
|
+
data.each { |k, v| hash[k] = convert_to_type(v, sub_type) }
|
275
|
+
end
|
276
|
+
else
|
277
|
+
# models, e.g. Pet
|
278
|
+
TransferZero.const_get(return_type).new.tap do |model|
|
279
|
+
model.build_from_hash data
|
280
|
+
end
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
284
|
+
# Save response body into a file in (the defined) temporary folder, using the filename
|
285
|
+
# from the "Content-Disposition" header if provided, otherwise a random filename.
|
286
|
+
# The response body is written to the file in chunks in order to handle files which
|
287
|
+
# size is larger than maximum Ruby String or even larger than the maximum memory a Ruby
|
288
|
+
# process can use.
|
289
|
+
#
|
290
|
+
# @see Configuration#temp_folder_path
|
291
|
+
def download_file(request)
|
292
|
+
tempfile = nil
|
293
|
+
encoding = nil
|
294
|
+
request.on_headers do |response|
|
295
|
+
content_disposition = response.headers['Content-Disposition']
|
296
|
+
if content_disposition && content_disposition =~ /filename=/i
|
297
|
+
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
|
298
|
+
prefix = sanitize_filename(filename)
|
299
|
+
else
|
300
|
+
prefix = 'download-'
|
301
|
+
end
|
302
|
+
prefix = prefix + '-' unless prefix.end_with?('-')
|
303
|
+
encoding = response.body.encoding
|
304
|
+
tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
|
305
|
+
@tempfile = tempfile
|
306
|
+
end
|
307
|
+
request.on_body do |chunk|
|
308
|
+
chunk.force_encoding(encoding)
|
309
|
+
tempfile.write(chunk)
|
310
|
+
end
|
311
|
+
request.on_complete do |response|
|
312
|
+
tempfile.close
|
313
|
+
@config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
|
314
|
+
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
|
315
|
+
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
|
316
|
+
"explicitly with `tempfile.delete`"
|
317
|
+
end
|
318
|
+
end
|
319
|
+
|
320
|
+
# Sanitize filename by removing path.
|
321
|
+
# e.g. ../../sun.gif becomes sun.gif
|
322
|
+
#
|
323
|
+
# @param [String] filename the filename to be sanitized
|
324
|
+
# @return [String] the sanitized filename
|
325
|
+
def sanitize_filename(filename)
|
326
|
+
filename.gsub(/.*[\/\\]/, '')
|
327
|
+
end
|
328
|
+
|
329
|
+
def build_request_url(path)
|
330
|
+
# Add leading and trailing slashes to path
|
331
|
+
path = "/#{path}".gsub(/\/+/, '/')
|
332
|
+
URI.encode(@config.base_url + path)
|
333
|
+
end
|
334
|
+
|
335
|
+
# Builds the HTTP request body
|
336
|
+
#
|
337
|
+
# @param [Hash] header_params Header parameters
|
338
|
+
# @param [Hash] form_params Query parameters
|
339
|
+
# @param [Object] body HTTP body (JSON/XML)
|
340
|
+
# @return [String] HTTP body data in the form of string
|
341
|
+
def build_request_body(header_params, form_params, body)
|
342
|
+
# http form
|
343
|
+
if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
|
344
|
+
header_params['Content-Type'] == 'multipart/form-data'
|
345
|
+
data = {}
|
346
|
+
form_params.each do |key, value|
|
347
|
+
case value
|
348
|
+
when ::File, ::Array, nil
|
349
|
+
# let typhoeus handle File, Array and nil parameters
|
350
|
+
data[key] = value
|
351
|
+
else
|
352
|
+
data[key] = value.to_s
|
353
|
+
end
|
354
|
+
end
|
355
|
+
elsif body
|
356
|
+
data = body.is_a?(String) ? body : body.to_json
|
357
|
+
else
|
358
|
+
data = nil
|
359
|
+
end
|
360
|
+
data
|
361
|
+
end
|
362
|
+
|
363
|
+
# Sign request using HMAC-SHA512 algorithm.
|
364
|
+
#
|
365
|
+
# @param [Hash] params An array with the nonce, request method, url and the hex digest of the request body
|
366
|
+
def sign_request(params)
|
367
|
+
to_sign = params.join('&')
|
368
|
+
OpenSSL::HMAC.digest(
|
369
|
+
DIGEST,
|
370
|
+
@config.api_secret,
|
371
|
+
to_sign
|
372
|
+
).unpack('H*').first
|
373
|
+
end
|
374
|
+
|
375
|
+
def add_query(url, query_params)
|
376
|
+
return url unless query_params.present?
|
377
|
+
|
378
|
+
url + '?' + query_params.to_query
|
379
|
+
end
|
380
|
+
|
381
|
+
# Sets user agent in HTTP header
|
382
|
+
#
|
383
|
+
# @param [String] user_agent User agent (e.g. swagger-codegen/ruby/1.0.0)
|
384
|
+
def user_agent=(user_agent)
|
385
|
+
@user_agent = user_agent
|
386
|
+
@default_headers['User-Agent'] = @user_agent
|
387
|
+
end
|
388
|
+
|
389
|
+
# Return Accept header based on an array of accepts provided.
|
390
|
+
# @param [Array] accepts array for Accept
|
391
|
+
# @return [String] the Accept header (e.g. application/json)
|
392
|
+
def select_header_accept(accepts)
|
393
|
+
return nil if accepts.nil? || accepts.empty?
|
394
|
+
# use JSON when present, otherwise use all of the provided
|
395
|
+
json_accept = accepts.find { |s| json_mime?(s) }
|
396
|
+
json_accept || accepts.join(',')
|
397
|
+
end
|
398
|
+
|
399
|
+
# Return Content-Type header based on an array of content types provided.
|
400
|
+
# @param [Array] content_types array for Content-Type
|
401
|
+
# @return [String] the Content-Type header (e.g. application/json)
|
402
|
+
def select_header_content_type(content_types)
|
403
|
+
# use application/json by default
|
404
|
+
return 'application/json' if content_types.nil? || content_types.empty?
|
405
|
+
# use JSON when present, otherwise use the first one
|
406
|
+
json_content_type = content_types.find { |s| json_mime?(s) }
|
407
|
+
json_content_type || content_types.first
|
408
|
+
end
|
409
|
+
|
410
|
+
# Convert object (array, hash, object, etc) to JSON string.
|
411
|
+
# @param [Object] model object to be converted into JSON string
|
412
|
+
# @return [String] JSON string representation of the object
|
413
|
+
def object_to_http_body(model)
|
414
|
+
return model if model.nil? || model.is_a?(String)
|
415
|
+
local_body = nil
|
416
|
+
if model.is_a?(Array)
|
417
|
+
local_body = model.map { |m| object_to_hash(m) }
|
418
|
+
else
|
419
|
+
local_body = object_to_hash(model)
|
420
|
+
end
|
421
|
+
local_body.to_json
|
422
|
+
end
|
423
|
+
|
424
|
+
# Convert object(non-array) to hash.
|
425
|
+
# @param [Object] obj object to be converted into JSON string
|
426
|
+
# @return [String] JSON string representation of the object
|
427
|
+
def object_to_hash(obj)
|
428
|
+
if obj.respond_to?(:to_hash)
|
429
|
+
obj.to_hash
|
430
|
+
else
|
431
|
+
obj
|
432
|
+
end
|
433
|
+
end
|
434
|
+
|
435
|
+
# Build parameter value according to the given collection format.
|
436
|
+
# @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
|
437
|
+
def build_collection_param(param, collection_format)
|
438
|
+
case collection_format
|
439
|
+
when :csv
|
440
|
+
param.join(',')
|
441
|
+
when :ssv
|
442
|
+
param.join(' ')
|
443
|
+
when :tsv
|
444
|
+
param.join("\t")
|
445
|
+
when :pipes
|
446
|
+
param.join('|')
|
447
|
+
when :multi
|
448
|
+
# return the array directly as typhoeus will handle it as expected
|
449
|
+
param
|
450
|
+
else
|
451
|
+
fail "unknown collection format: #{collection_format.inspect}"
|
452
|
+
end
|
453
|
+
end
|
454
|
+
end
|
455
|
+
end
|