snaptrade 1.8.0 → 1.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (293) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +10 -10
  3. data/Gemfile.lock +11 -13
  4. data/README.md +218 -218
  5. data/docs/APIStatusApi.md +57 -57
  6. data/docs/AccountInformationApi.md +535 -535
  7. data/docs/AuthenticationApi.md +314 -314
  8. data/docs/ConnectionsApi.md +265 -265
  9. data/docs/ErrorLogsApi.md +68 -68
  10. data/docs/OptionsApi.md +378 -378
  11. data/docs/ReferenceDataApi.md +595 -595
  12. data/docs/TradingApi.md +478 -478
  13. data/docs/TransactionsAndReportingApi.md +183 -183
  14. data/lib/snaptrade/api/account_information_api.rb +801 -801
  15. data/lib/snaptrade/api/api_status_api.rb +98 -98
  16. data/lib/snaptrade/api/authentication_api.rb +481 -481
  17. data/lib/snaptrade/api/connections_api.rb +398 -398
  18. data/lib/snaptrade/api/error_logs_api.rb +112 -112
  19. data/lib/snaptrade/api/options_api.rb +591 -591
  20. data/lib/snaptrade/api/reference_data_api.rb +894 -894
  21. data/lib/snaptrade/api/trading_api.rb +741 -741
  22. data/lib/snaptrade/api/transactions_and_reporting_api.rb +283 -283
  23. data/lib/snaptrade/api_client.rb +427 -427
  24. data/lib/snaptrade/api_client_custom.rb +32 -32
  25. data/lib/snaptrade/api_error.rb +6 -6
  26. data/lib/snaptrade/configuration.rb +334 -334
  27. data/lib/snaptrade/models/account.rb +6 -6
  28. data/lib/snaptrade/models/account_holdings.rb +251 -251
  29. data/lib/snaptrade/models/account_holdings_account.rb +263 -263
  30. data/lib/snaptrade/models/account_order_record.rb +6 -6
  31. data/lib/snaptrade/models/account_order_record_status.rb +6 -6
  32. data/lib/snaptrade/models/account_simple.rb +6 -6
  33. data/lib/snaptrade/models/account_sync_status.rb +6 -6
  34. data/lib/snaptrade/models/action.rb +6 -6
  35. data/lib/snaptrade/models/auth_type.rb +6 -6
  36. data/lib/snaptrade/models/authentication_login_snap_trade_user200_response.rb +6 -6
  37. data/lib/snaptrade/models/balance.rb +6 -6
  38. data/lib/snaptrade/models/brokerage.rb +6 -6
  39. data/lib/snaptrade/models/brokerage_authorization.rb +6 -6
  40. data/lib/snaptrade/models/brokerage_authorization_type.rb +6 -6
  41. data/lib/snaptrade/models/brokerage_authorization_type_read_only.rb +6 -6
  42. data/lib/snaptrade/models/brokerage_authorization_type_read_only_brokerage.rb +6 -6
  43. data/lib/snaptrade/models/brokerage_authorization_type_read_only_type.rb +6 -6
  44. data/lib/snaptrade/models/brokerage_authorization_type_type.rb +6 -6
  45. data/lib/snaptrade/models/brokerage_symbol.rb +6 -6
  46. data/lib/snaptrade/models/brokerage_type.rb +6 -6
  47. data/lib/snaptrade/models/calculated_trade.rb +6 -6
  48. data/lib/snaptrade/models/cash_restriction.rb +6 -6
  49. data/lib/snaptrade/models/connection_type.rb +6 -6
  50. data/lib/snaptrade/models/connections_session_events200_response_inner.rb +6 -6
  51. data/lib/snaptrade/models/currency.rb +6 -6
  52. data/lib/snaptrade/models/delete_user_response.rb +6 -6
  53. data/lib/snaptrade/models/dividend_at_date.rb +6 -6
  54. data/lib/snaptrade/models/encrypted_response.rb +6 -6
  55. data/lib/snaptrade/models/encrypted_response_encrypted_message_data.rb +6 -6
  56. data/lib/snaptrade/models/exchange.rb +6 -6
  57. data/lib/snaptrade/models/exchange_rate_pairs.rb +6 -6
  58. data/lib/snaptrade/models/excluded_asset.rb +6 -6
  59. data/lib/snaptrade/models/jwt.rb +6 -6
  60. data/lib/snaptrade/models/login_redirect_uri.rb +6 -6
  61. data/lib/snaptrade/models/manual_trade.rb +6 -6
  62. data/lib/snaptrade/models/manual_trade_and_impact.rb +6 -6
  63. data/lib/snaptrade/models/manual_trade_balance.rb +6 -6
  64. data/lib/snaptrade/models/manual_trade_form.rb +6 -6
  65. data/lib/snaptrade/models/manual_trade_symbol.rb +6 -6
  66. data/lib/snaptrade/models/model400_failed_request_response.rb +6 -6
  67. data/lib/snaptrade/models/model401_failed_request_response.rb +6 -6
  68. data/lib/snaptrade/models/model403_failed_request_response.rb +6 -6
  69. data/lib/snaptrade/models/model404_failed_request_response.rb +6 -6
  70. data/lib/snaptrade/models/model_asset_class.rb +6 -6
  71. data/lib/snaptrade/models/model_asset_class_details.rb +6 -6
  72. data/lib/snaptrade/models/model_asset_class_target.rb +6 -6
  73. data/lib/snaptrade/models/model_portfolio.rb +6 -6
  74. data/lib/snaptrade/models/model_portfolio_asset_class.rb +6 -6
  75. data/lib/snaptrade/models/model_portfolio_details.rb +6 -6
  76. data/lib/snaptrade/models/model_portfolio_security.rb +6 -6
  77. data/lib/snaptrade/models/model_type.rb +6 -6
  78. data/lib/snaptrade/models/monthly_dividends.rb +6 -6
  79. data/lib/snaptrade/models/net_contributions.rb +6 -6
  80. data/lib/snaptrade/models/net_dividend.rb +6 -6
  81. data/lib/snaptrade/models/option_chain_inner.rb +6 -6
  82. data/lib/snaptrade/models/option_chain_inner_chain_per_root_inner.rb +6 -6
  83. data/lib/snaptrade/models/option_chain_inner_chain_per_root_inner_chain_per_strike_price_inner.rb +6 -6
  84. data/lib/snaptrade/models/option_leg.rb +6 -6
  85. data/lib/snaptrade/models/option_leg_action.rb +6 -6
  86. data/lib/snaptrade/models/option_strategy.rb +6 -6
  87. data/lib/snaptrade/models/option_strategy_legs_inner.rb +6 -6
  88. data/lib/snaptrade/models/options_get_option_strategy_request.rb +6 -6
  89. data/lib/snaptrade/models/options_holdings.rb +6 -6
  90. data/lib/snaptrade/models/options_place_option_strategy_request.rb +6 -6
  91. data/lib/snaptrade/models/options_position.rb +6 -6
  92. data/lib/snaptrade/models/options_symbol.rb +6 -6
  93. data/lib/snaptrade/models/order_strategy_execute_body_order_type.rb +6 -6
  94. data/lib/snaptrade/models/order_strategy_execute_body_time_in_force.rb +6 -6
  95. data/lib/snaptrade/models/order_type.rb +6 -6
  96. data/lib/snaptrade/models/partner_data.rb +6 -6
  97. data/lib/snaptrade/models/past_value.rb +6 -6
  98. data/lib/snaptrade/models/performance_custom.rb +6 -6
  99. data/lib/snaptrade/models/portfolio_group.rb +6 -6
  100. data/lib/snaptrade/models/portfolio_group_info.rb +6 -6
  101. data/lib/snaptrade/models/portfolio_group_position.rb +6 -6
  102. data/lib/snaptrade/models/portfolio_group_settings.rb +6 -6
  103. data/lib/snaptrade/models/position.rb +6 -6
  104. data/lib/snaptrade/models/position_symbol.rb +6 -6
  105. data/lib/snaptrade/models/redirect_tokenand_pin.rb +6 -6
  106. data/lib/snaptrade/models/security_type.rb +6 -6
  107. data/lib/snaptrade/models/session_event.rb +6 -6
  108. data/lib/snaptrade/models/session_event_type.rb +6 -6
  109. data/lib/snaptrade/models/snap_trade_api_disclaimer_accept_status.rb +6 -6
  110. data/lib/snaptrade/models/snap_trade_holdings_account.rb +6 -6
  111. data/lib/snaptrade/models/snap_trade_holdings_account_account_id.rb +6 -6
  112. data/lib/snaptrade/models/snap_trade_holdings_total_value.rb +6 -6
  113. data/lib/snaptrade/models/snap_trade_login_user_request_body.rb +6 -6
  114. data/lib/snaptrade/models/snap_trade_register_user_request_body.rb +6 -6
  115. data/lib/snaptrade/models/state.rb +6 -6
  116. data/lib/snaptrade/models/status.rb +6 -6
  117. data/lib/snaptrade/models/strategy_impact.rb +6 -6
  118. data/lib/snaptrade/models/strategy_impact_legs_inner.rb +6 -6
  119. data/lib/snaptrade/models/strategy_order_place.rb +6 -6
  120. data/lib/snaptrade/models/strategy_order_place_orders_inner.rb +6 -6
  121. data/lib/snaptrade/models/strategy_order_place_orders_inner_legs_inner.rb +6 -6
  122. data/lib/snaptrade/models/strategy_order_record.rb +6 -6
  123. data/lib/snaptrade/models/strategy_order_record_order_type.rb +6 -6
  124. data/lib/snaptrade/models/strategy_order_record_status.rb +6 -6
  125. data/lib/snaptrade/models/strategy_order_record_time_in_force.rb +6 -6
  126. data/lib/snaptrade/models/strategy_quotes.rb +6 -6
  127. data/lib/snaptrade/models/strategy_quotes_greek.rb +6 -6
  128. data/lib/snaptrade/models/strategy_type.rb +6 -6
  129. data/lib/snaptrade/models/sub_period_return_rate.rb +6 -6
  130. data/lib/snaptrade/models/symbol.rb +6 -6
  131. data/lib/snaptrade/models/symbol_query.rb +6 -6
  132. data/lib/snaptrade/models/symbols_quotes_inner.rb +6 -6
  133. data/lib/snaptrade/models/target_asset.rb +6 -6
  134. data/lib/snaptrade/models/time_in_force.rb +6 -6
  135. data/lib/snaptrade/models/trade.rb +6 -6
  136. data/lib/snaptrade/models/trade_action.rb +6 -6
  137. data/lib/snaptrade/models/trade_execution_status.rb +6 -6
  138. data/lib/snaptrade/models/trade_execution_status_action.rb +6 -6
  139. data/lib/snaptrade/models/trade_impact.rb +6 -6
  140. data/lib/snaptrade/models/trading_cancel_user_account_order_request.rb +6 -6
  141. data/lib/snaptrade/models/trading_place_oco_order_request.rb +6 -6
  142. data/lib/snaptrade/models/transactions_status.rb +6 -6
  143. data/lib/snaptrade/models/type.rb +6 -6
  144. data/lib/snaptrade/models/underlying_symbol.rb +6 -6
  145. data/lib/snaptrade/models/universal_activity.rb +6 -6
  146. data/lib/snaptrade/models/universal_symbol.rb +6 -6
  147. data/lib/snaptrade/models/universal_symbol_ticker.rb +6 -6
  148. data/lib/snaptrade/models/us_exchange.rb +6 -6
  149. data/lib/snaptrade/models/user_error_log.rb +6 -6
  150. data/lib/snaptrade/models/user_i_dand_secret.rb +6 -6
  151. data/lib/snaptrade/models/user_settings.rb +6 -6
  152. data/lib/snaptrade/version.rb +13 -13
  153. data/lib/snaptrade.rb +211 -211
  154. data/snaptrade.gemspec +41 -41
  155. data/spec/api/account_information_api_spec.rb +135 -135
  156. data/spec/api/api_status_api_spec.rb +42 -42
  157. data/spec/api/authentication_api_spec.rb +88 -88
  158. data/spec/api/connections_api_spec.rb +82 -82
  159. data/spec/api/error_logs_api_spec.rb +43 -43
  160. data/spec/api/options_api_spec.rb +101 -101
  161. data/spec/api/reference_data_api_spec.rb +150 -150
  162. data/spec/api/trading_api_spec.rb +112 -112
  163. data/spec/api/transactions_and_reporting_api_spec.rb +67 -67
  164. data/spec/api_client_spec.rb +193 -193
  165. data/spec/configuration_spec.rb +6 -6
  166. data/spec/getting_started_spec.rb +108 -108
  167. data/spec/models/account_holdings_account_spec.rb +54 -54
  168. data/spec/models/account_holdings_spec.rb +48 -48
  169. data/spec/models/account_order_record_spec.rb +132 -132
  170. data/spec/models/account_order_record_status_spec.rb +24 -24
  171. data/spec/models/account_simple_spec.rb +48 -48
  172. data/spec/models/account_spec.rb +84 -84
  173. data/spec/models/account_sync_status_spec.rb +30 -30
  174. data/spec/models/action_spec.rb +24 -24
  175. data/spec/models/auth_type_spec.rb +24 -24
  176. data/spec/models/authentication_login_snap_trade_user200_response_spec.rb +27 -27
  177. data/spec/models/balance_spec.rb +36 -36
  178. data/spec/models/brokerage_authorization_spec.rb +78 -78
  179. data/spec/models/brokerage_authorization_type_read_only_brokerage_spec.rb +42 -42
  180. data/spec/models/brokerage_authorization_type_read_only_spec.rb +48 -48
  181. data/spec/models/brokerage_authorization_type_read_only_type_spec.rb +24 -24
  182. data/spec/models/brokerage_authorization_type_spec.rb +30 -30
  183. data/spec/models/brokerage_authorization_type_type_spec.rb +24 -24
  184. data/spec/models/brokerage_spec.rb +132 -132
  185. data/spec/models/brokerage_symbol_spec.rb +54 -54
  186. data/spec/models/brokerage_type_spec.rb +36 -36
  187. data/spec/models/calculated_trade_spec.rb +36 -36
  188. data/spec/models/cash_restriction_spec.rb +54 -54
  189. data/spec/models/connection_type_spec.rb +24 -24
  190. data/spec/models/connections_session_events200_response_inner_spec.rb +66 -66
  191. data/spec/models/currency_spec.rb +42 -42
  192. data/spec/models/delete_user_response_spec.rb +36 -36
  193. data/spec/models/dividend_at_date_spec.rb +42 -42
  194. data/spec/models/encrypted_response_encrypted_message_data_spec.rb +42 -42
  195. data/spec/models/encrypted_response_spec.rb +36 -36
  196. data/spec/models/exchange_rate_pairs_spec.rb +42 -42
  197. data/spec/models/exchange_spec.rb +72 -72
  198. data/spec/models/excluded_asset_spec.rb +30 -30
  199. data/spec/models/jwt_spec.rb +30 -30
  200. data/spec/models/login_redirect_uri_spec.rb +36 -36
  201. data/spec/models/manual_trade_and_impact_spec.rb +42 -42
  202. data/spec/models/manual_trade_balance_spec.rb +42 -42
  203. data/spec/models/manual_trade_form_spec.rb +72 -72
  204. data/spec/models/manual_trade_spec.rb +72 -72
  205. data/spec/models/manual_trade_symbol_spec.rb +60 -60
  206. data/spec/models/model400_failed_request_response_spec.rb +36 -36
  207. data/spec/models/model401_failed_request_response_spec.rb +36 -36
  208. data/spec/models/model403_failed_request_response_spec.rb +36 -36
  209. data/spec/models/model404_failed_request_response_spec.rb +36 -36
  210. data/spec/models/model_asset_class_details_spec.rb +36 -36
  211. data/spec/models/model_asset_class_spec.rb +36 -36
  212. data/spec/models/model_asset_class_target_spec.rb +30 -30
  213. data/spec/models/model_portfolio_asset_class_spec.rb +36 -36
  214. data/spec/models/model_portfolio_details_spec.rb +42 -42
  215. data/spec/models/model_portfolio_security_spec.rb +36 -36
  216. data/spec/models/model_portfolio_spec.rb +42 -42
  217. data/spec/models/model_type_spec.rb +24 -24
  218. data/spec/models/monthly_dividends_spec.rb +36 -36
  219. data/spec/models/net_contributions_spec.rb +42 -42
  220. data/spec/models/net_dividend_spec.rb +42 -42
  221. data/spec/models/option_chain_inner_chain_per_root_inner_chain_per_strike_price_inner_spec.rb +42 -42
  222. data/spec/models/option_chain_inner_chain_per_root_inner_spec.rb +42 -42
  223. data/spec/models/option_chain_inner_spec.rb +54 -54
  224. data/spec/models/option_leg_action_spec.rb +24 -24
  225. data/spec/models/option_leg_spec.rb +42 -42
  226. data/spec/models/option_strategy_legs_inner_spec.rb +48 -48
  227. data/spec/models/option_strategy_spec.rb +54 -54
  228. data/spec/models/options_get_option_strategy_request_spec.rb +42 -42
  229. data/spec/models/options_holdings_spec.rb +60 -60
  230. data/spec/models/options_place_option_strategy_request_spec.rb +42 -42
  231. data/spec/models/options_position_spec.rb +66 -66
  232. data/spec/models/options_symbol_spec.rb +90 -90
  233. data/spec/models/order_strategy_execute_body_order_type_spec.rb +24 -24
  234. data/spec/models/order_strategy_execute_body_time_in_force_spec.rb +24 -24
  235. data/spec/models/order_type_spec.rb +24 -24
  236. data/spec/models/partner_data_spec.rb +96 -96
  237. data/spec/models/past_value_spec.rb +42 -42
  238. data/spec/models/performance_custom_spec.rb +138 -138
  239. data/spec/models/portfolio_group_info_spec.rb +90 -90
  240. data/spec/models/portfolio_group_position_spec.rb +42 -42
  241. data/spec/models/portfolio_group_settings_spec.rb +54 -54
  242. data/spec/models/portfolio_group_spec.rb +36 -36
  243. data/spec/models/position_spec.rb +60 -60
  244. data/spec/models/position_symbol_spec.rb +60 -60
  245. data/spec/models/redirect_tokenand_pin_spec.rb +36 -36
  246. data/spec/models/security_type_spec.rb +48 -48
  247. data/spec/models/session_event_spec.rb +66 -66
  248. data/spec/models/session_event_type_spec.rb +24 -24
  249. data/spec/models/snap_trade_api_disclaimer_accept_status_spec.rb +36 -36
  250. data/spec/models/snap_trade_holdings_account_account_id_spec.rb +78 -78
  251. data/spec/models/snap_trade_holdings_account_spec.rb +72 -72
  252. data/spec/models/snap_trade_holdings_total_value_spec.rb +36 -36
  253. data/spec/models/snap_trade_login_user_request_body_spec.rb +54 -54
  254. data/spec/models/snap_trade_register_user_request_body_spec.rb +36 -36
  255. data/spec/models/state_spec.rb +24 -24
  256. data/spec/models/status_spec.rb +42 -42
  257. data/spec/models/strategy_impact_legs_inner_spec.rb +66 -66
  258. data/spec/models/strategy_impact_spec.rb +90 -90
  259. data/spec/models/strategy_order_place_orders_inner_legs_inner_spec.rb +66 -66
  260. data/spec/models/strategy_order_place_orders_inner_spec.rb +282 -282
  261. data/spec/models/strategy_order_place_spec.rb +36 -36
  262. data/spec/models/strategy_order_record_order_type_spec.rb +24 -24
  263. data/spec/models/strategy_order_record_spec.rb +90 -90
  264. data/spec/models/strategy_order_record_status_spec.rb +24 -24
  265. data/spec/models/strategy_order_record_time_in_force_spec.rb +24 -24
  266. data/spec/models/strategy_quotes_greek_spec.rb +54 -54
  267. data/spec/models/strategy_quotes_spec.rb +60 -60
  268. data/spec/models/strategy_type_spec.rb +24 -24
  269. data/spec/models/sub_period_return_rate_spec.rb +42 -42
  270. data/spec/models/symbol_query_spec.rb +30 -30
  271. data/spec/models/symbol_spec.rb +66 -66
  272. data/spec/models/symbols_quotes_inner_spec.rb +60 -60
  273. data/spec/models/target_asset_spec.rb +60 -60
  274. data/spec/models/time_in_force_spec.rb +24 -24
  275. data/spec/models/trade_action_spec.rb +24 -24
  276. data/spec/models/trade_execution_status_action_spec.rb +24 -24
  277. data/spec/models/trade_execution_status_spec.rb +78 -78
  278. data/spec/models/trade_impact_spec.rb +54 -54
  279. data/spec/models/trade_spec.rb +72 -72
  280. data/spec/models/trading_cancel_user_account_order_request_spec.rb +30 -30
  281. data/spec/models/trading_place_oco_order_request_spec.rb +36 -36
  282. data/spec/models/transactions_status_spec.rb +36 -36
  283. data/spec/models/type_spec.rb +24 -24
  284. data/spec/models/underlying_symbol_spec.rb +66 -66
  285. data/spec/models/universal_activity_spec.rb +120 -120
  286. data/spec/models/universal_symbol_spec.rb +72 -72
  287. data/spec/models/universal_symbol_ticker_spec.rb +72 -72
  288. data/spec/models/us_exchange_spec.rb +78 -78
  289. data/spec/models/user_error_log_spec.rb +60 -60
  290. data/spec/models/user_i_dand_secret_spec.rb +36 -36
  291. data/spec/models/user_settings_spec.rb +84 -84
  292. data/spec/spec_helper.rb +6 -6
  293. metadata +113 -113
