moneykit 0.0.3 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (192) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -2
  3. data/lib/moneykit/api/access_token_api.rb +9 -9
  4. data/lib/moneykit/api/account_numbers_api.rb +6 -6
  5. data/lib/moneykit/api/accounts_api.rb +15 -15
  6. data/lib/moneykit/api/identity_api.rb +6 -6
  7. data/lib/moneykit/api/institutions_api.rb +7 -7
  8. data/lib/moneykit/api/link_session_api.rb +7 -7
  9. data/lib/moneykit/api/links_api.rb +76 -13
  10. data/lib/moneykit/api/products_api.rb +6 -6
  11. data/lib/moneykit/api/transactions_api.rb +28 -28
  12. data/lib/moneykit/api/users_api.rb +14 -14
  13. data/lib/moneykit/api/webhooks_api.rb +97 -0
  14. data/lib/moneykit/api_client.rb +40 -36
  15. data/lib/moneykit/api_error.rb +1 -1
  16. data/lib/moneykit/configuration.rb +20 -30
  17. data/lib/moneykit/models/account.rb +27 -54
  18. data/lib/moneykit/models/account_balances.rb +18 -27
  19. data/lib/moneykit/models/account_group.rb +23 -26
  20. data/lib/moneykit/models/account_identity.rb +34 -59
  21. data/lib/moneykit/models/account_numbers.rb +75 -51
  22. data/lib/moneykit/models/account_numbers_link_product.rb +18 -28
  23. data/lib/moneykit/models/account_numbers_product_settings.rb +16 -21
  24. data/lib/moneykit/models/account_with_account_numbers.rb +29 -56
  25. data/lib/moneykit/models/accounts_link_product.rb +17 -25
  26. data/lib/moneykit/models/ach_number.rb +19 -26
  27. data/lib/moneykit/models/address.rb +17 -28
  28. data/lib/moneykit/models/api_error_auth_expired_access_token_response.rb +17 -24
  29. data/lib/moneykit/models/api_error_auth_unauthorized_response.rb +18 -25
  30. data/lib/moneykit/models/api_error_rate_limit_exceeded_response.rb +17 -24
  31. data/lib/moneykit/models/bacs_number.rb +18 -25
  32. data/lib/moneykit/models/basic_account_details.rb +20 -29
  33. data/lib/moneykit/models/country.rb +1 -1
  34. data/lib/moneykit/models/create_link_session_request.rb +48 -45
  35. data/lib/moneykit/models/create_link_session_response.rb +17 -24
  36. data/lib/moneykit/models/currency.rb +1 -1
  37. data/lib/moneykit/models/cursor_pagination.rb +17 -24
  38. data/lib/moneykit/models/customer_app.rb +18 -25
  39. data/lib/moneykit/models/eft_number.rb +19 -26
  40. data/lib/moneykit/models/email.rb +17 -22
  41. data/lib/moneykit/models/exchange_token_request.rb +17 -24
  42. data/lib/moneykit/models/exchange_token_response.rb +19 -26
  43. data/lib/moneykit/models/generate_access_token_response.rb +19 -26
  44. data/lib/moneykit/models/get_account_numbers_response.rb +23 -26
  45. data/lib/moneykit/models/get_account_response.rb +19 -26
  46. data/lib/moneykit/models/get_accounts_response.rb +25 -31
  47. data/lib/moneykit/models/get_institutions_response.rb +25 -31
  48. data/lib/moneykit/models/get_transactions_response.rb +34 -39
  49. data/lib/moneykit/models/get_user_accounts_response.rb +24 -32
  50. data/lib/moneykit/models/get_user_links_response.rb +24 -32
  51. data/lib/moneykit/models/get_user_transactions_response.rb +32 -34
  52. data/lib/moneykit/models/http_validation_error.rb +24 -28
  53. data/lib/moneykit/models/identity_link_product.rb +18 -28
  54. data/lib/moneykit/models/identity_product_settings.rb +16 -21
  55. data/lib/moneykit/models/identity_response.rb +24 -30
  56. data/lib/moneykit/models/institution.rb +29 -38
  57. data/lib/moneykit/models/institution_error_not_found_response.rb +17 -24
  58. data/lib/moneykit/models/international_number.rb +18 -25
  59. data/lib/moneykit/models/introspect_client_response.rb +22 -29
  60. data/lib/moneykit/models/jwk_set.rb +23 -26
  61. data/lib/moneykit/models/link_common.rb +25 -39
  62. data/lib/moneykit/models/link_error_bad_config_response.rb +269 -0
  63. data/lib/moneykit/models/link_error_bad_state_response.rb +19 -28
  64. data/lib/moneykit/models/link_error_deleted_response.rb +17 -24
  65. data/lib/moneykit/models/link_error_forbidden_action_response.rb +18 -25
  66. data/lib/moneykit/models/link_error_not_found_response.rb +17 -24
  67. data/lib/moneykit/models/link_error_unauthorized_access_response.rb +17 -24
  68. data/lib/moneykit/models/link_permission_scope.rb +1 -1
  69. data/lib/moneykit/models/link_permissions.rb +23 -33
  70. data/lib/moneykit/models/link_products.rb +16 -21
  71. data/lib/moneykit/models/link_response.rb +26 -39
  72. data/lib/moneykit/models/link_session_customer_user.rb +20 -28
  73. data/lib/moneykit/models/link_session_customer_user_email.rb +18 -25
  74. data/lib/moneykit/models/link_session_customer_user_phone.rb +18 -26
  75. data/lib/moneykit/models/link_session_error_forbidden_config_response.rb +18 -25
  76. data/lib/moneykit/models/link_session_error_invalid_token_exchange.rb +18 -25
  77. data/lib/moneykit/models/link_session_setting_overrides.rb +19 -43
  78. data/lib/moneykit/models/link_state.rb +1 -1
  79. data/lib/moneykit/models/link_state_changed_webhook.rb +28 -31
  80. data/lib/moneykit/models/money_link_features.rb +27 -32
  81. data/lib/moneykit/models/owner.rb +42 -42
  82. data/lib/moneykit/models/phone_number.rb +18 -25
  83. data/lib/moneykit/models/phone_number_type.rb +1 -1
  84. data/lib/moneykit/models/product.rb +1 -1
  85. data/lib/moneykit/models/products_settings.rb +19 -24
  86. data/lib/moneykit/models/provider.rb +4 -2
  87. data/lib/moneykit/models/public_link_error.rb +43 -0
  88. data/lib/moneykit/models/refresh_products_request.rb +23 -34
  89. data/lib/moneykit/models/requested_link_permission.rb +19 -26
  90. data/lib/moneykit/models/response401_disconnect_links_id_delete.rb +75 -269
  91. data/lib/moneykit/models/response401_exchange_token_link_session_exchange_token_post.rb +74 -267
  92. data/lib/moneykit/models/response401_get_account_links_id_accounts_account_id_get.rb +75 -269
  93. data/lib/moneykit/models/response401_get_account_numbers_links_id_accounts_numbers_get.rb +75 -269
  94. data/lib/moneykit/models/response401_get_accounts_links_id_accounts_get.rb +75 -269
  95. data/lib/moneykit/models/response401_get_identities_links_id_identity_get.rb +75 -269
  96. data/lib/moneykit/models/response401_get_institution_institutions_institution_id_get.rb +74 -267
  97. data/lib/moneykit/models/response401_get_institutions_institutions_get.rb +74 -267
  98. data/lib/moneykit/models/response401_get_link_links_id_get.rb +75 -269
  99. data/lib/moneykit/models/response401_get_transactions_diff_links_id_transactions_sync_get.rb +75 -269
  100. data/lib/moneykit/models/response401_get_transactions_links_id_transactions_get.rb +75 -269
  101. data/lib/moneykit/models/response401_get_user_accounts_users_id_accounts_get.rb +74 -267
  102. data/lib/moneykit/models/response401_get_user_links_users_id_links_get.rb +74 -267
  103. data/lib/moneykit/models/response401_get_user_transactions_users_id_transactions_get.rb +74 -267
  104. data/lib/moneykit/models/response401_get_well_known_jwks_well_known_jwks_json_get.rb +74 -267
  105. data/lib/moneykit/models/response401_instrospect_client_auth_introspect_get.rb +74 -267
  106. data/lib/moneykit/models/response401_refresh_products_links_id_products_post.rb +75 -269
  107. data/lib/moneykit/models/response401_reset_login_links_id_reset_post.rb +102 -0
  108. data/lib/moneykit/models/response401_trigger_test_link_webhook_event_webhooks_test_link_id_post.rb +102 -0
  109. data/lib/moneykit/models/response401_update_link_links_id_patch.rb +75 -269
  110. data/lib/moneykit/models/transaction.rb +35 -35
  111. data/lib/moneykit/models/transaction_diff.rb +36 -37
  112. data/lib/moneykit/models/transaction_sync_response.rb +23 -30
  113. data/lib/moneykit/models/transaction_type.rb +1 -1
  114. data/lib/moneykit/models/transaction_type_filter.rb +1 -1
  115. data/lib/moneykit/models/transactions_link_product.rb +18 -28
  116. data/lib/moneykit/models/transactions_product_settings.rb +16 -21
  117. data/lib/moneykit/models/update_link_request.rb +21 -28
  118. data/lib/moneykit/models/validation_error.rb +25 -28
  119. data/lib/moneykit/models/validation_error_location_inner.rb +101 -0
  120. data/lib/moneykit/models/webhook_link_test_event.rb +40 -0
  121. data/lib/moneykit/models/webhook_test_link_request.rb +252 -0
  122. data/lib/moneykit/models/webhook_test_link_response.rb +220 -0
  123. data/lib/moneykit/version.rb +2 -2
  124. data/lib/moneykit.rb +10 -29
  125. data/lib/plaid_compatible/money_kit_plaid_compatible/api/plaid_api.rb +684 -679
  126. data/lib/plaid_compatible/money_kit_plaid_compatible/api_client.rb +330 -330
  127. data/lib/plaid_compatible/money_kit_plaid_compatible/api_error.rb +33 -33
  128. data/lib/plaid_compatible/money_kit_plaid_compatible/configuration.rb +245 -245
  129. data/lib/plaid_compatible/money_kit_plaid_compatible/models/account_balance.rb +198 -198
  130. data/lib/plaid_compatible/money_kit_plaid_compatible/models/account_base.rb +213 -213
  131. data/lib/plaid_compatible/money_kit_plaid_compatible/models/account_identity.rb +223 -223
  132. data/lib/plaid_compatible/money_kit_plaid_compatible/models/account_subtype.rb +89 -89
  133. data/lib/plaid_compatible/money_kit_plaid_compatible/models/account_type.rb +22 -22
  134. data/lib/plaid_compatible/money_kit_plaid_compatible/models/accounts_get_request.rb +198 -198
  135. data/lib/plaid_compatible/money_kit_plaid_compatible/models/accounts_get_request_options.rb +175 -175
  136. data/lib/plaid_compatible/money_kit_plaid_compatible/models/accounts_get_response.rb +194 -194
  137. data/lib/plaid_compatible/money_kit_plaid_compatible/models/address.rb +183 -183
  138. data/lib/plaid_compatible/money_kit_plaid_compatible/models/address_data.rb +209 -209
  139. data/lib/plaid_compatible/money_kit_plaid_compatible/models/auth_get_numbers.rb +211 -209
  140. data/lib/plaid_compatible/money_kit_plaid_compatible/models/auth_get_request.rb +198 -198
  141. data/lib/plaid_compatible/money_kit_plaid_compatible/models/auth_get_request_options.rb +175 -175
  142. data/lib/plaid_compatible/money_kit_plaid_compatible/models/auth_get_response.rb +200 -200
  143. data/lib/plaid_compatible/money_kit_plaid_compatible/models/country_code.rb +19 -19
  144. data/lib/plaid_compatible/money_kit_plaid_compatible/models/email.rb +184 -184
  145. data/lib/plaid_compatible/money_kit_plaid_compatible/models/http_validation_error.rb +171 -171
  146. data/lib/plaid_compatible/money_kit_plaid_compatible/models/identity_get_request.rb +198 -198
  147. data/lib/plaid_compatible/money_kit_plaid_compatible/models/identity_get_request_options.rb +175 -175
  148. data/lib/plaid_compatible/money_kit_plaid_compatible/models/identity_get_response.rb +194 -194
  149. data/lib/plaid_compatible/money_kit_plaid_compatible/models/institution.rb +216 -214
  150. data/lib/plaid_compatible/money_kit_plaid_compatible/models/institutions_get_by_id_request.rb +202 -202
  151. data/lib/plaid_compatible/money_kit_plaid_compatible/models/institutions_get_by_id_response.rb +183 -183
  152. data/lib/plaid_compatible/money_kit_plaid_compatible/models/item.rb +246 -245
  153. data/lib/plaid_compatible/money_kit_plaid_compatible/models/item_error_webhook.rb +208 -208
  154. data/lib/plaid_compatible/money_kit_plaid_compatible/models/item_get_request.rb +192 -192
  155. data/lib/plaid_compatible/money_kit_plaid_compatible/models/item_get_response.rb +189 -189
  156. data/lib/plaid_compatible/money_kit_plaid_compatible/models/item_public_token_exchange_request.rb +192 -192
  157. data/lib/plaid_compatible/money_kit_plaid_compatible/models/item_public_token_exchange_response.rb +193 -193
  158. data/lib/plaid_compatible/money_kit_plaid_compatible/models/item_remove_request.rb +192 -192
  159. data/lib/plaid_compatible/money_kit_plaid_compatible/models/item_remove_response.rb +175 -175
  160. data/lib/plaid_compatible/money_kit_plaid_compatible/models/item_status_investments.rb +182 -182
  161. data/lib/plaid_compatible/money_kit_plaid_compatible/models/item_status_last_webhook.rb +182 -182
  162. data/lib/plaid_compatible/money_kit_plaid_compatible/models/item_status_nullable.rb +183 -183
  163. data/lib/plaid_compatible/money_kit_plaid_compatible/models/item_status_transactions.rb +182 -182
  164. data/lib/plaid_compatible/money_kit_plaid_compatible/models/link_token_create_request.rb +268 -266
  165. data/lib/plaid_compatible/money_kit_plaid_compatible/models/link_token_create_request_user.rb +212 -212
  166. data/lib/plaid_compatible/money_kit_plaid_compatible/models/link_token_create_response.rb +193 -193
  167. data/lib/plaid_compatible/money_kit_plaid_compatible/models/numbers_ach.rb +202 -202
  168. data/lib/plaid_compatible/money_kit_plaid_compatible/models/numbers_bacs.rb +193 -193
  169. data/lib/plaid_compatible/money_kit_plaid_compatible/models/numbers_eft.rb +202 -202
  170. data/lib/plaid_compatible/money_kit_plaid_compatible/models/numbers_international.rb +193 -193
  171. data/lib/plaid_compatible/money_kit_plaid_compatible/models/owner.rb +211 -209
  172. data/lib/plaid_compatible/money_kit_plaid_compatible/models/personal_finance_category.rb +184 -184
  173. data/lib/plaid_compatible/money_kit_plaid_compatible/models/phone_number.rb +193 -193
  174. data/lib/plaid_compatible/money_kit_plaid_compatible/models/plaid_error.rb +247 -245
  175. data/lib/plaid_compatible/money_kit_plaid_compatible/models/plaid_error_type.rb +30 -30
  176. data/lib/plaid_compatible/money_kit_plaid_compatible/models/products.rb +20 -20
  177. data/lib/plaid_compatible/money_kit_plaid_compatible/models/removed_transaction.rb +174 -174
  178. data/lib/plaid_compatible/money_kit_plaid_compatible/models/transaction.rb +283 -283
  179. data/lib/plaid_compatible/money_kit_plaid_compatible/models/transactions_get_request.rb +217 -217
  180. data/lib/plaid_compatible/money_kit_plaid_compatible/models/transactions_get_request_options.rb +211 -211
  181. data/lib/plaid_compatible/money_kit_plaid_compatible/models/transactions_get_response.rb +217 -217
  182. data/lib/plaid_compatible/money_kit_plaid_compatible/models/transactions_refresh_request.rb +192 -192
  183. data/lib/plaid_compatible/money_kit_plaid_compatible/models/transactions_refresh_response.rb +175 -175
  184. data/lib/plaid_compatible/money_kit_plaid_compatible/models/transactions_sync_request.rb +215 -215
  185. data/lib/plaid_compatible/money_kit_plaid_compatible/models/transactions_sync_request_options.rb +184 -184
  186. data/lib/plaid_compatible/money_kit_plaid_compatible/models/transactions_sync_response.rb +228 -228
  187. data/lib/plaid_compatible/money_kit_plaid_compatible/models/validation_error.rb +187 -187
  188. data/lib/plaid_compatible/money_kit_plaid_compatible/models/webhook_environment_values.rb +19 -19
  189. data/lib/plaid_compatible/money_kit_plaid_compatible/version.rb +1 -1
  190. data/lib/plaid_compatible/money_kit_plaid_compatible.rb +13 -13
  191. data/moneykit.gemspec +2 -1
  192. metadata +14 -5
