webull-openapi-python-sdk 1.0.0__py3-none-any.whl

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 (295) hide show
  1. samples/__init__.py +1 -0
  2. samples/data/__init__.py +1 -0
  3. samples/data/data_client.py +57 -0
  4. samples/data/data_streaming_client.py +86 -0
  5. samples/data/data_streaming_client_async.py +101 -0
  6. samples/trade/__init__.py +0 -0
  7. samples/trade/trade_client.py +163 -0
  8. samples/trade/trade_client_v2.py +181 -0
  9. samples/trade/trade_event_client.py +47 -0
  10. webull/__init__.py +1 -0
  11. webull/core/__init__.py +12 -0
  12. webull/core/auth/__init__.py +0 -0
  13. webull/core/auth/algorithm/__init__.py +0 -0
  14. webull/core/auth/algorithm/sha_hmac1.py +65 -0
  15. webull/core/auth/algorithm/sha_hmac256.py +75 -0
  16. webull/core/auth/composer/__init__.py +0 -0
  17. webull/core/auth/composer/default_signature_composer.py +125 -0
  18. webull/core/auth/credentials.py +46 -0
  19. webull/core/auth/signers/__init__.py +0 -0
  20. webull/core/auth/signers/app_key_signer.py +72 -0
  21. webull/core/auth/signers/signer.py +48 -0
  22. webull/core/auth/signers/signer_factory.py +58 -0
  23. webull/core/cache/__init__.py +225 -0
  24. webull/core/client.py +410 -0
  25. webull/core/common/__init__.py +0 -0
  26. webull/core/common/api_type.py +19 -0
  27. webull/core/common/easy_enum.py +35 -0
  28. webull/core/common/region.py +7 -0
  29. webull/core/compat.py +85 -0
  30. webull/core/context/__init__.py +0 -0
  31. webull/core/context/request_context_holder.py +33 -0
  32. webull/core/data/endpoints.json +22 -0
  33. webull/core/data/retry_config.json +15 -0
  34. webull/core/endpoint/__init__.py +8 -0
  35. webull/core/endpoint/chained_endpoint_resolver.py +57 -0
  36. webull/core/endpoint/default_endpoint_resolver.py +60 -0
  37. webull/core/endpoint/local_config_regional_endpoint_resolver.py +77 -0
  38. webull/core/endpoint/resolver_endpoint_request.py +46 -0
  39. webull/core/endpoint/user_customized_endpoint_resolver.py +55 -0
  40. webull/core/exception/__init__.py +0 -0
  41. webull/core/exception/error_code.py +23 -0
  42. webull/core/exception/error_msg.py +21 -0
  43. webull/core/exception/exceptions.py +53 -0
  44. webull/core/headers.py +57 -0
  45. webull/core/http/__init__.py +0 -0
  46. webull/core/http/initializer/__init__.py +0 -0
  47. webull/core/http/initializer/client_initializer.py +79 -0
  48. webull/core/http/initializer/token/__init__.py +0 -0
  49. webull/core/http/initializer/token/bean/__init__.py +0 -0
  50. webull/core/http/initializer/token/bean/access_token.py +40 -0
  51. webull/core/http/initializer/token/bean/check_token_request.py +44 -0
  52. webull/core/http/initializer/token/bean/create_token_request.py +45 -0
  53. webull/core/http/initializer/token/bean/refresh_token_request.py +44 -0
  54. webull/core/http/initializer/token/token_manager.py +208 -0
  55. webull/core/http/initializer/token/token_operation.py +72 -0
  56. webull/core/http/method_type.py +43 -0
  57. webull/core/http/protocol_type.py +43 -0
  58. webull/core/http/request.py +121 -0
  59. webull/core/http/response.py +166 -0
  60. webull/core/request.py +278 -0
  61. webull/core/retry/__init__.py +0 -0
  62. webull/core/retry/backoff_strategy.py +102 -0
  63. webull/core/retry/retry_condition.py +214 -0
  64. webull/core/retry/retry_policy.py +63 -0
  65. webull/core/retry/retry_policy_context.py +51 -0
  66. webull/core/utils/__init__.py +0 -0
  67. webull/core/utils/common.py +62 -0
  68. webull/core/utils/data.py +25 -0
  69. webull/core/utils/desensitize.py +33 -0
  70. webull/core/utils/validation.py +49 -0
  71. webull/core/vendored/__init__.py +0 -0
  72. webull/core/vendored/requests/__init__.py +94 -0
  73. webull/core/vendored/requests/__version__.py +28 -0
  74. webull/core/vendored/requests/_internal_utils.py +56 -0
  75. webull/core/vendored/requests/adapters.py +539 -0
  76. webull/core/vendored/requests/api.py +166 -0
  77. webull/core/vendored/requests/auth.py +307 -0
  78. webull/core/vendored/requests/certs.py +34 -0
  79. webull/core/vendored/requests/compat.py +85 -0
  80. webull/core/vendored/requests/cookies.py +555 -0
  81. webull/core/vendored/requests/exceptions.py +136 -0
  82. webull/core/vendored/requests/help.py +134 -0
  83. webull/core/vendored/requests/hooks.py +48 -0
  84. webull/core/vendored/requests/models.py +960 -0
  85. webull/core/vendored/requests/packages/__init__.py +17 -0
  86. webull/core/vendored/requests/packages/certifi/__init__.py +17 -0
  87. webull/core/vendored/requests/packages/certifi/__main__.py +16 -0
  88. webull/core/vendored/requests/packages/certifi/cacert.pem +4433 -0
  89. webull/core/vendored/requests/packages/certifi/core.py +51 -0
  90. webull/core/vendored/requests/packages/chardet/__init__.py +53 -0
  91. webull/core/vendored/requests/packages/chardet/big5freq.py +400 -0
  92. webull/core/vendored/requests/packages/chardet/big5prober.py +61 -0
  93. webull/core/vendored/requests/packages/chardet/chardistribution.py +247 -0
  94. webull/core/vendored/requests/packages/chardet/charsetgroupprober.py +120 -0
  95. webull/core/vendored/requests/packages/chardet/charsetprober.py +159 -0
  96. webull/core/vendored/requests/packages/chardet/cli/__init__.py +1 -0
  97. webull/core/vendored/requests/packages/chardet/cli/chardetect.py +99 -0
  98. webull/core/vendored/requests/packages/chardet/codingstatemachine.py +102 -0
  99. webull/core/vendored/requests/packages/chardet/compat.py +48 -0
  100. webull/core/vendored/requests/packages/chardet/cp949prober.py +63 -0
  101. webull/core/vendored/requests/packages/chardet/enums.py +90 -0
  102. webull/core/vendored/requests/packages/chardet/escprober.py +115 -0
  103. webull/core/vendored/requests/packages/chardet/escsm.py +260 -0
  104. webull/core/vendored/requests/packages/chardet/eucjpprober.py +106 -0
  105. webull/core/vendored/requests/packages/chardet/euckrfreq.py +209 -0
  106. webull/core/vendored/requests/packages/chardet/euckrprober.py +61 -0
  107. webull/core/vendored/requests/packages/chardet/euctwfreq.py +401 -0
  108. webull/core/vendored/requests/packages/chardet/euctwprober.py +60 -0
  109. webull/core/vendored/requests/packages/chardet/gb2312freq.py +297 -0
  110. webull/core/vendored/requests/packages/chardet/gb2312prober.py +60 -0
  111. webull/core/vendored/requests/packages/chardet/hebrewprober.py +306 -0
  112. webull/core/vendored/requests/packages/chardet/jisfreq.py +339 -0
  113. webull/core/vendored/requests/packages/chardet/jpcntx.py +247 -0
  114. webull/core/vendored/requests/packages/chardet/langbulgarianmodel.py +242 -0
  115. webull/core/vendored/requests/packages/chardet/langcyrillicmodel.py +347 -0
  116. webull/core/vendored/requests/packages/chardet/langgreekmodel.py +239 -0
  117. webull/core/vendored/requests/packages/chardet/langhebrewmodel.py +214 -0
  118. webull/core/vendored/requests/packages/chardet/langhungarianmodel.py +239 -0
  119. webull/core/vendored/requests/packages/chardet/langthaimodel.py +213 -0
  120. webull/core/vendored/requests/packages/chardet/langturkishmodel.py +207 -0
  121. webull/core/vendored/requests/packages/chardet/latin1prober.py +159 -0
  122. webull/core/vendored/requests/packages/chardet/mbcharsetprober.py +105 -0
  123. webull/core/vendored/requests/packages/chardet/mbcsgroupprober.py +68 -0
  124. webull/core/vendored/requests/packages/chardet/mbcssm.py +586 -0
  125. webull/core/vendored/requests/packages/chardet/sbcharsetprober.py +146 -0
  126. webull/core/vendored/requests/packages/chardet/sbcsgroupprober.py +87 -0
  127. webull/core/vendored/requests/packages/chardet/sjisprober.py +106 -0
  128. webull/core/vendored/requests/packages/chardet/universaldetector.py +300 -0
  129. webull/core/vendored/requests/packages/chardet/utf8prober.py +96 -0
  130. webull/core/vendored/requests/packages/chardet/version.py +23 -0
  131. webull/core/vendored/requests/packages/urllib3/__init__.py +114 -0
  132. webull/core/vendored/requests/packages/urllib3/_collections.py +346 -0
  133. webull/core/vendored/requests/packages/urllib3/connection.py +405 -0
  134. webull/core/vendored/requests/packages/urllib3/connectionpool.py +910 -0
  135. webull/core/vendored/requests/packages/urllib3/contrib/__init__.py +0 -0
  136. webull/core/vendored/requests/packages/urllib3/contrib/_appengine_environ.py +44 -0
  137. webull/core/vendored/requests/packages/urllib3/contrib/_securetransport/__init__.py +0 -0
  138. webull/core/vendored/requests/packages/urllib3/contrib/_securetransport/bindings.py +607 -0
  139. webull/core/vendored/requests/packages/urllib3/contrib/_securetransport/low_level.py +360 -0
  140. webull/core/vendored/requests/packages/urllib3/contrib/appengine.py +303 -0
  141. webull/core/vendored/requests/packages/urllib3/contrib/ntlmpool.py +125 -0
  142. webull/core/vendored/requests/packages/urllib3/contrib/pyopenssl.py +484 -0
  143. webull/core/vendored/requests/packages/urllib3/contrib/securetransport.py +818 -0
  144. webull/core/vendored/requests/packages/urllib3/contrib/socks.py +206 -0
  145. webull/core/vendored/requests/packages/urllib3/exceptions.py +260 -0
  146. webull/core/vendored/requests/packages/urllib3/fields.py +192 -0
  147. webull/core/vendored/requests/packages/urllib3/filepost.py +112 -0
  148. webull/core/vendored/requests/packages/urllib3/packages/__init__.py +19 -0
  149. webull/core/vendored/requests/packages/urllib3/packages/backports/__init__.py +0 -0
  150. webull/core/vendored/requests/packages/urllib3/packages/backports/makefile.py +67 -0
  151. webull/core/vendored/requests/packages/urllib3/packages/ordered_dict.py +273 -0
  152. webull/core/vendored/requests/packages/urllib3/packages/six.py +882 -0
  153. webull/core/vendored/requests/packages/urllib3/packages/socks.py +887 -0
  154. webull/core/vendored/requests/packages/urllib3/packages/ssl_match_hostname/__init__.py +19 -0
  155. webull/core/vendored/requests/packages/urllib3/packages/ssl_match_hostname/_implementation.py +170 -0
  156. webull/core/vendored/requests/packages/urllib3/poolmanager.py +467 -0
  157. webull/core/vendored/requests/packages/urllib3/request.py +164 -0
  158. webull/core/vendored/requests/packages/urllib3/response.py +721 -0
  159. webull/core/vendored/requests/packages/urllib3/util/__init__.py +68 -0
  160. webull/core/vendored/requests/packages/urllib3/util/connection.py +148 -0
  161. webull/core/vendored/requests/packages/urllib3/util/queue.py +35 -0
  162. webull/core/vendored/requests/packages/urllib3/util/request.py +132 -0
  163. webull/core/vendored/requests/packages/urllib3/util/response.py +101 -0
  164. webull/core/vendored/requests/packages/urllib3/util/retry.py +426 -0
  165. webull/core/vendored/requests/packages/urllib3/util/selectors.py +601 -0
  166. webull/core/vendored/requests/packages/urllib3/util/ssl_.py +396 -0
  167. webull/core/vendored/requests/packages/urllib3/util/timeout.py +256 -0
  168. webull/core/vendored/requests/packages/urllib3/util/url.py +252 -0
  169. webull/core/vendored/requests/packages/urllib3/util/wait.py +164 -0
  170. webull/core/vendored/requests/packages.py +28 -0
  171. webull/core/vendored/requests/sessions.py +750 -0
  172. webull/core/vendored/requests/status_codes.py +105 -0
  173. webull/core/vendored/requests/structures.py +119 -0
  174. webull/core/vendored/requests/utils.py +916 -0
  175. webull/core/vendored/six.py +905 -0
  176. webull/data/__init__.py +3 -0
  177. webull/data/common/__init__.py +0 -0
  178. webull/data/common/category.py +26 -0
  179. webull/data/common/connect_ack.py +29 -0
  180. webull/data/common/direction.py +25 -0
  181. webull/data/common/exchange_code.py +33 -0
  182. webull/data/common/exercise_style.py +22 -0
  183. webull/data/common/expiration_cycle.py +26 -0
  184. webull/data/common/instrument_status.py +23 -0
  185. webull/data/common/option_type.py +20 -0
  186. webull/data/common/subscribe_type.py +22 -0
  187. webull/data/common/timespan.py +29 -0
  188. webull/data/data_client.py +35 -0
  189. webull/data/data_streaming_client.py +89 -0
  190. webull/data/internal/__init__.py +0 -0
  191. webull/data/internal/default_retry_policy.py +84 -0
  192. webull/data/internal/exceptions.py +60 -0
  193. webull/data/internal/quotes_client.py +314 -0
  194. webull/data/internal/quotes_decoder.py +40 -0
  195. webull/data/internal/quotes_payload_decoder.py +35 -0
  196. webull/data/internal/quotes_topic.py +36 -0
  197. webull/data/quotes/__init__.py +0 -0
  198. webull/data/quotes/instrument.py +33 -0
  199. webull/data/quotes/market_data.py +187 -0
  200. webull/data/quotes/market_streaming_data.py +66 -0
  201. webull/data/quotes/subscribe/__init__.py +0 -0
  202. webull/data/quotes/subscribe/ask_bid_result.py +49 -0
  203. webull/data/quotes/subscribe/basic_result.py +45 -0
  204. webull/data/quotes/subscribe/broker_result.py +33 -0
  205. webull/data/quotes/subscribe/message_pb2.py +37 -0
  206. webull/data/quotes/subscribe/order_result.py +30 -0
  207. webull/data/quotes/subscribe/payload_type.py +19 -0
  208. webull/data/quotes/subscribe/quote_decoder.py +28 -0
  209. webull/data/quotes/subscribe/quote_result.py +47 -0
  210. webull/data/quotes/subscribe/snapshot_decoder.py +30 -0
  211. webull/data/quotes/subscribe/snapshot_result.py +69 -0
  212. webull/data/quotes/subscribe/tick_decoder.py +29 -0
  213. webull/data/quotes/subscribe/tick_result.py +47 -0
  214. webull/data/request/__init__.py +0 -0
  215. webull/data/request/get_batch_historical_bars_request.py +43 -0
  216. webull/data/request/get_corp_action_request.py +47 -0
  217. webull/data/request/get_eod_bars_request.py +32 -0
  218. webull/data/request/get_historical_bars_request.py +43 -0
  219. webull/data/request/get_instruments_request.py +30 -0
  220. webull/data/request/get_quotes_request.py +35 -0
  221. webull/data/request/get_snapshot_request.py +38 -0
  222. webull/data/request/get_tick_request.py +37 -0
  223. webull/data/request/subscribe_request.py +43 -0
  224. webull/data/request/unsubscribe_request.py +42 -0
  225. webull/trade/__init__.py +2 -0
  226. webull/trade/common/__init__.py +0 -0
  227. webull/trade/common/account_type.py +22 -0
  228. webull/trade/common/category.py +29 -0
  229. webull/trade/common/combo_ticker_type.py +23 -0
  230. webull/trade/common/combo_type.py +31 -0
  231. webull/trade/common/currency.py +24 -0
  232. webull/trade/common/forbid_reason.py +27 -0
  233. webull/trade/common/instrument_type.py +27 -0
  234. webull/trade/common/markets.py +27 -0
  235. webull/trade/common/order_entrust_type.py +21 -0
  236. webull/trade/common/order_side.py +23 -0
  237. webull/trade/common/order_status.py +25 -0
  238. webull/trade/common/order_tif.py +24 -0
  239. webull/trade/common/order_type.py +30 -0
  240. webull/trade/common/trade_policy.py +22 -0
  241. webull/trade/common/trading_date_type.py +24 -0
  242. webull/trade/common/trailing_type.py +23 -0
  243. webull/trade/events/__init__.py +0 -0
  244. webull/trade/events/default_retry_policy.py +64 -0
  245. webull/trade/events/events_pb2.py +43 -0
  246. webull/trade/events/events_pb2_grpc.py +66 -0
  247. webull/trade/events/signature_composer.py +61 -0
  248. webull/trade/events/types.py +21 -0
  249. webull/trade/request/__init__.py +0 -0
  250. webull/trade/request/cancel_order_request.py +28 -0
  251. webull/trade/request/get_account_balance_request.py +28 -0
  252. webull/trade/request/get_account_positions_request.py +30 -0
  253. webull/trade/request/get_account_profile_request.py +26 -0
  254. webull/trade/request/get_app_subscriptions.py +28 -0
  255. webull/trade/request/get_open_orders_request.py +30 -0
  256. webull/trade/request/get_order_detail_request.py +27 -0
  257. webull/trade/request/get_today_orders_request.py +31 -0
  258. webull/trade/request/get_trade_calendar_request.py +30 -0
  259. webull/trade/request/get_trade_instrument_detail_request.py +24 -0
  260. webull/trade/request/get_trade_security_detail_request.py +42 -0
  261. webull/trade/request/get_tradeable_instruments_request.py +27 -0
  262. webull/trade/request/palce_order_request.py +91 -0
  263. webull/trade/request/place_order_request_v2.py +58 -0
  264. webull/trade/request/replace_order_request.py +73 -0
  265. webull/trade/request/replace_order_request_v2.py +38 -0
  266. webull/trade/request/v2/__init__.py +0 -0
  267. webull/trade/request/v2/cancel_option_request.py +28 -0
  268. webull/trade/request/v2/cancel_order_request.py +28 -0
  269. webull/trade/request/v2/get_account_balance_request.py +28 -0
  270. webull/trade/request/v2/get_account_list.py +23 -0
  271. webull/trade/request/v2/get_account_positions_request.py +24 -0
  272. webull/trade/request/v2/get_order_detail_request.py +26 -0
  273. webull/trade/request/v2/get_order_history_request.py +35 -0
  274. webull/trade/request/v2/palce_order_request.py +87 -0
  275. webull/trade/request/v2/place_option_request.py +64 -0
  276. webull/trade/request/v2/preview_option_request.py +28 -0
  277. webull/trade/request/v2/preview_order_request.py +59 -0
  278. webull/trade/request/v2/replace_option_request.py +28 -0
  279. webull/trade/request/v2/replace_order_request.py +57 -0
  280. webull/trade/trade/__init__.py +0 -0
  281. webull/trade/trade/account_info.py +83 -0
  282. webull/trade/trade/order_operation.py +246 -0
  283. webull/trade/trade/trade_calendar.py +37 -0
  284. webull/trade/trade/trade_instrument.py +72 -0
  285. webull/trade/trade/v2/__init__.py +0 -0
  286. webull/trade/trade/v2/account_info_v2.py +55 -0
  287. webull/trade/trade/v2/order_operation_v2.py +206 -0
  288. webull/trade/trade_client.py +43 -0
  289. webull/trade/trade_events_client.py +233 -0
  290. webull_openapi_python_sdk-1.0.0.dist-info/METADATA +28 -0
  291. webull_openapi_python_sdk-1.0.0.dist-info/RECORD +295 -0
  292. webull_openapi_python_sdk-1.0.0.dist-info/WHEEL +5 -0
  293. webull_openapi_python_sdk-1.0.0.dist-info/licenses/LICENSE +202 -0
  294. webull_openapi_python_sdk-1.0.0.dist-info/licenses/NOTICE +56 -0
  295. webull_openapi_python_sdk-1.0.0.dist-info/top_level.txt +2 -0