@@ -1,427 +1,427 @@
1
- =begin
2
- #SnapTrade
3
-
4
- #Connect brokerage accounts to your app for live positions and trading
5
-
6
- The version of the OpenAPI document: 1.0.0
7
- Contact: api@snaptrade.com
8
-
9
- =end
10
-
11
- require 'date'
12
- require 'json'
13
- require 'logger'
14
- require 'tempfile'
15
- require 'time'
16
- require_relative './api_client_custom'
17
- require 'faraday'
18
- require 'faraday/multipart' if Gem::Version.new(Faraday::VERSION) >= Gem::Version.new('2.0')
19
-
20
- module SnapTrade
21
- class ApiClient
22
- # The Configuration object holding settings to be used in the API client.
23
- attr_accessor :config
24
-
25
- # Defines the headers to be used in HTTP requests of all API calls by default.
26
- #
27
- # @return [Hash]
28
- attr_accessor :default_headers
29
-
30
- # Initializes the ApiClient
31
- # @option config [Configuration] Configuration for initializing the object, default to Configuration.default
32
- def initialize(config = Configuration.default)
33
- @config = config
34
- @user_agent = "Konfig/#{VERSION}/ruby"
35
- @default_headers = {
36
- 'Content-Type' => 'application/json',
37
- 'User-Agent' => @user_agent
38
- }
39
- end
40
-
41
- def self.default
42
- @@default ||= ApiClient.new
43
- end
44
-
45
- # Call an API with given options.
46
- #
47
- # @return [Array<(Object, Integer, Hash)>] an array of 3 elements:
48
- # the data deserialized from response body (could be nil), response status code and response headers.
49
- def call_api(http_method, path, opts = {})
50
- begin
51
- response = connection(opts).public_send(http_method.to_sym.downcase) do |req|
52
- build_request(http_method, path, req, opts)
53
- end
54
-
55
- if config.debugging
56
- config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
57
- end
58
-
59
- unless response.success?
60
- if response.status == 0
61
- # Errors from libcurl will be made visible here
62
- fail ApiError.new(code: 0,
63
- message: response.return_message)
64
- else
65
- fail ApiError.new(code: response.status,
66
- response_headers: response.headers,
67
- response_body: response.body),
68
- response.reason_phrase
69
- end
70
- end
71
- rescue Faraday::TimeoutError
72
- fail ApiError.new('Connection timed out')
73
- end
74
-
75
- if opts[:return_type]
76
- data = deserialize(response, opts[:return_type])
77
- else
78
- data = nil
79
- end
80
- return data, response.status, response.headers, response
81
- end
82
-
83
- # Builds the HTTP request
84
- #
85
- # @param [String] http_method HTTP method/verb (e.g. POST)
86
- # @param [String] path URL path (e.g. /account/new)
87
- # @option opts [Hash] :header_params Header parameters
88
- # @option opts [Hash] :query_params Query parameters
89
- # @option opts [Hash] :form_params Query parameters
90
- # @option opts [Object] :body HTTP body (JSON/XML)
91
- # @return [Faraday::Request] A Faraday Request
92
- def build_request(http_method, path, request, opts = {})
93
- url = build_request_url(path, opts)
94
- http_method = http_method.to_sym.downcase
95
-
96
- header_params = @default_headers.merge(opts[:header_params] || {})
97
- query_params = opts[:query_params] || {}
98
- form_params = opts[:form_params] || {}
99
-
100
- update_params_for_auth! header_params, query_params, opts[:auth_names]
101
-
102
- if [:post, :patch, :put, :delete].include?(http_method)
103
- req_body = build_request_body(header_params, form_params, opts[:body])
104
- if config.debugging
105
- config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
106
- end
107
- end
108
- request.headers = header_params
109
- request.body = req_body
110
-
111
- # Overload default options only if provided
112
- request.options.params_encoder = config.params_encoder if config.params_encoder
113
- request.options.timeout = config.timeout if config.timeout
114
-
115
- request.url url
116
- request.params = query_params
117
- download_file(request) if opts[:return_type] == 'File' || opts[:return_type] == 'Binary'
118
-
119
- ApiClientCustom.request_hook(request, @config)
120
-
121
- request
122
- end
123
-
124
- # Builds the HTTP request body
125
- #
126
- # @param [Hash] header_params Header parameters
127
- # @param [Hash] form_params Query parameters
128
- # @param [Object] body HTTP body (JSON/XML)
129
- # @return [String] HTTP body data in the form of string
130
- def build_request_body(header_params, form_params, body)
131
- # http form
132
- if header_params['Content-Type'] == 'application/x-www-form-urlencoded'
133
- data = URI.encode_www_form(form_params)
134
- elsif header_params['Content-Type'] == 'multipart/form-data'
135
- data = {}
136
- form_params.each do |key, value|
137
- case value
138
- when ::File, ::Tempfile
139
- # TODO hardcode to application/octet-stream, need better way to detect content type
140
- data[key] = Faraday::FilePart.new(value.path, 'application/octet-stream', value.path)
141
- when ::Array, nil
142
- # let Faraday handle Array and nil parameters
143
- data[key] = value
144
- else
145
- data[key] = value.to_s
146
- end
147
- end
148
- elsif body
149
- data = body.is_a?(String) ? body : body.to_json
150
- else
151
- data = nil
152
- end
153
- data
154
- end
155
-
156
- def download_file(request)
157
- @stream = []
158
-
159
- # handle streaming Responses
160
- request.options.on_data = Proc.new do |chunk, overall_received_bytes|
161
- @stream << chunk
162
- end
163
- end
164
-
165
- def connection(opts)
166
- opts[:header_params]['Content-Type'] == 'multipart/form-data' ? connection_multipart : connection_regular
167
- end
168
-
169
- def connection_multipart
170
- @connection_multipart ||= build_connection do |conn|
171
- conn.request :multipart
172
- conn.request :url_encoded
173
- end
174
- end
175
-
176
- def connection_regular
177
- @connection_regular ||= build_connection
178
- end
179
-
180
- def build_connection
181
- Faraday.new(url: config.base_url, ssl: ssl_options) do |conn|
182
- basic_auth(conn)
183
- config.configure_middleware(conn)
184
- yield(conn) if block_given?
185
- conn.adapter(Faraday.default_adapter)
186
- end
187
- end
188
-
189
- def ssl_options
190
- {
191
- ca_file: config.ssl_ca_file,
192
- verify: config.ssl_verify,
193
- verify_mode: config.ssl_verify_mode,
194
- client_cert: config.ssl_client_cert,
195
- client_key: config.ssl_client_key
196
- }
197
- end
198
-
199
- def basic_auth(conn)
200
- if config.username && config.password
201
- if Gem::Version.new(Faraday::VERSION) >= Gem::Version.new('2.0')
202
- conn.request(:authorization, :basic, config.username, config.password)
203
- else
204
- conn.request(:basic_auth, config.username, config.password)
205
- end
206
- end
207
- end
208
-
209
- # Check if the given MIME is a JSON MIME.
210
- # JSON MIME examples:
211
- # application/json
212
- # application/json; charset=UTF8
213
- # APPLICATION/JSON
214
- # */*
215
- # @param [String] mime MIME
216
- # @return [Boolean] True if the MIME is application/json
217
- def json_mime?(mime)
218
- (mime == '*/*') || !(mime =~ /Application\/.*json(?!p)(;.*)?/i).nil?
219
- end
220
-
221
- # Deserialize the response to the given return type.
222
- #
223
- # @param [Response] response HTTP response
224
- # @param [String] return_type some examples: "User", "Array<User>", "Hash<String, Integer>"
225
- def deserialize(response, return_type)
226
- body = response.body
227
-
228
- # handle file downloading - return the File instance processed in request callbacks
229
- # note that response body is empty when the file is written in chunks in request on_body callback
230
- if return_type == 'File'
231
- if @config.return_binary_data == true
232
- # return byte stream
233
- encoding = body.encoding
234
- return @stream.join.force_encoding(encoding)
235
- else
236
- # return file instead of binary data
237
- content_disposition = response.headers['Content-Disposition']
238
- if content_disposition && content_disposition =~ /filename=/i
239
- filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
240
- prefix = sanitize_filename(filename)
241
- else
242
- prefix = 'download-'
243
- end
244
- prefix = prefix + '-' unless prefix.end_with?('-')
245
- encoding = body.encoding
246
- @tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
247
- @tempfile.write(@stream.join.force_encoding(encoding))
248
- @tempfile.close
249
- @config.logger.info "Temp file written to #{@tempfile.path}, please copy the file to a proper folder "\
250
- "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
251
- "will be deleted automatically with GC. It's also recommended to delete the temp file "\
252
- "explicitly with `tempfile.delete`"
253
- return @tempfile
254
- end
255
- end
256
-
257
- return nil if body.nil? || body.empty?
258
-
259
- # return response body directly for String return type
260
- return body if return_type == 'String'
261
-
262
- # ensuring a default content type
263
- content_type = response.headers['Content-Type'] || 'application/json'
264
-
265
- fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
266
-
267
- begin
268
- data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
269
- rescue JSON::ParserError => e
270
- if %w(String Date Time).include?(return_type)
271
- data = body
272
- else
273
- raise e
274
- end
275
- end
276
-
277
- convert_to_type data, return_type
278
- end
279
-
280
- # Convert data to the given return type.
281
- # @param [Object] data Data to be converted
282
- # @param [String] return_type Return type
283
- # @return [Mixed] Data in a particular type
284
- def convert_to_type(data, return_type)
285
- return nil if data.nil?
286
- case return_type
287
- when 'String'
288
- data.to_s
289
- when 'Integer'
290
- data.to_i
291
- when 'Float'
292
- data.to_f
293
- when 'Boolean'
294
- data == true
295
- when 'Time'
296
- # parse date time (expecting ISO 8601 format)
297
- Time.parse data
298
- when 'Date'
299
- # parse date time (expecting ISO 8601 format)
300
- Date.parse data
301
- when 'Object'
302
- # generic object (usually a Hash), return directly
303
- data
304
- when /\AArray<(.+)>\z/
305
- # e.g. Array<Pet>
306
- sub_type = $1
307
- data.map { |item| convert_to_type(item, sub_type) }
308
- when /\AHash\<String, (.+)\>\z/
309
- # e.g. Hash<String, Integer>
310
- sub_type = $1
311
- {}.tap do |hash|
312
- data.each { |k, v| hash[k] = convert_to_type(v, sub_type) }
313
- end
314
- else
315
- # models (e.g. Pet) or oneOf
316
- klass = SnapTrade.const_get(return_type)
317
- klass.respond_to?(:openapi_one_of) ? klass.build(data) : klass.build_from_hash(data)
318
- end
319
- end
320
-
321
- # Sanitize filename by removing path.
322
- # e.g. ../../sun.gif becomes sun.gif
323
- #
324
- # @param [String] filename the filename to be sanitized
325
- # @return [String] the sanitized filename
326
- def sanitize_filename(filename)
327
- filename.gsub(/.*[\/\\]/, '')
328
- end
329
-
330
- def build_request_url(path, opts = {})
331
- # Add leading and trailing slashes to path
332
- path = "/#{path}".gsub(/\/+/, '/')
333
- @config.base_url(opts[:operation]) + path
334
- end
335
-
336
- # Update header and query params based on authentication settings.
337
- #
338
- # @param [Hash] header_params Header parameters
339
- # @param [Hash] query_params Query parameters
340
- # @param [String] auth_names Authentication scheme name
341
- def update_params_for_auth!(header_params, query_params, auth_names)
342
- Array(auth_names).each do |auth_name|
343
- auth_setting = @config.auth_settings[auth_name]
344
- next unless auth_setting
345
- case auth_setting[:in]
346
- when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
347
- when 'query' then query_params[auth_setting[:key]] = auth_setting[:value]
348
- else fail ArgumentError, 'Authentication token must be in `query` or `header`'
349
- end
350
- end
351
- end
352
-
353
- # Sets user agent in HTTP header
354
- #
355
- # @param [String] user_agent User agent (e.g. Konfig/ruby/1.0.0)
356
- def user_agent=(user_agent)
357
- @user_agent = user_agent
358
- @default_headers['User-Agent'] = @user_agent
359
- end
360
-
361
- # Return Accept header based on an array of accepts provided.
362
- # @param [Array] accepts array for Accept
363
- # @return [String] the Accept header (e.g. application/json)
364
- def select_header_accept(accepts)
365
- return nil if accepts.nil? || accepts.empty?
366
- # use JSON when present, otherwise use all of the provided
367
- json_accept = accepts.find { |s| json_mime?(s) }
368
- json_accept || accepts.join(',')
369
- end
370
-
371
- # Return Content-Type header based on an array of content types provided.
372
- # @param [Array] content_types array for Content-Type
373
- # @return [String] the Content-Type header (e.g. application/json)
374
- def select_header_content_type(content_types)
375
- # return nil by default
376
- return if content_types.nil? || content_types.empty?
377
- # use JSON when present, otherwise use the first one
378
- json_content_type = content_types.find { |s| json_mime?(s) }
379
- json_content_type || content_types.first
380
- end
381
-
382
- # Convert object (array, hash, object, etc) to JSON string.
383
- # @param [Object] model object to be converted into JSON string
384
- # @return [String] JSON string representation of the object
385
- def object_to_http_body(model)
386
- return model if model.nil? || model.is_a?(String)
387
- local_body = nil
388
- if model.is_a?(Array)
389
- local_body = model.map { |m| object_to_hash(m) }
390
- else
391
- local_body = object_to_hash(model)
392
- end
393
- local_body.to_json
394
- end
395
-
396
- # Convert object(non-array) to hash.
397
- # @param [Object] obj object to be converted into JSON string
398
- # @return [String] JSON string representation of the object
399
- def object_to_hash(obj)
400
- if obj.respond_to?(:to_hash)
401
- obj.to_hash
402
- else
403
- obj
404
- end
405
- end
406
-
407
- # Build parameter value according to the given collection format.
408
- # @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
409
- def build_collection_param(param, collection_format)
410
- case collection_format
411
- when :csv
412
- param.join(',')
413
- when :ssv
414
- param.join(' ')
415
- when :tsv
416
- param.join("\t")
417
- when :pipes
418
- param.join('|')
419
- when :multi
420
- # return the array directly as typhoeus will handle it as expected
421
- param
422
- else
423
- fail "unknown collection format: #{collection_format.inspect}"
424
- end
425
- end
426
- end
427
- end
1
+ =begin
2
+ #SnapTrade
3
+
4
+ #Connect brokerage accounts to your app for live positions and trading
5
+
6
+ The version of the OpenAPI document: 1.0.0
7
+ Contact: api@snaptrade.com
8
+
9
+ =end
10
+
11
+ require 'date'
12
+ require 'json'
13
+ require 'logger'
14
+ require 'tempfile'
15
+ require 'time'
16
+ require_relative './api_client_custom'
17
+ require 'faraday'
18
+ require 'faraday/multipart' if Gem::Version.new(Faraday::VERSION) >= Gem::Version.new('2.0')
19
+
20
+ module SnapTrade
21
+ class ApiClient
22
+ # The Configuration object holding settings to be used in the API client.
23
+ attr_accessor :config
24
+
25
+ # Defines the headers to be used in HTTP requests of all API calls by default.
26
+ #
27
+ # @return [Hash]
28
+ attr_accessor :default_headers
29
+
30
+ # Initializes the ApiClient
31
+ # @option config [Configuration] Configuration for initializing the object, default to Configuration.default
32
+ def initialize(config = Configuration.default)
33
+ @config = config
34
+ @user_agent = "Konfig/#{VERSION}/ruby"
35
+ @default_headers = {
36
+ 'Content-Type' => 'application/json',
37
+ 'User-Agent' => @user_agent
38
+ }
39
+ end
40
+
41
+ def self.default
42
+ @@default ||= ApiClient.new
43
+ end
44
+
45
+ # Call an API with given options.
46
+ #
47
+ # @return [Array<(Object, Integer, Hash)>] an array of 3 elements:
48
+ # the data deserialized from response body (could be nil), response status code and response headers.
49
+ def call_api(http_method, path, opts = {})
50
+ begin
51
+ response = connection(opts).public_send(http_method.to_sym.downcase) do |req|
52
+ build_request(http_method, path, req, opts)
53
+ end
54
+
55
+ if config.debugging
56
+ config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
57
+ end
58
+
59
+ unless response.success?
60
+ if response.status == 0
61
+ # Errors from libcurl will be made visible here
62
+ fail ApiError.new(code: 0,
63
+ message: response.return_message)
64
+ else
65
+ fail ApiError.new(code: response.status,
66
+ response_headers: response.headers,
67
+ response_body: response.body),
68
+ response.reason_phrase
69
+ end
70
+ end
71
+ rescue Faraday::TimeoutError
72
+ fail ApiError.new('Connection timed out')
73
+ end
74
+
75
+ if opts[:return_type]
76
+ data = deserialize(response, opts[:return_type])
77
+ else
78
+ data = nil
79
+ end
80
+ return data, response.status, response.headers, response
81
+ end
82
+
83
+ # Builds the HTTP request
84
+ #
85
+ # @param [String] http_method HTTP method/verb (e.g. POST)
86
+ # @param [String] path URL path (e.g. /account/new)
87
+ # @option opts [Hash] :header_params Header parameters
88
+ # @option opts [Hash] :query_params Query parameters
89
+ # @option opts [Hash] :form_params Query parameters
90
+ # @option opts [Object] :body HTTP body (JSON/XML)
91
+ # @return [Faraday::Request] A Faraday Request
92
+ def build_request(http_method, path, request, opts = {})
93
+ url = build_request_url(path, opts)
94
+ http_method = http_method.to_sym.downcase
95
+
96
+ header_params = @default_headers.merge(opts[:header_params] || {})
97
+ query_params = opts[:query_params] || {}
98
+ form_params = opts[:form_params] || {}
99
+
100
+ update_params_for_auth! header_params, query_params, opts[:auth_names]
101
+
102
+ if [:post, :patch, :put, :delete].include?(http_method)
103
+ req_body = build_request_body(header_params, form_params, opts[:body])
104
+ if config.debugging
105
+ config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
106
+ end
107
+ end
108
+ request.headers = header_params
109
+ request.body = req_body
110
+
111
+ # Overload default options only if provided
112
+ request.options.params_encoder = config.params_encoder if config.params_encoder
113
+ request.options.timeout = config.timeout if config.timeout
114
+
115
+ request.url url
116
+ request.params = query_params
117
+ download_file(request) if opts[:return_type] == 'File' || opts[:return_type] == 'Binary'
118
+
119
+ ApiClientCustom.request_hook(request, @config)
120
+
121
+ request
122
+ end
123
+
124
+ # Builds the HTTP request body
125
+ #
126
+ # @param [Hash] header_params Header parameters
127
+ # @param [Hash] form_params Query parameters
128
+ # @param [Object] body HTTP body (JSON/XML)
129
+ # @return [String] HTTP body data in the form of string
130
+ def build_request_body(header_params, form_params, body)
131
+ # http form
132
+ if header_params['Content-Type'] == 'application/x-www-form-urlencoded'
133
+ data = URI.encode_www_form(form_params)
134
+ elsif header_params['Content-Type'] == 'multipart/form-data'
135
+ data = {}
136
+ form_params.each do |key, value|
137
+ case value
138
+ when ::File, ::Tempfile
139
+ # TODO hardcode to application/octet-stream, need better way to detect content type
140
+ data[key] = Faraday::FilePart.new(value.path, 'application/octet-stream', value.path)
141
+ when ::Array, nil
142
+ # let Faraday handle Array and nil parameters
143
+ data[key] = value
144
+ else
145
+ data[key] = value.to_s
146
+ end
147
+ end
148
+ elsif body
149
+ data = body.is_a?(String) ? body : body.to_json
150
+ else
151
+ data = nil
152
+ end
153
+ data
154
+ end
155
+
156
+ def download_file(request)
157
+ @stream = []
158
+
159
+ # handle streaming Responses
160
+ request.options.on_data = Proc.new do |chunk, overall_received_bytes|
161
+ @stream << chunk
162
+ end
163
+ end
164
+
165
+ def connection(opts)
166
+ opts[:header_params]['Content-Type'] == 'multipart/form-data' ? connection_multipart : connection_regular
167
+ end
168
+
169
+ def connection_multipart
170
+ @connection_multipart ||= build_connection do |conn|
171
+ conn.request :multipart
172
+ conn.request :url_encoded
173
+ end
174
+ end
175
+
176
+ def connection_regular
177
+ @connection_regular ||= build_connection
178
+ end
179
+
180
+ def build_connection
181
+ Faraday.new(url: config.base_url, ssl: ssl_options) do |conn|
182
+ basic_auth(conn)
183
+ config.configure_middleware(conn)
184
+ yield(conn) if block_given?
185
+ conn.adapter(Faraday.default_adapter)
186
+ end
187
+ end
188
+
189
+ def ssl_options
190
+ {
191
+ ca_file: config.ssl_ca_file,
192
+ verify: config.ssl_verify,
193
+ verify_mode: config.ssl_verify_mode,
194
+ client_cert: config.ssl_client_cert,
195
+ client_key: config.ssl_client_key
196
+ }
197
+ end
198
+
199
+ def basic_auth(conn)
200
+ if config.username && config.password
201
+ if Gem::Version.new(Faraday::VERSION) >= Gem::Version.new('2.0')
202
+ conn.request(:authorization, :basic, config.username, config.password)
203
+ else
204
+ conn.request(:basic_auth, config.username, config.password)
205
+ end
206
+ end
207
+ end
208
+
209
+ # Check if the given MIME is a JSON MIME.
210
+ # JSON MIME examples:
211
+ # application/json
212
+ # application/json; charset=UTF8
213
+ # APPLICATION/JSON
214
+ # */*
215
+ # @param [String] mime MIME
216
+ # @return [Boolean] True if the MIME is application/json
217
+ def json_mime?(mime)
218
+ (mime == '*/*') || !(mime =~ /Application\/.*json(?!p)(;.*)?/i).nil?
219
+ end
220
+
221
+ # Deserialize the response to the given return type.
222
+ #
223
+ # @param [Response] response HTTP response
224
+ # @param [String] return_type some examples: "User", "Array<User>", "Hash<String, Integer>"
225
+ def deserialize(response, return_type)
226
+ body = response.body
227
+
228
+ # handle file downloading - return the File instance processed in request callbacks
229
+ # note that response body is empty when the file is written in chunks in request on_body callback
230
+ if return_type == 'File'
231
+ if @config.return_binary_data == true
232
+ # return byte stream
233
+ encoding = body.encoding
234
+ return @stream.join.force_encoding(encoding)
235
+ else
236
+ # return file instead of binary data
237
+ content_disposition = response.headers['Content-Disposition']
238
+ if content_disposition && content_disposition =~ /filename=/i
239
+ filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
240
+ prefix = sanitize_filename(filename)
241
+ else
242
+ prefix = 'download-'
243
+ end
244
+ prefix = prefix + '-' unless prefix.end_with?('-')
245
+ encoding = body.encoding
246
+ @tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
247
+ @tempfile.write(@stream.join.force_encoding(encoding))
248
+ @tempfile.close
249
+ @config.logger.info "Temp file written to #{@tempfile.path}, please copy the file to a proper folder "\
250
+ "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
251
+ "will be deleted automatically with GC. It's also recommended to delete the temp file "\
252
+ "explicitly with `tempfile.delete`"
253
+ return @tempfile
254
+ end
255
+ end
256
+
257
+ return nil if body.nil? || body.empty?
258
+
259
+ # return response body directly for String return type
260
+ return body if return_type == 'String'
261
+
262
+ # ensuring a default content type
263
+ content_type = response.headers['Content-Type'] || 'application/json'
264
+
265
+ fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
266
+
267
+ begin
268
+ data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
269
+ rescue JSON::ParserError => e
270
+ if %w(String Date Time).include?(return_type)
271
+ data = body
272
+ else
273
+ raise e
274
+ end
275
+ end
276
+
277
+ convert_to_type data, return_type
278
+ end
279
+
280
+ # Convert data to the given return type.
281
+ # @param [Object] data Data to be converted
282
+ # @param [String] return_type Return type
283
+ # @return [Mixed] Data in a particular type
284
+ def convert_to_type(data, return_type)
285
+ return nil if data.nil?
286
+ case return_type
287
+ when 'String'
288
+ data.to_s
289
+ when 'Integer'
290
+ data.to_i
291
+ when 'Float'
292
+ data.to_f
293
+ when 'Boolean'
294
+ data == true
295
+ when 'Time'
296
+ # parse date time (expecting ISO 8601 format)
297
+ Time.parse data
298
+ when 'Date'
299
+ # parse date time (expecting ISO 8601 format)
300
+ Date.parse data
301
+ when 'Object'
302
+ # generic object (usually a Hash), return directly
303
+ data
304
+ when /\AArray<(.+)>\z/
305
+ # e.g. Array<Pet>
306
+ sub_type = $1
307
+ data.map { |item| convert_to_type(item, sub_type) }
308
+ when /\AHash\<String, (.+)\>\z/
309
+ # e.g. Hash<String, Integer>
310
+ sub_type = $1
311
+ {}.tap do |hash|
312
+ data.each { |k, v| hash[k] = convert_to_type(v, sub_type) }
313
+ end
314
+ else
315
+ # models (e.g. Pet) or oneOf
316
+ klass = SnapTrade.const_get(return_type)
317
+ klass.respond_to?(:openapi_one_of) ? klass.build(data) : klass.build_from_hash(data)
318
+ end
319
+ end
320
+
321
+ # Sanitize filename by removing path.
322
+ # e.g. ../../sun.gif becomes sun.gif
323
+ #
324
+ # @param [String] filename the filename to be sanitized
325
+ # @return [String] the sanitized filename
326
+ def sanitize_filename(filename)
327
+ filename.gsub(/.*[\/\\]/, '')
328
+ end
329
+
330
+ def build_request_url(path, opts = {})
331
+ # Add leading and trailing slashes to path
332
+ path = "/#{path}".gsub(/\/+/, '/')
333
+ @config.base_url(opts[:operation]) + path
334
+ end
335
+
336
+ # Update header and query params based on authentication settings.
337
+ #
338
+ # @param [Hash] header_params Header parameters
339
+ # @param [Hash] query_params Query parameters
340
+ # @param [String] auth_names Authentication scheme name
341
+ def update_params_for_auth!(header_params, query_params, auth_names)
342
+ Array(auth_names).each do |auth_name|
343
+ auth_setting = @config.auth_settings[auth_name]
344
+ next unless auth_setting
345
+ case auth_setting[:in]
346
+ when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
347
+ when 'query' then query_params[auth_setting[:key]] = auth_setting[:value]
348
+ else fail ArgumentError, 'Authentication token must be in `query` or `header`'
349
+ end
350
+ end
351
+ end
352
+
353
+ # Sets user agent in HTTP header
354
+ #
355
+ # @param [String] user_agent User agent (e.g. Konfig/ruby/1.0.0)
356
+ def user_agent=(user_agent)
357
+ @user_agent = user_agent
358
+ @default_headers['User-Agent'] = @user_agent
359
+ end
360
+
361
+ # Return Accept header based on an array of accepts provided.
362
+ # @param [Array] accepts array for Accept
363
+ # @return [String] the Accept header (e.g. application/json)
364
+ def select_header_accept(accepts)
365
+ return nil if accepts.nil? || accepts.empty?
366
+ # use JSON when present, otherwise use all of the provided
367
+ json_accept = accepts.find { |s| json_mime?(s) }
368
+ json_accept || accepts.join(',')
369
+ end
370
+
371
+ # Return Content-Type header based on an array of content types provided.
372
+ # @param [Array] content_types array for Content-Type
373
+ # @return [String] the Content-Type header (e.g. application/json)
374
+ def select_header_content_type(content_types)
375
+ # return nil by default
376
+ return if content_types.nil? || content_types.empty?
377
+ # use JSON when present, otherwise use the first one
378
+ json_content_type = content_types.find { |s| json_mime?(s) }
379
+ json_content_type || content_types.first
380
+ end
381
+
382
+ # Convert object (array, hash, object, etc) to JSON string.
383
+ # @param [Object] model object to be converted into JSON string
384
+ # @return [String] JSON string representation of the object
385
+ def object_to_http_body(model)
386
+ return model if model.nil? || model.is_a?(String)
387
+ local_body = nil
388
+ if model.is_a?(Array)
389
+ local_body = model.map { |m| object_to_hash(m) }
390
+ else
391
+ local_body = object_to_hash(model)
392
+ end
393
+ local_body.to_json
394
+ end
395
+
396
+ # Convert object(non-array) to hash.
397
+ # @param [Object] obj object to be converted into JSON string
398
+ # @return [String] JSON string representation of the object
399
+ def object_to_hash(obj)
400
+ if obj.respond_to?(:to_hash)
401
+ obj.to_hash
402
+ else
403
+ obj
404
+ end
405
+ end
406
+
407
+ # Build parameter value according to the given collection format.
408
+ # @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
409
+ def build_collection_param(param, collection_format)
410
+ case collection_format
411
+ when :csv
412
+ param.join(',')
413
+ when :ssv
414
+ param.join(' ')
415
+ when :tsv
416
+ param.join("\t")
417
+ when :pipes
418
+ param.join('|')
419
+ when :multi
420
+ # return the array directly as typhoeus will handle it as expected
421
+ param
422
+ else
423
+ fail "unknown collection format: #{collection_format.inspect}"
424
+ end
425
+ end
426
+ end
427
+ end