@@ -17,383 +17,383 @@ require 'faraday/multipart'
17
17
 
18
18
  module MoneyKit
19
19
  module PlaidCompatible
20
- class ApiClient
21
- # The Configuration object holding settings to be used in the API client.
22
- attr_accessor :config
23
-
24
- # Defines the headers to be used in HTTP requests of all API calls by default.
25
- #
26
- # @return [Hash]
27
- attr_accessor :default_headers
28
-
29
- # The Faraday::Connection object for fine tuning configuration options.
30
- attr_reader :connection
31
-
32
- # Initializes the ApiClient
33
- # @option config [Configuration] Configuration for initializing the object, default to Configuration.default
34
- def initialize(config = Configuration.default)
35
- @config = config
36
- @user_agent = 'Plaid Ruby v1.0.0'
37
- @default_headers = {
38
- 'Content-Type' => 'application/json',
39
- 'User-Agent' => @user_agent
40
- }
41
- @connection = create_connection
42
- end
43
-
44
- def create_connection
45
- @connection = Faraday.new(url: config.base_url) do |builder|
46
- block_given? ? yield(builder) : builder.adapter(Faraday.default_adapter)
20
+ class ApiClient
21
+ # The Configuration object holding settings to be used in the API client.
22
+ attr_accessor :config
23
+
24
+ # Defines the headers to be used in HTTP requests of all API calls by default.
25
+ #
26
+ # @return [Hash]
27
+ attr_accessor :default_headers
28
+
29
+ # The Faraday::Connection object for fine tuning configuration options.
30
+ attr_reader :connection
31
+
32
+ # Initializes the ApiClient
33
+ # @option config [Configuration] Configuration for initializing the object, default to Configuration.default
34
+ def initialize(config = Configuration.default)
35
+ @config = config
36
+ @user_agent = 'Plaid Ruby v1.0.0'
37
+ @default_headers = {
38
+ 'Content-Type' => 'application/json',
39
+ 'User-Agent' => @user_agent
40
+ }
41
+ @connection = create_connection
47
42
  end