@@ -0,0 +1,360 @@
1
+ # Copyright 2022 Webull
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """
16
+ Low-level helpers for the SecureTransport bindings.
17
+
18
+ These are Python functions that are not directly related to the high-level APIs
19
+ but are necessary to get them to work. They include a whole bunch of low-level
20
+ CoreFoundation messing about and memory management. The concerns in this module
21
+ are almost entirely about trying to avoid memory leaks and providing
22
+ appropriate and useful assistance to the higher-level code.
23
+ """
24
+ import base64
25
+ import ctypes
26
+ import itertools
27
+ import re
28
+ import os
29
+ import ssl
30
+ import tempfile
31
+
32
+ from .bindings import Security, CoreFoundation, CFConst
33
+
34
+
35
+ # This regular expression is used to grab PEM data out of a PEM bundle.
36
+ _PEM_CERTS_RE = re.compile(
37
+ b"-----BEGIN CERTIFICATE-----\n(.*?)\n-----END CERTIFICATE-----", re.DOTALL
38
+ )
39
+
40
+
41
+ def _cf_data_from_bytes(bytestring):
42
+ """
43
+ Given a bytestring, create a CFData object from it. This CFData object must
44
+ be CFReleased by the caller.
45
+ """
46
+ return CoreFoundation.CFDataCreate(
47
+ CoreFoundation.kCFAllocatorDefault, bytestring, len(bytestring)
48
+ )
49
+
50
+
51
+ def _cf_dictionary_from_tuples(tuples):
52
+ """
53
+ Given a list of Python tuples, create an associated CFDictionary.
54
+ """
55
+ dictionary_size = len(tuples)
56
+
57
+ # We need to get the dictionary keys and values out in the same order.
58
+ keys = (t[0] for t in tuples)
59
+ values = (t[1] for t in tuples)
60
+ cf_keys = (CoreFoundation.CFTypeRef * dictionary_size)(*keys)
61
+ cf_values = (CoreFoundation.CFTypeRef * dictionary_size)(*values)
62
+
63
+ return CoreFoundation.CFDictionaryCreate(
64
+ CoreFoundation.kCFAllocatorDefault,
65
+ cf_keys,
66
+ cf_values,
67
+ dictionary_size,
68
+ CoreFoundation.kCFTypeDictionaryKeyCallBacks,
69
+ CoreFoundation.kCFTypeDictionaryValueCallBacks,
70
+ )
71
+
72
+
73
+ def _cf_string_to_unicode(value):
74
+ """
75
+ Creates a Unicode string from a CFString object. Used entirely for error
76
+ reporting.
77
+
78
+ Yes, it annoys me quite a lot that this function is this complex.
79
+ """
80
+ value_as_void_p = ctypes.cast(value, ctypes.POINTER(ctypes.c_void_p))
81
+
82
+ string = CoreFoundation.CFStringGetCStringPtr(
83
+ value_as_void_p,
84
+ CFConst.kCFStringEncodingUTF8
85
+ )
86
+ if string is None:
87
+ buffer = ctypes.create_string_buffer(1024)
88
+ result = CoreFoundation.CFStringGetCString(
89
+ value_as_void_p,
90
+ buffer,
91
+ 1024,
92
+ CFConst.kCFStringEncodingUTF8
93
+ )
94
+ if not result:
95
+ raise OSError('Error copying C string from CFStringRef')
96
+ string = buffer.value
97
+ if string is not None:
98
+ string = string.decode('utf-8')
99
+ return string
100
+
101
+
102
+ def _assert_no_error(error, exception_class=None):
103
+ """
104
+ Checks the return code and throws an exception if there is an error to
105
+ report
106
+ """
107
+ if error == 0:
108
+ return
109
+
110
+ cf_error_string = Security.SecCopyErrorMessageString(error, None)
111
+ output = _cf_string_to_unicode(cf_error_string)
112
+ CoreFoundation.CFRelease(cf_error_string)
113
+
114
+ if output is None or output == u'':
115
+ output = u'OSStatus %s' % error
116
+
117
+ if exception_class is None:
118
+ exception_class = ssl.SSLError
119
+
120
+ raise exception_class(output)
121
+
122
+
123
+ def _cert_array_from_pem(pem_bundle):
124
+ """
125
+ Given a bundle of certs in PEM format, turns them into a CFArray of certs
126
+ that can be used to validate a cert chain.
127
+ """
128
+ # Normalize the PEM bundle's line endings.
129
+ pem_bundle = pem_bundle.replace(b"\r\n", b"\n")
130
+
131
+ der_certs = [
132
+ base64.b64decode(match.group(1))
133
+ for match in _PEM_CERTS_RE.finditer(pem_bundle)
134
+ ]
135
+ if not der_certs:
136
+ raise ssl.SSLError("No root certificates specified")
137
+
138
+ cert_array = CoreFoundation.CFArrayCreateMutable(
139
+ CoreFoundation.kCFAllocatorDefault,
140
+ 0,
141
+ ctypes.byref(CoreFoundation.kCFTypeArrayCallBacks)
142
+ )
143
+ if not cert_array:
144
+ raise ssl.SSLError("Unable to allocate memory!")
145
+
146
+ try:
147
+ for der_bytes in der_certs:
148
+ certdata = _cf_data_from_bytes(der_bytes)
149
+ if not certdata:
150
+ raise ssl.SSLError("Unable to allocate memory!")
151
+ cert = Security.SecCertificateCreateWithData(
152
+ CoreFoundation.kCFAllocatorDefault, certdata
153
+ )
154
+ CoreFoundation.CFRelease(certdata)
155
+ if not cert:
156
+ raise ssl.SSLError("Unable to build cert object!")
157
+
158
+ CoreFoundation.CFArrayAppendValue(cert_array, cert)
159
+ CoreFoundation.CFRelease(cert)
160
+ except Exception:
161
+ # We need to free the array before the exception bubbles further.
162
+ # We only want to do that if an error occurs: otherwise, the caller
163
+ # should free.
164
+ CoreFoundation.CFRelease(cert_array)
165
+
166
+ return cert_array
167
+
168
+
169
+ def _is_cert(item):
170
+ """
171
+ Returns True if a given CFTypeRef is a certificate.
172
+ """
173
+ expected = Security.SecCertificateGetTypeID()
174
+ return CoreFoundation.CFGetTypeID(item) == expected
175
+
176
+
177
+ def _is_identity(item):
178
+ """
179
+ Returns True if a given CFTypeRef is an identity.
180
+ """
181
+ expected = Security.SecIdentityGetTypeID()
182
+ return CoreFoundation.CFGetTypeID(item) == expected
183
+
184
+
185
+ def _temporary_keychain():
186
+ """
187
+ This function creates a temporary Mac keychain that we can use to work with
188
+ credentials. This keychain uses a one-time password and a temporary file to
189
+ store the data. We expect to have one keychain per socket. The returned
190
+ SecKeychainRef must be freed by the caller, including calling
191
+ SecKeychainDelete.
192
+
193
+ Returns a tuple of the SecKeychainRef and the path to the temporary
194
+ directory that contains it.
195
+ """
196
+ # Unfortunately, SecKeychainCreate requires a path to a keychain. This
197
+ # means we cannot use mkstemp to use a generic temporary file. Instead,
198
+ # we're going to create a temporary directory and a filename to use there.
199
+ # This filename will be 8 random bytes expanded into base64. We also need
200
+ # some random bytes to password-protect the keychain we're creating, so we
201
+ # ask for 40 random bytes.
202
+ random_bytes = os.urandom(40)
203
+ filename = base64.b16encode(random_bytes[:8]).decode('utf-8')
204
+ password = base64.b16encode(random_bytes[8:]) # Must be valid UTF-8
205
+ tempdirectory = tempfile.mkdtemp()
206
+
207
+ keychain_path = os.path.join(tempdirectory, filename).encode('utf-8')
208
+
209
+ # We now want to create the keychain itself.
210
+ keychain = Security.SecKeychainRef()
211
+ status = Security.SecKeychainCreate(
212
+ keychain_path,
213
+ len(password),
214
+ password,
215
+ False,
216
+ None,
217
+ ctypes.byref(keychain)
218
+ )
219
+ _assert_no_error(status)
220
+
221
+ # Having created the keychain, we want to pass it off to the caller.
222
+ return keychain, tempdirectory
223
+
224
+
225
+ def _load_items_from_file(keychain, path):
226
+ """
227
+ Given a single file, loads all the trust objects from it into arrays and
228
+ the keychain.
229
+ Returns a tuple of lists: the first list is a list of identities, the
230
+ second a list of certs.
231
+ """
232
+ certificates = []
233
+ identities = []
234
+ result_array = None
235
+
236
+ with open(path, 'rb') as f:
237
+ raw_filedata = f.read()
238
+
239
+ try:
240
+ filedata = CoreFoundation.CFDataCreate(
241
+ CoreFoundation.kCFAllocatorDefault,
242
+ raw_filedata,
243
+ len(raw_filedata)
244
+ )
245
+ result_array = CoreFoundation.CFArrayRef()
246
+ result = Security.SecItemImport(
247
+ filedata, # cert data
248
+ None, # Filename, leaving it out for now
249
+ None, # What the type of the file is, we don't care
250
+ None, # what's in the file, we don't care
251
+ 0, # import flags
252
+ None, # key params, can include passphrase in the future
253
+ keychain, # The keychain to insert into
254
+ ctypes.byref(result_array) # Results
255
+ )
256
+ _assert_no_error(result)
257
+
258
+ # A CFArray is not very useful to us as an intermediary
259
+ # representation, so we are going to extract the objects we want
260
+ # and then free the array. We don't need to keep hold of keys: the
261
+ # keychain already has them!
262
+ result_count = CoreFoundation.CFArrayGetCount(result_array)
263
+ for index in range(result_count):
264
+ item = CoreFoundation.CFArrayGetValueAtIndex(
265
+ result_array, index
266
+ )
267
+ item = ctypes.cast(item, CoreFoundation.CFTypeRef)
268
+
269
+ if _is_cert(item):
270
+ CoreFoundation.CFRetain(item)
271
+ certificates.append(item)
272
+ elif _is_identity(item):
273
+ CoreFoundation.CFRetain(item)
274
+ identities.append(item)
275
+ finally:
276
+ if result_array:
277
+ CoreFoundation.CFRelease(result_array)
278
+
279
+ CoreFoundation.CFRelease(filedata)
280
+
281
+ return (identities, certificates)
282
+
283
+
284
+ def _load_client_cert_chain(keychain, *paths):
285
+ """
286
+ Load certificates and maybe keys from a number of files. Has the end goal
287
+ of returning a CFArray containing one SecIdentityRef, and then zero or more
288
+ SecCertificateRef objects, suitable for use as a client certificate trust
289
+ chain.
290
+ """
291
+ # Ok, the strategy.
292
+ #
293
+ # This relies on knowing that macOS will not give you a SecIdentityRef
294
+ # unless you have imported a key into a keychain. This is a somewhat
295
+ # artificial limitation of macOS (for example, it doesn't necessarily
296
+ # affect iOS), but there is nothing inside Security.framework that lets you
297
+ # get a SecIdentityRef without having a key in a keychain.
298
+ #
299
+ # So the policy here is we take all the files and iterate them in order.
300
+ # Each one will use SecItemImport to have one or more objects loaded from
301
+ # it. We will also point at a keychain that macOS can use to work with the
302
+ # private key.
303
+ #
304
+ # Once we have all the objects, we'll check what we actually have. If we
305
+ # already have a SecIdentityRef in hand, fab: we'll use that. Otherwise,
306
+ # we'll take the first certificate (which we assume to be our leaf) and
307
+ # ask the keychain to give us a SecIdentityRef with that cert's associated
308
+ # key.
309
+ #
310
+ # We'll then return a CFArray containing the trust chain: one
311
+ # SecIdentityRef and then zero-or-more SecCertificateRef objects. The
312
+ # responsibility for freeing this CFArray will be with the caller. This
313
+ # CFArray must remain alive for the entire connection, so in practice it
314
+ # will be stored with a single SSLSocket, along with the reference to the
315
+ # keychain.
316
+ certificates = []
317
+ identities = []
318
+
319
+ # Filter out bad paths.
320
+ paths = (path for path in paths if path)
321
+
322
+ try:
323
+ for file_path in paths:
324
+ new_identities, new_certs = _load_items_from_file(
325
+ keychain, file_path
326
+ )
327
+ identities.extend(new_identities)
328
+ certificates.extend(new_certs)
329
+
330
+ # Ok, we have everything. The question is: do we have an identity? If
331
+ # not, we want to grab one from the first cert we have.
332
+ if not identities:
333
+ new_identity = Security.SecIdentityRef()
334
+ status = Security.SecIdentityCreateWithCertificate(
335
+ keychain,
336
+ certificates[0],
337
+ ctypes.byref(new_identity)
338
+ )
339
+ _assert_no_error(status)
340
+ identities.append(new_identity)
341
+
342
+ # We now want to release the original certificate, as we no longer
343
+ # need it.
344
+ CoreFoundation.CFRelease(certificates.pop(0))
345
+
346
+ # We now need to build a new CFArray that holds the trust chain.
347
+ trust_chain = CoreFoundation.CFArrayCreateMutable(
348
+ CoreFoundation.kCFAllocatorDefault,
349
+ 0,
350
+ ctypes.byref(CoreFoundation.kCFTypeArrayCallBacks),
351
+ )
352
+ for item in itertools.chain(identities, certificates):
353
+ # ArrayAppendValue does a CFRetain on the item. That's fine,
354
+ # because the finally block will release our other refs to them.
355
+ CoreFoundation.CFArrayAppendValue(trust_chain, item)
356
+
357
+ return trust_chain
358
+ finally:
359
+ for obj in itertools.chain(identities, certificates):
360
+ CoreFoundation.CFRelease(obj)
@@ -0,0 +1,303 @@
1
+ # Copyright 2022 Webull
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """
16
+ This module provides a pool manager that uses Google App Engine's
17
+ `URLFetch Service <https://cloud.google.com/appengine/docs/python/urlfetch>`_.
18
+
19
+ Example usage::
20
+
21
+ from urllib3 import PoolManager
22
+ from urllib3.contrib.appengine import AppEngineManager, is_appengine_sandbox
23
+
24
+ if is_appengine_sandbox():
25
+ # AppEngineManager uses AppEngine's URLFetch API behind the scenes
26
+ http = AppEngineManager()
27
+ else:
28
+ # PoolManager uses a socket-level API behind the scenes
29
+ http = PoolManager()
30
+
31
+ r = http.request('GET', 'https://google.com/')
32
+
33
+ There are `limitations <https://cloud.google.com/appengine/docs/python/\
34
+ urlfetch/#Python_Quotas_and_limits>`_ to the URLFetch service and it may not be
35
+ the best choice for your application. There are three options for using
36
+ urllib3 on Google App Engine:
37
+
38
+ 1. You can use :class:`AppEngineManager` with URLFetch. URLFetch is
39
+ cost-effective in many circumstances as long as your usage is within the
40
+ limitations.
41
+ 2. You can use a normal :class:`~urllib3.PoolManager` by enabling sockets.
42
+ Sockets also have `limitations and restrictions
43
+ <https://cloud.google.com/appengine/docs/python/sockets/\
44
+ #limitations-and-restrictions>`_ and have a lower free quota than URLFetch.
45
+ To use sockets, be sure to specify the following in your ``app.yaml``::
46
+
47
+ env_variables:
48
+ GAE_USE_SOCKETS_HTTPLIB : 'true'
49
+
50
+ 3. If you are using `App Engine Flexible
51
+ <https://cloud.google.com/appengine/docs/flexible/>`_, you can use the standard
52
+ :class:`PoolManager` without any configuration or special environment variables.
53
+ """
54
+
55
+ from __future__ import absolute_import
56
+ import io
57
+ import logging
58
+ import warnings
59
+ from ..packages.six.moves.urllib.parse import urljoin
60
+
61
+ from ..exceptions import (
62
+ HTTPError,
63
+ HTTPWarning,
64
+ MaxRetryError,
65
+ ProtocolError,
66
+ TimeoutError,
67
+ SSLError
68
+ )
69
+
70
+ from ..request import RequestMethods
71
+ from ..response import HTTPResponse
72
+ from ..util.timeout import Timeout
73
+ from ..util.retry import Retry
74
+ from . import _appengine_environ
75
+
76
+ try:
77
+ from google.appengine.api import urlfetch
78
+ except ImportError:
79
+ urlfetch = None
80
+
81
+
82
+ log = logging.getLogger(__name__)
83
+
84
+
85
+ class AppEnginePlatformWarning(HTTPWarning):
86
+ pass
87
+
88
+
89
+ class AppEnginePlatformError(HTTPError):
90
+ pass
91
+
92
+
93
+ class AppEngineManager(RequestMethods):
94
+ """
95
+ Connection manager for Google App Engine sandbox applications.
96
+
97
+ This manager uses the URLFetch service directly instead of using the
98
+ emulated httplib, and is subject to URLFetch limitations as described in
99
+ the App Engine documentation `here
100
+ <https://cloud.google.com/appengine/docs/python/urlfetch>`_.
101
+
102
+ Notably it will raise an :class:`AppEnginePlatformError` if:
103
+ * URLFetch is not available.
104
+ * If you attempt to use this on App Engine Flexible, as full socket
105
+ support is available.
106
+ * If a request size is more than 10 megabytes.
107
+ * If a response size is more than 32 megabtyes.
108
+ * If you use an unsupported request method such as OPTIONS.
109
+
110
+ Beyond those cases, it will raise normal urllib3 errors.
111
+ """
112
+
113
+ def __init__(self, headers=None, retries=None, validate_certificate=True,
114
+ urlfetch_retries=True):
115
+ if not urlfetch:
116
+ raise AppEnginePlatformError(
117
+ "URLFetch is not available in this environment.")
118
+
119
+ if is_prod_appengine_mvms():
120
+ raise AppEnginePlatformError(
121
+ "Use normal urllib3.PoolManager instead of AppEngineManager"
122
+ "on Managed VMs, as using URLFetch is not necessary in "
123
+ "this environment.")
124
+
125
+ warnings.warn(
126
+ "urllib3 is using URLFetch on Google App Engine sandbox instead "
127
+ "of sockets. To use sockets directly instead of URLFetch see "
128
+ "https://urllib3.readthedocs.io/en/latest/reference/urllib3.contrib.html.",
129
+ AppEnginePlatformWarning)
130
+
131
+ RequestMethods.__init__(self, headers)
132
+ self.validate_certificate = validate_certificate
133
+ self.urlfetch_retries = urlfetch_retries
134
+
135
+ self.retries = retries or Retry.DEFAULT
136
+
137
+ def __enter__(self):
138
+ return self
139
+
140
+ def __exit__(self, exc_type, exc_val, exc_tb):
141
+ # Return False to re-raise any potential exceptions
142
+ return False
143
+
144
+ def urlopen(self, method, url, body=None, headers=None,
145
+ retries=None, redirect=True, timeout=Timeout.DEFAULT_TIMEOUT,
146
+ **response_kw):
147
+
148
+ retries = self._get_retries(retries, redirect)
149
+
150
+ try:
151
+ follow_redirects = (
152
+ redirect and
153
+ retries.redirect != 0 and
154
+ retries.total)
155
+ response = urlfetch.fetch(
156
+ url,
157
+ payload=body,
158
+ method=method,
159
+ headers=headers or {},
160
+ allow_truncated=False,
161
+ follow_redirects=self.urlfetch_retries and follow_redirects,
162
+ deadline=self._get_absolute_timeout(timeout),
163
+ validate_certificate=self.validate_certificate,
164
+ )
165
+ except urlfetch.DeadlineExceededError as e:
166
+ raise TimeoutError(self, e)
167
+
168
+ except urlfetch.InvalidURLError as e:
169
+ if 'too large' in str(e):
170
+ raise AppEnginePlatformError(
171
+ "URLFetch request too large, URLFetch only "
172
+ "supports requests up to 10mb in size.", e)
173
+ raise ProtocolError(e)
174
+
175
+ except urlfetch.DownloadError as e:
176
+ if 'Too many redirects' in str(e):
177
+ raise MaxRetryError(self, url, reason=e)
178
+ raise ProtocolError(e)
179
+
180
+ except urlfetch.ResponseTooLargeError as e:
181
+ raise AppEnginePlatformError(
182
+ "URLFetch response too large, URLFetch only supports"
183
+ "responses up to 32mb in size.", e)
184
+
185
+ except urlfetch.SSLCertificateError as e:
186
+ raise SSLError(e)
187
+
188
+ except urlfetch.InvalidMethodError as e:
189
+ raise AppEnginePlatformError(
190
+ "URLFetch does not support method: %s" % method, e)
191
+
192
+ http_response = self._urlfetch_response_to_http_response(
193
+ response, retries=retries, **response_kw)
194
+
195
+ # Handle redirect?
196
+ redirect_location = redirect and http_response.get_redirect_location()
197
+ if redirect_location:
198
+ # Check for redirect response
199
+ if (self.urlfetch_retries and retries.raise_on_redirect):
200
+ raise MaxRetryError(self, url, "too many redirects")
201
+ else:
202
+ if http_response.status == 303:
203
+ method = 'GET'
204
+
205
+ try:
206
+ retries = retries.increment(method, url, response=http_response, _pool=self)
207
+ except MaxRetryError:
208
+ if retries.raise_on_redirect:
209
+ raise MaxRetryError(self, url, "too many redirects")
210
+ return http_response
211
+
212
+ retries.sleep_for_retry(http_response)
213
+ log.debug("Redirecting %s -> %s", url, redirect_location)
214
+ redirect_url = urljoin(url, redirect_location)
215
+ return self.urlopen(
216
+ method, redirect_url, body, headers,
217
+ retries=retries, redirect=redirect,
218
+ timeout=timeout, **response_kw)
219
+
220
+ # Check if we should retry the HTTP response.
221
+ has_retry_after = bool(http_response.getheader('Retry-After'))
222
+ if retries.is_retry(method, http_response.status, has_retry_after):
223
+ retries = retries.increment(
224
+ method, url, response=http_response, _pool=self)
225
+ log.debug("Retry: %s", url)
226
+ retries.sleep(http_response)
227
+ return self.urlopen(
228
+ method, url,
229
+ body=body, headers=headers,
230
+ retries=retries, redirect=redirect,
231
+ timeout=timeout, **response_kw)
232
+
233
+ return http_response
234
+
235
+ def _urlfetch_response_to_http_response(self, urlfetch_resp, **response_kw):
236
+
237
+ if is_prod_appengine():
238
+ # Production GAE handles deflate encoding automatically, but does
239
+ # not remove the encoding header.
240
+ content_encoding = urlfetch_resp.headers.get('content-encoding')
241
+
242
+ if content_encoding == 'deflate':
243
+ del urlfetch_resp.headers['content-encoding']
244
+
245
+ transfer_encoding = urlfetch_resp.headers.get('transfer-encoding')
246
+ # We have a full response's content,
247
+ # so let's make sure we don't report ourselves as chunked data.
248
+ if transfer_encoding == 'chunked':
249
+ encodings = transfer_encoding.split(",")
250
+ encodings.remove('chunked')
251
+ urlfetch_resp.headers['transfer-encoding'] = ','.join(encodings)
252
+
253
+ original_response = HTTPResponse(
254
+ # In order for decoding to work, we must present the content as
255
+ # a file-like object.
256
+ body=io.BytesIO(urlfetch_resp.content),
257
+ msg=urlfetch_resp.header_msg,
258
+ headers=urlfetch_resp.headers,
259
+ status=urlfetch_resp.status_code,
260
+ **response_kw
261
+ )
262
+
263
+ return HTTPResponse(
264
+ body=io.BytesIO(urlfetch_resp.content),
265
+ headers=urlfetch_resp.headers,
266
+ status=urlfetch_resp.status_code,
267
+ original_response=original_response,
268
+ **response_kw
269
+ )
270
+
271
+ def _get_absolute_timeout(self, timeout):
272
+ if timeout is Timeout.DEFAULT_TIMEOUT:
273
+ return None # Defer to URLFetch's default.
274
+ if isinstance(timeout, Timeout):
275
+ if timeout._read is not None or timeout._connect is not None:
276
+ warnings.warn(
277
+ "URLFetch does not support granular timeout settings, "
278
+ "reverting to total or default URLFetch timeout.",
279
+ AppEnginePlatformWarning)
280
+ return timeout.total
281
+ return timeout
282
+
283
+ def _get_retries(self, retries, redirect):
284
+ if not isinstance(retries, Retry):
285
+ retries = Retry.from_int(
286
+ retries, redirect=redirect, default=self.retries)
287
+
288
+ if retries.connect or retries.read or retries.redirect:
289
+ warnings.warn(
290
+ "URLFetch only supports total retries and does not "
291
+ "recognize connect, read, or redirect retry parameters.",
292
+ AppEnginePlatformWarning)
293
+
294
+ return retries
295
+
296
+
297
+ # Alias methods from _appengine_environ to maintain public API interface.
298
+
299
+ is_appengine = _appengine_environ.is_appengine
300
+ is_appengine_sandbox = _appengine_environ.is_appengine_sandbox
301
+ is_local_appengine = _appengine_environ.is_local_appengine
302
+ is_prod_appengine = _appengine_environ.is_prod_appengine
303
+ is_prod_appengine_mvms = _appengine_environ.is_prod_appengine_mvms