48
- end
49
43
 
50
- def self.default
51
- @@default ||= ApiClient.new
52
- end
44
+ def create_connection
45
+ @connection = Faraday.new(url: config.base_url) do |builder|
46
+ block_given? ? yield(builder) : builder.adapter(Faraday.default_adapter)
47
+ end
48
+ end
53
49
 
54
- # Call an API with given options.
55
- #
56
- # @return [Array<(Object, Integer, Hash)>] an array of 3 elements:
57
- # the data deserialized from response body (could be nil), response status code and response headers.
58
- def call_api(http_method, path, opts = {})
59
- if opts[:header_params]['Content-Type'] == 'multipart/form-data'
60
- connection.request :multipart
61
- connection.request :url_encoded
50
+ def self.default
51
+ @@default ||= ApiClient.new
62
52
  end
63
53
 
64
- begin
65
- response = connection.public_send(http_method.to_sym.downcase) do |req|
66
- build_request(http_method, path, req, opts)
54
+ # Call an API with given options.
55
+ #
56
+ # @return [Array<(Object, Integer, Hash)>] an array of 3 elements:
57
+ # the data deserialized from response body (could be nil), response status code and response headers.
58
+ def call_api(http_method, path, opts = {})
59
+ if opts[:header_params]['Content-Type'] == 'multipart/form-data'
60
+ connection.request :multipart
61
+ connection.request :url_encoded
67
62
  end
68
63
 
69
- @config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n" if @config.debugging
70
-
71
- unless response.success?
72
- if response.status == 0
73
- # Errors from libcurl will be made visible here
74
- raise ApiError.new(code: 0,
75
- message: response.return_message)
76
- else
77
- raw_body = response.body
78
- # Gracefully handle malformed body which is possible during API outage/maintanance
79
- parsed_body = begin
80
- JSON.parse(raw_body)
81
- rescue JSON::ParserError
82
- nil
64
+ begin
65
+ response = connection.public_send(http_method.to_sym.downcase) do |req|
66
+ build_request(http_method, path, req, opts)
67
+ end
68
+
69
+ @config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n" if @config.debugging
70
+
71
+ unless response.success?
72
+ if response.status == 0
73
+ # Errors from libcurl will be made visible here
74
+ raise ApiError.new(code: 0,
75
+ message: response.return_message)
76
+ else
77
+ raw_body = response.body
78
+ # Gracefully handle malformed body which is possible during API outage/maintanance
79
+ parsed_body = begin
80
+ JSON.parse(raw_body)
81
+ rescue JSON::ParserError
82
+ nil
83
+ end
84
+ raise ApiError.new(code: response.status,
85
+ response_headers: response.headers,
86
+ response_body: raw_body,
87
+ data: parsed_body),
88
+ response.reason_phrase
83
89
  end
84
- raise ApiError.new(code: response.status,
85
- response_headers: response.headers,
86
- response_body: raw_body,
87
- data: parsed_body),
88
- response.reason_phrase
89
90
  end
91
+ rescue Faraday::TimeoutError
92
+ raise ApiError, 'Connection timed out'
90
93
  end
91
- rescue Faraday::TimeoutError
92
- raise ApiError, 'Connection timed out'
93
- end
94
94
 
95
- if opts[:return_type]
96
- prepare_file(response) if opts[:return_type] == 'File'
97
- data = deserialize(response, opts[:return_type])
98
- else
99
- data = nil
95
+ if opts[:return_type]
96
+ prepare_file(response) if opts[:return_type] == 'File'
97
+ data = deserialize(response, opts[:return_type])
98
+ else
99
+ data = nil
100
+ end
101
+ [data, response.status, response.headers]
100
102
  end
101
- [data, response.status, response.headers]
102
- end
103
103
 
104
- # Builds the HTTP request
105
- #
106
- # @param [String] http_method HTTP method/verb (e.g. POST)
107
- # @param [String] path URL path (e.g. /account/new)
108
- # @option opts [Hash] :header_params Header parameters
109
- # @option opts [Hash] :query_params Query parameters
110
- # @option opts [Hash] :form_params Query parameters
111
- # @option opts [Object] :body HTTP body (JSON/XML)
112
- # @return [Typhoeus::Request] A Typhoeus Request
113
- def build_request(http_method, path, request, opts = {})
114
- url = build_request_url(path)
115
- http_method = http_method.to_sym.downcase
116
-
117
- header_params = @default_headers.merge(opts[:header_params] || {})
118
- query_params = opts[:query_params] || {}
119
- form_params = opts[:form_params] || {}
120
-
121
- update_params_for_auth! header_params, query_params, opts[:auth_names]
122
-
123
- req_opts = {
124
- method: http_method,
125
- headers: header_params,
126
- params: query_params,
127
- params_encoding: @config.params_encoding,
128
- timeout: @config.timeout,
129
- verbose: @config.debugging
130
- }
131
-
132
- if %i[post patch put delete].include?(http_method)
133
- req_body = build_request_body(header_params, form_params, opts[:body])
134
- req_opts.update body: req_body
135
- @config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n" if @config.debugging
104
+ # Builds the HTTP request
105
+ #
106
+ # @param [String] http_method HTTP method/verb (e.g. POST)
107
+ # @param [String] path URL path (e.g. /account/new)
108
+ # @option opts [Hash] :header_params Header parameters
109
+ # @option opts [Hash] :query_params Query parameters
110
+ # @option opts [Hash] :form_params Query parameters
111
+ # @option opts [Object] :body HTTP body (JSON/XML)
112
+ # @return [Typhoeus::Request] A Typhoeus Request
113
+ def build_request(http_method, path, request, opts = {})
114
+ url = build_request_url(path)
115
+ http_method = http_method.to_sym.downcase
116
+
117
+ header_params = @default_headers.merge(opts[:header_params] || {})
118
+ query_params = opts[:query_params] || {}
119
+ form_params = opts[:form_params] || {}
120
+
121
+ update_params_for_auth! header_params, query_params, opts[:auth_names]
122
+
123
+ req_opts = {
124
+ method: http_method,
125
+ headers: header_params,
126
+ params: query_params,
127
+ params_encoding: @config.params_encoding,
128
+ timeout: @config.timeout,
129
+ verbose: @config.debugging
130
+ }
131
+
132
+ if %i[post patch put delete].include?(http_method)
133
+ req_body = build_request_body(header_params, form_params, opts[:body])
134
+ req_opts.update body: req_body
135
+ @config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n" if @config.debugging
136
+ end
137
+ request.headers = header_params
138
+ request.body = req_body
139
+ request.url url
140
+ request.params = query_params
141
+ request
136
142
  end
137
- request.headers = header_params
138
- request.body = req_body
139
- request.url url
140
- request.params = query_params
141
- request
142
- end
143
143
 
144
- # Builds the HTTP request body
145
- #
146
- # @param [Hash] header_params Header parameters
147
- # @param [Hash] form_params Query parameters
148
- # @param [Object] body HTTP body (JSON/XML)
149
- # @return [String] HTTP body data in the form of string
150
- def build_request_body(header_params, form_params, body)
151
- # http form
152
- if header_params['Content-Type'] == 'application/x-www-form-urlencoded'
153
- data = URI.encode_www_form(form_params)
154
- elsif header_params['Content-Type'] == 'multipart/form-data'
155
- data = {}
156
- form_params.each do |key, value|
157
- data[key] = case value
158
- when ::File, ::Tempfile
159
- # TODO: hardcode to application/octet-stream, need better way to detect content type
160
- Faraday::Multipart::Post::UploadIO.new(value.path, 'application/octet-stream', value.path)
161
- when ::Array, nil
162
- # let Faraday handle Array and nil parameters
163
- value
164
- else
165
- value.to_s
166
- end
144
+ # Builds the HTTP request body
145
+ #
146
+ # @param [Hash] header_params Header parameters
147
+ # @param [Hash] form_params Query parameters
148
+ # @param [Object] body HTTP body (JSON/XML)
149
+ # @return [String] HTTP body data in the form of string
150
+ def build_request_body(header_params, form_params, body)
151
+ # http form
152
+ if header_params['Content-Type'] == 'application/x-www-form-urlencoded'
153
+ data = URI.encode_www_form(form_params)
154
+ elsif header_params['Content-Type'] == 'multipart/form-data'
155
+ data = {}
156
+ form_params.each do |key, value|
157
+ data[key] = case value
158
+ when ::File, ::Tempfile
159
+ # TODO: hardcode to application/octet-stream, need better way to detect content type
160
+ Faraday::Multipart::Post::UploadIO.new(value.path, 'application/octet-stream', value.path)
161
+ when ::Array, nil
162
+ # let Faraday handle Array and nil parameters
163
+ value
164
+ else
165
+ value.to_s
166
+ end
167
+ end
168
+ elsif body
169
+ data = body.is_a?(String) ? body : body.to_json
170
+ else
171
+ data = nil
167
172
  end
168
- elsif body
169
- data = body.is_a?(String) ? body : body.to_json
170
- else
171
- data = nil
173
+ data
172
174
  end
173
- data
174
- end
175
175
 
176
- # Check if the given MIME is a JSON MIME.
177
- # JSON MIME examples:
178
- # application/json
179
- # application/json; charset=UTF8
180
- # APPLICATION/JSON
181
- # */*
182
- # @param [String] mime MIME
183
- # @return [Boolean] True if the MIME is application/json
184
- def json_mime?(mime)
185
- (mime == '*/*') || !(mime =~ %r{Application/.*json(?!p)(;.*)?}i).nil?
186
- end
176
+ # Check if the given MIME is a JSON MIME.
177
+ # JSON MIME examples:
178
+ # application/json
179
+ # application/json; charset=UTF8
180
+ # APPLICATION/JSON
181
+ # */*
182
+ # @param [String] mime MIME
183
+ # @return [Boolean] True if the MIME is application/json
184
+ def json_mime?(mime)
185
+ (mime == '*/*') || !(mime =~ %r{Application/.*json(?!p)(;.*)?}i).nil?
186
+ end
187
187
 
188
- # Deserialize the response to the given return type.
189
- #
190
- # @param [Response] response HTTP response
191
- # @param [String] return_type some examples: "User", "Array<User>", "Hash<String, Integer>"
192
- def deserialize(response, return_type)
193
- body = response.body
188
+ # Deserialize the response to the given return type.
189
+ #
190
+ # @param [Response] response HTTP response
191
+ # @param [String] return_type some examples: "User", "Array<User>", "Hash<String, Integer>"
192
+ def deserialize(response, return_type)
193
+ body = response.body
194
194
 
195
- # handle file downloading - return the File instance processed in request callbacks
196
- # note that response body is empty when the file is written in chunks in request on_body callback
197
- return @tempfile if return_type == 'File'
195
+ # handle file downloading - return the File instance processed in request callbacks
196
+ # note that response body is empty when the file is written in chunks in request on_body callback
197
+ return @tempfile if return_type == 'File'
198
198
 
199
- return nil if body.nil? || body.empty?
199
+ return nil if body.nil? || body.empty?
200
200
 
201
- # return response body directly for String return type
202
- return body if return_type == 'String'
201
+ # return response body directly for String return type
202
+ return body if return_type == 'String'
203
203
 
204
- # ensuring a default content type
205
- content_type = response.headers['Content-Type'] || 'application/json'
204
+ # ensuring a default content type
205
+ content_type = response.headers['Content-Type'] || 'application/json'
206
206
 
207
- raise "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
207
+ raise "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
208
208
 
209
- begin
210
- data = JSON.parse("[#{body}]", symbolize_names: true)[0]
211
- rescue JSON::ParserError => e
212
- raise e unless %w[String Date DateTime].include?(return_type)
209
+ begin
210
+ data = JSON.parse("[#{body}]", symbolize_names: true)[0]
211
+ rescue JSON::ParserError => e
212
+ raise e unless %w[String Date DateTime].include?(return_type)
213
213
 
214
- data = body
215
- end
214
+ data = body
215
+ end
216
216
 
217
- convert_to_type data, return_type
218
- end
217
+ convert_to_type data, return_type
218
+ end
219
219
 
220
- # Convert data to the given return type.
221
- # @param [Object] data Data to be converted
222
- # @param [String] return_type Return type
223
- # @return [Mixed] Data in a particular type
224
- def convert_to_type(data, return_type)
225
- return nil if data.nil?
226
-
227
- case return_type
228
- when 'String'
229
- data.to_s
230
- when 'Integer'
231
- data.to_i
232
- when 'Float'
233
- data.to_f
234
- when 'Boolean'
235
- data == true
236
- when 'DateTime'
237
- # parse date time (expecting ISO 8601 format)
238
- DateTime.parse data
239
- when 'Date'
240
- # parse date time (expecting ISO 8601 format)
241
- Date.parse data
242
- when 'Object'
243
- # generic object (usually a Hash), return directly
244
- data
245
- when /\AArray<(.+)>\z/
246
- # e.g. Array<Pet>
247
- sub_type = ::Regexp.last_match(1)
248
- data.map { |item| convert_to_type(item, sub_type) }
249
- when /\AHash<String, (.+)>\z/
250
- # e.g. Hash<String, Integer>
251
- sub_type = ::Regexp.last_match(1)
252
- {}.tap do |hash|
253
- data.each { |k, v| hash[k] = convert_to_type(v, sub_type) }
220
+ # Convert data to the given return type.
221
+ # @param [Object] data Data to be converted
222
+ # @param [String] return_type Return type
223
+ # @return [Mixed] Data in a particular type
224
+ def convert_to_type(data, return_type)
225
+ return nil if data.nil?
226
+
227
+ case return_type
228
+ when 'String'
229
+ data.to_s
230
+ when 'Integer'
231
+ data.to_i
232
+ when 'Float'
233
+ data.to_f
234
+ when 'Boolean'
235
+ data == true
236
+ when 'DateTime'
237
+ # parse date time (expecting ISO 8601 format)
238
+ DateTime.parse data
239
+ when 'Date'
240
+ # parse date time (expecting ISO 8601 format)
241
+ Date.parse data
242
+ when 'Object'
243
+ # generic object (usually a Hash), return directly
244
+ data
245
+ when /\AArray<(.+)>\z/
246
+ # e.g. Array<Pet>
247
+ sub_type = ::Regexp.last_match(1)
248
+ data.map { |item| convert_to_type(item, sub_type) }
249
+ when /\AHash<String, (.+)>\z/
250
+ # e.g. Hash<String, Integer>
251
+ sub_type = ::Regexp.last_match(1)
252
+ {}.tap do |hash|
253
+ data.each { |k, v| hash[k] = convert_to_type(v, sub_type) }
254
+ end
255
+ else
256
+ # models, e.g. Pet
257
+ MoneyKit::PlaidCompatible.const_get(return_type).build_from_hash(data)
254
258
  end
255
- else
256
- # models, e.g. Pet
257
- MoneyKit::PlaidCompatible.const_get(return_type).build_from_hash(data)
258
259
  end
259
- end
260
260
 
261
- # Save response body into a file in (the defined) temporary folder, using the filename
262
- # from the "Content-Disposition" header if provided, otherwise a random filename.
263
- # The response body is written to the file in chunks in order to handle files which
264
- # size is larger than maximum Ruby String or even larger than the maximum memory a Ruby
265
- # process can use.
266
- #
267
- # @see Configuration#temp_folder_path
268
- def prepare_file(response)
269
- content_disposition = response.headers['Content-Disposition']
270
- if content_disposition && content_disposition =~ /filename=/i
271
- filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
272
- prefix = sanitize_filename(filename)
273
- else
274
- prefix = 'download-'
261
+ # Save response body into a file in (the defined) temporary folder, using the filename
262
+ # from the "Content-Disposition" header if provided, otherwise a random filename.
263
+ # The response body is written to the file in chunks in order to handle files which
264
+ # size is larger than maximum Ruby String or even larger than the maximum memory a Ruby
265
+ # process can use.
266
+ #
267
+ # @see Configuration#temp_folder_path
268
+ def prepare_file(response)
269
+ content_disposition = response.headers['Content-Disposition']
270
+ if content_disposition && content_disposition =~ /filename=/i
271
+ filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
272
+ prefix = sanitize_filename(filename)
273
+ else
274
+ prefix = 'download-'
275
+ end
276
+ prefix += '-' unless prefix.end_with?('-')
277
+ encoding = response.body.encoding
278
+ tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
279
+ @tempfile = tempfile
280
+ tempfile.write(response.body)
281
+ tempfile.close if tempfile
282
+ @config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder " \
283
+ "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file " \
284
+ "will be deleted automatically with GC. It's also recommended to delete the temp file " \
285
+ 'explicitly with `tempfile.delete`'
275
286
  end
276
- prefix += '-' unless prefix.end_with?('-')
277
- encoding = response.body.encoding
278
- tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
279
- @tempfile = tempfile
280
- tempfile.write(response.body)
281
- tempfile.close if tempfile
282
- @config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder " \
283
- "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file " \
284
- "will be deleted automatically with GC. It's also recommended to delete the temp file " \
285
- 'explicitly with `tempfile.delete`'
286
- end
287
287
 
288
- # Sanitize filename by removing path.
289
- # e.g. ../../sun.gif becomes sun.gif
290
- #
291
- # @param [String] filename the filename to be sanitized
292
- # @return [String] the sanitized filename
293
- def sanitize_filename(filename)
294
- filename.gsub(%r{.*[/\\]}, '')
295
- end
288
+ # Sanitize filename by removing path.
289
+ # e.g. ../../sun.gif becomes sun.gif
290
+ #
291
+ # @param [String] filename the filename to be sanitized
292
+ # @return [String] the sanitized filename
293
+ def sanitize_filename(filename)
294
+ filename.gsub(%r{.*[/\\]}, '')
295
+ end
296
296
 
297
- def build_request_url(path)
298
- # Add leading and trailing slashes to path
299
- path = "/#{path}".gsub(%r{/+}, '/')
300
- @config.base_url + path
301
- end
297
+ def build_request_url(path)
298
+ # Add leading and trailing slashes to path
299
+ path = "/#{path}".gsub(%r{/+}, '/')
300
+ @config.base_url + path
301
+ end
302
302
 
303
- # Update hearder and query params based on authentication settings.
304
- #
305
- # @param [Hash] header_params Header parameters
306
- # @param [Hash] query_params Query parameters
307
- # @param [String] auth_names Authentication scheme name
308
- def update_params_for_auth!(header_params, query_params, auth_names)
309
- Array(auth_names).each do |auth_name|
310
- auth_setting = @config.auth_settings[auth_name]
311
- next unless auth_setting
312
-
313
- case auth_setting[:in]
314
- when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
315
- when 'query' then query_params[auth_setting[:key]] = auth_setting[:value]
316
- else raise ArgumentError, 'Authentication token must be in `query` of `header`'
303
+ # Update hearder and query params based on authentication settings.
304
+ #
305
+ # @param [Hash] header_params Header parameters
306
+ # @param [Hash] query_params Query parameters
307
+ # @param [String] auth_names Authentication scheme name
308
+ def update_params_for_auth!(header_params, query_params, auth_names)
309
+ Array(auth_names).each do |auth_name|
310
+ auth_setting = @config.auth_settings[auth_name]
311
+ next unless auth_setting
312
+
313
+ case auth_setting[:in]
314
+ when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
315
+ when 'query' then query_params[auth_setting[:key]] = auth_setting[:value]
316
+ else raise ArgumentError, 'Authentication token must be in `query` of `header`'
317
+ end
317
318
  end
318
319
  end
319
- end
320
320
 
321
- # Sets user agent in HTTP header
322
- #
323
- # @param [String] user_agent User agent (e.g. openapi-generator/ruby/1.0.0)
324
- def user_agent=(user_agent)
325
- @user_agent = user_agent
326
- @default_headers['User-Agent'] = @user_agent
327
- end
321
+ # Sets user agent in HTTP header
322
+ #
323
+ # @param [String] user_agent User agent (e.g. openapi-generator/ruby/1.0.0)
324
+ def user_agent=(user_agent)
325
+ @user_agent = user_agent
326
+ @default_headers['User-Agent'] = @user_agent
327
+ end
328
328
 
329
- # Return Accept header based on an array of accepts provided.
330
- # @param [Array] accepts array for Accept
331
- # @return [String] the Accept header (e.g. application/json)
332
- def select_header_accept(accepts)
333
- return nil if accepts.nil? || accepts.empty?
329
+ # Return Accept header based on an array of accepts provided.
330
+ # @param [Array] accepts array for Accept
331
+ # @return [String] the Accept header (e.g. application/json)
332
+ def select_header_accept(accepts)
333
+ return nil if accepts.nil? || accepts.empty?
334
334
 
335
- # use JSON when present, otherwise use all of the provided
336
- json_accept = accepts.find { |s| json_mime?(s) }
337
- json_accept || accepts.join(',')
338
- end
335
+ # use JSON when present, otherwise use all of the provided
336
+ json_accept = accepts.find { |s| json_mime?(s) }
337
+ json_accept || accepts.join(',')
338
+ end
339
339
 
340
- # Return Content-Type header based on an array of content types provided.
341
- # @param [Array] content_types array for Content-Type
342
- # @return [String] the Content-Type header (e.g. application/json)
343
- def select_header_content_type(content_types)
344
- # use application/json by default
345
- return 'application/json' if content_types.nil? || content_types.empty?
340
+ # Return Content-Type header based on an array of content types provided.
341
+ # @param [Array] content_types array for Content-Type
342
+ # @return [String] the Content-Type header (e.g. application/json)
343
+ def select_header_content_type(content_types)
344
+ # use application/json by default
345
+ return 'application/json' if content_types.nil? || content_types.empty?
346
346
 
347
- # use JSON when present, otherwise use the first one
348
- json_content_type = content_types.find { |s| json_mime?(s) }
349
- json_content_type || content_types.first
350
- end
347
+ # use JSON when present, otherwise use the first one
348
+ json_content_type = content_types.find { |s| json_mime?(s) }
349
+ json_content_type || content_types.first
350
+ end
351
351
 
352
- # Convert object (array, hash, object, etc) to JSON string.
353
- # @param [Object] model object to be converted into JSON string
354
- # @return [String] JSON string representation of the object
355
- def object_to_http_body(model)
356
- return model if model.nil? || model.is_a?(String)
357
-
358
- local_body = nil
359
- local_body = if model.is_a?(Array)
360
- model.map { |m| object_to_hash(m) }
361
- else
362
- object_to_hash(model)
363
- end
364
- local_body.to_json
365
- end
352
+ # Convert object (array, hash, object, etc) to JSON string.
353
+ # @param [Object] model object to be converted into JSON string
354
+ # @return [String] JSON string representation of the object
355
+ def object_to_http_body(model)
356
+ return model if model.nil? || model.is_a?(String)
357
+
358
+ local_body = nil
359
+ local_body = if model.is_a?(Array)
360
+ model.map { |m| object_to_hash(m) }
361
+ else
362
+ object_to_hash(model)
363
+ end
364
+ local_body.to_json
365
+ end
366
366
 
367
- # Convert object(non-array) to hash.
368
- # @param [Object] obj object to be converted into JSON string
369
- # @return [String] JSON string representation of the object
370
- def object_to_hash(obj)
371
- if obj.respond_to?(:to_hash)
372
- obj.to_hash
373
- else
374
- obj
367
+ # Convert object(non-array) to hash.
368
+ # @param [Object] obj object to be converted into JSON string
369
+ # @return [String] JSON string representation of the object
370
+ def object_to_hash(obj)
371
+ if obj.respond_to?(:to_hash)
372
+ obj.to_hash
373
+ else
374
+ obj
375
+ end
375
376
  end
376
- end
377
377
 
378
- # Build parameter value according to the given collection format.
379
- # @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
380
- def build_collection_param(param, collection_format)
381
- case collection_format
382
- when :csv
383
- param.join(',')
384
- when :ssv
385
- param.join(' ')
386
- when :tsv
387
- param.join("\t")
388
- when :pipes
389
- param.join('|')
390
- when :multi
391
- # return the array directly as typhoeus will handle it as expected
392
- param
393
- else
394
- raise "unknown collection format: #{collection_format.inspect}"
378
+ # Build parameter value according to the given collection format.
379
+ # @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
380
+ def build_collection_param(param, collection_format)
381
+ case collection_format
382
+ when :csv
383
+ param.join(',')
384
+ when :ssv
385
+ param.join(' ')
386
+ when :tsv
387
+ param.join("\t")
388
+ when :pipes
389
+ param.join('|')
390
+ when :multi
391
+ # return the array directly as typhoeus will handle it as expected
392
+ param
393
+ else
394
+ raise "unknown collection format: #{collection_format.inspect}"
395
+ end
395
396
  end
396
397
  end
397
398
  end
398
- end
399
399
  end