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,555 @@
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
+ # -*- coding: utf-8 -*-
16
+
17
+ """
18
+ requests.cookies
19
+ ~~~~~~~~~~~~~~~~
20
+
21
+ Compatibility code to be able to use `cookielib.CookieJar` with requests.
22
+
23
+ requests.utils imports from here, so be careful with imports.
24
+ """
25
+
26
+ import copy
27
+ import time
28
+ import calendar
29
+
30
+ from ._internal_utils import to_native_string
31
+ from .compat import cookielib, urlparse, urlunparse, Morsel, MutableMapping
32
+
33
+ try:
34
+ import threading
35
+ except ImportError:
36
+ import dummy_threading as threading
37
+
38
+
39
+ class MockRequest(object):
40
+ """Wraps a `requests.Request` to mimic a `urllib2.Request`.
41
+
42
+ The code in `cookielib.CookieJar` expects this interface in order to correctly
43
+ manage cookie policies, i.e., determine whether a cookie can be set, given the
44
+ domains of the request and the cookie.
45
+
46
+ The original request object is read-only. The client is responsible for collecting
47
+ the new headers via `get_new_headers()` and interpreting them appropriately. You
48
+ probably want `get_cookie_header`, defined below.
49
+ """
50
+
51
+ def __init__(self, request):
52
+ self._r = request
53
+ self._new_headers = {}
54
+ self.type = urlparse(self._r.url).scheme
55
+
56
+ def get_type(self):
57
+ return self.type
58
+
59
+ def get_host(self):
60
+ return urlparse(self._r.url).netloc
61
+
62
+ def get_origin_req_host(self):
63
+ return self.get_host()
64
+
65
+ def get_full_url(self):
66
+ # Only return the response's URL if the user hadn't set the Host
67
+ # header
68
+ if not self._r.headers.get('Host'):
69
+ return self._r.url
70
+ # If they did set it, retrieve it and reconstruct the expected domain
71
+ host = to_native_string(self._r.headers['Host'], encoding='utf-8')
72
+ parsed = urlparse(self._r.url)
73
+ # Reconstruct the URL as we expect it
74
+ return urlunparse([
75
+ parsed.scheme, host, parsed.path, parsed.params, parsed.query,
76
+ parsed.fragment
77
+ ])
78
+
79
+ def is_unverifiable(self):
80
+ return True
81
+
82
+ def has_header(self, name):
83
+ return name in self._r.headers or name in self._new_headers
84
+
85
+ def get_header(self, name, default=None):
86
+ return self._r.headers.get(name, self._new_headers.get(name, default))
87
+
88
+ def add_header(self, key, val):
89
+ """cookielib has no legitimate use for this method; add it back if you find one."""
90
+ raise NotImplementedError("Cookie headers should be added with add_unredirected_header()")
91
+
92
+ def add_unredirected_header(self, name, value):
93
+ self._new_headers[name] = value
94
+
95
+ def get_new_headers(self):
96
+ return self._new_headers
97
+
98
+ @property
99
+ def unverifiable(self):
100
+ return self.is_unverifiable()
101
+
102
+ @property
103
+ def origin_req_host(self):
104
+ return self.get_origin_req_host()
105
+
106
+ @property
107
+ def host(self):
108
+ return self.get_host()
109
+
110
+
111
+ class MockResponse(object):
112
+ """Wraps a `httplib.HTTPMessage` to mimic a `urllib.addinfourl`.
113
+
114
+ ...what? Basically, expose the parsed HTTP headers from the server response
115
+ the way `cookielib` expects to see them.
116
+ """
117
+
118
+ def __init__(self, headers):
119
+ """Make a MockResponse for `cookielib` to read.
120
+
121
+ :param headers: a httplib.HTTPMessage or analogous carrying the headers
122
+ """
123
+ self._headers = headers
124
+
125
+ def info(self):
126
+ return self._headers
127
+
128
+ def getheaders(self, name):
129
+ self._headers.getheaders(name)
130
+
131
+
132
+ def extract_cookies_to_jar(jar, request, response):
133
+ """Extract the cookies from the response into a CookieJar.
134
+
135
+ :param jar: cookielib.CookieJar (not necessarily a RequestsCookieJar)
136
+ :param request: our own requests.Request object
137
+ :param response: urllib3.HTTPResponse object
138
+ """
139
+ if not (hasattr(response, '_original_response') and
140
+ response._original_response):
141
+ return
142
+ # the _original_response field is the wrapped httplib.HTTPResponse object,
143
+ req = MockRequest(request)
144
+ # pull out the HTTPMessage with the headers and put it in the mock:
145
+ res = MockResponse(response._original_response.msg)
146
+ jar.extract_cookies(res, req)
147
+
148
+
149
+ def get_cookie_header(jar, request):
150
+ """
151
+ Produce an appropriate Cookie header string to be sent with `request`, or None.
152
+
153
+ :rtype: str
154
+ """
155
+ r = MockRequest(request)
156
+ jar.add_cookie_header(r)
157
+ return r.get_new_headers().get('Cookie')
158
+
159
+
160
+ def remove_cookie_by_name(cookiejar, name, domain=None, path=None):
161
+ """Unsets a cookie by name, by default over all domains and paths.
162
+
163
+ Wraps CookieJar.clear(), is O(n).
164
+ """
165
+ clearables = []
166
+ for cookie in cookiejar:
167
+ if cookie.name != name:
168
+ continue
169
+ if domain is not None and domain != cookie.domain:
170
+ continue
171
+ if path is not None and path != cookie.path:
172
+ continue
173
+ clearables.append((cookie.domain, cookie.path, cookie.name))
174
+
175
+ for domain, path, name in clearables:
176
+ cookiejar.clear(domain, path, name)
177
+
178
+
179
+ class CookieConflictError(RuntimeError):
180
+ """There are two cookies that meet the criteria specified in the cookie jar.
181
+ Use .get and .set and include domain and path args in order to be more specific.
182
+ """
183
+
184
+
185
+ class RequestsCookieJar(cookielib.CookieJar, MutableMapping):
186
+ """Compatibility class; is a cookielib.CookieJar, but exposes a dict
187
+ interface.
188
+
189
+ This is the CookieJar we create by default for requests and sessions that
190
+ don't specify one, since some clients may expect response.cookies and
191
+ session.cookies to support dict operations.
192
+
193
+ Requests does not use the dict interface internally; it's just for
194
+ compatibility with external client code. All requests code should work
195
+ out of the box with externally provided instances of ``CookieJar``, e.g.
196
+ ``LWPCookieJar`` and ``FileCookieJar``.
197
+
198
+ Unlike a regular CookieJar, this class is pickleable.
199
+
200
+ .. warning:: dictionary operations that are normally O(1) may be O(n).
201
+ """
202
+
203
+ def get(self, name, default=None, domain=None, path=None):
204
+ """Dict-like get() that also supports optional domain and path args in
205
+ order to resolve naming collisions from using one cookie jar over
206
+ multiple domains.
207
+
208
+ .. warning:: operation is O(n), not O(1).
209
+ """
210
+ try:
211
+ return self._find_no_duplicates(name, domain, path)
212
+ except KeyError:
213
+ return default
214
+
215
+ def set(self, name, value, **kwargs):
216
+ """Dict-like set() that also supports optional domain and path args in
217
+ order to resolve naming collisions from using one cookie jar over
218
+ multiple domains.
219
+ """
220
+ # support client code that unsets cookies by assignment of a None value:
221
+ if value is None:
222
+ remove_cookie_by_name(self, name, domain=kwargs.get('domain'), path=kwargs.get('path'))
223
+ return
224
+
225
+ if isinstance(value, Morsel):
226
+ c = morsel_to_cookie(value)
227
+ else:
228
+ c = create_cookie(name, value, **kwargs)
229
+ self.set_cookie(c)
230
+ return c
231
+
232
+ def iterkeys(self):
233
+ """Dict-like iterkeys() that returns an iterator of names of cookies
234
+ from the jar.
235
+
236
+ .. seealso:: itervalues() and iteritems().
237
+ """
238
+ for cookie in iter(self):
239
+ yield cookie.name
240
+
241
+ def keys(self):
242
+ """Dict-like keys() that returns a list of names of cookies from the
243
+ jar.
244
+
245
+ .. seealso:: values() and items().
246
+ """
247
+ return list(self.iterkeys())
248
+
249
+ def itervalues(self):
250
+ """Dict-like itervalues() that returns an iterator of values of cookies
251
+ from the jar.
252
+
253
+ .. seealso:: iterkeys() and iteritems().
254
+ """
255
+ for cookie in iter(self):
256
+ yield cookie.value
257
+
258
+ def values(self):
259
+ """Dict-like values() that returns a list of values of cookies from the
260
+ jar.
261
+
262
+ .. seealso:: keys() and items().
263
+ """
264
+ return list(self.itervalues())
265
+
266
+ def iteritems(self):
267
+ """Dict-like iteritems() that returns an iterator of name-value tuples
268
+ from the jar.
269
+
270
+ .. seealso:: iterkeys() and itervalues().
271
+ """
272
+ for cookie in iter(self):
273
+ yield cookie.name, cookie.value
274
+
275
+ def items(self):
276
+ """Dict-like items() that returns a list of name-value tuples from the
277
+ jar. Allows client-code to call ``dict(RequestsCookieJar)`` and get a
278
+ vanilla python dict of key value pairs.
279
+
280
+ .. seealso:: keys() and values().
281
+ """
282
+ return list(self.iteritems())
283
+
284
+ def list_domains(self):
285
+ """Utility method to list all the domains in the jar."""
286
+ domains = []
287
+ for cookie in iter(self):
288
+ if cookie.domain not in domains:
289
+ domains.append(cookie.domain)
290
+ return domains
291
+
292
+ def list_paths(self):
293
+ """Utility method to list all the paths in the jar."""
294
+ paths = []
295
+ for cookie in iter(self):
296
+ if cookie.path not in paths:
297
+ paths.append(cookie.path)
298
+ return paths
299
+
300
+ def multiple_domains(self):
301
+ """Returns True if there are multiple domains in the jar.
302
+ Returns False otherwise.
303
+
304
+ :rtype: bool
305
+ """
306
+ domains = []
307
+ for cookie in iter(self):
308
+ if cookie.domain is not None and cookie.domain in domains:
309
+ return True
310
+ domains.append(cookie.domain)
311
+ return False # there is only one domain in jar
312
+
313
+ def get_dict(self, domain=None, path=None):
314
+ """Takes as an argument an optional domain and path and returns a plain
315
+ old Python dict of name-value pairs of cookies that meet the
316
+ requirements.
317
+
318
+ :rtype: dict
319
+ """
320
+ dictionary = {}
321
+ for cookie in iter(self):
322
+ if (
323
+ (domain is None or cookie.domain == domain) and
324
+ (path is None or cookie.path == path)
325
+ ):
326
+ dictionary[cookie.name] = cookie.value
327
+ return dictionary
328
+
329
+ def __contains__(self, name):
330
+ try:
331
+ return super(RequestsCookieJar, self).__contains__(name)
332
+ except CookieConflictError:
333
+ return True
334
+
335
+ def __getitem__(self, name):
336
+ """Dict-like __getitem__() for compatibility with client code. Throws
337
+ exception if there are more than one cookie with name. In that case,
338
+ use the more explicit get() method instead.
339
+
340
+ .. warning:: operation is O(n), not O(1).
341
+ """
342
+ return self._find_no_duplicates(name)
343
+
344
+ def __setitem__(self, name, value):
345
+ """Dict-like __setitem__ for compatibility with client code. Throws
346
+ exception if there is already a cookie of that name in the jar. In that
347
+ case, use the more explicit set() method instead.
348
+ """
349
+ self.set(name, value)
350
+
351
+ def __delitem__(self, name):
352
+ """Deletes a cookie given a name. Wraps ``cookielib.CookieJar``'s
353
+ ``remove_cookie_by_name()``.
354
+ """
355
+ remove_cookie_by_name(self, name)
356
+
357
+ def set_cookie(self, cookie, *args, **kwargs):
358
+ if hasattr(cookie.value, 'startswith') and cookie.value.startswith('"') and cookie.value.endswith('"'):
359
+ cookie.value = cookie.value.replace('\\"', '')
360
+ return super(RequestsCookieJar, self).set_cookie(cookie, *args, **kwargs)
361
+
362
+ def update(self, other):
363
+ """Updates this jar with cookies from another CookieJar or dict-like"""
364
+ if isinstance(other, cookielib.CookieJar):
365
+ for cookie in other:
366
+ self.set_cookie(copy.copy(cookie))
367
+ else:
368
+ super(RequestsCookieJar, self).update(other)
369
+
370
+ def _find(self, name, domain=None, path=None):
371
+ """Requests uses this method internally to get cookie values.
372
+
373
+ If there are conflicting cookies, _find arbitrarily chooses one.
374
+ See _find_no_duplicates if you want an exception thrown if there are
375
+ conflicting cookies.
376
+
377
+ :param name: a string containing name of cookie
378
+ :param domain: (optional) string containing domain of cookie
379
+ :param path: (optional) string containing path of cookie
380
+ :return: cookie.value
381
+ """
382
+ for cookie in iter(self):
383
+ if cookie.name == name:
384
+ if domain is None or cookie.domain == domain:
385
+ if path is None or cookie.path == path:
386
+ return cookie.value
387
+
388
+ raise KeyError('name=%r, domain=%r, path=%r' % (name, domain, path))
389
+
390
+ def _find_no_duplicates(self, name, domain=None, path=None):
391
+ """Both ``__get_item__`` and ``get`` call this function: it's never
392
+ used elsewhere in Requests.
393
+
394
+ :param name: a string containing name of cookie
395
+ :param domain: (optional) string containing domain of cookie
396
+ :param path: (optional) string containing path of cookie
397
+ :raises KeyError: if cookie is not found
398
+ :raises CookieConflictError: if there are multiple cookies
399
+ that match name and optionally domain and path
400
+ :return: cookie.value
401
+ """
402
+ toReturn = None
403
+ for cookie in iter(self):
404
+ if cookie.name == name:
405
+ if domain is None or cookie.domain == domain:
406
+ if path is None or cookie.path == path:
407
+ if toReturn is not None: # if there are multiple cookies that meet passed in criteria
408
+ raise CookieConflictError('There are multiple cookies with name, %r' % (name))
409
+ toReturn = cookie.value # we will eventually return this as long as no cookie conflict
410
+
411
+ if toReturn:
412
+ return toReturn
413
+ raise KeyError('name=%r, domain=%r, path=%r' % (name, domain, path))
414
+
415
+ def __getstate__(self):
416
+ """Unlike a normal CookieJar, this class is pickleable."""
417
+ state = self.__dict__.copy()
418
+ # remove the unpickleable RLock object
419
+ state.pop('_cookies_lock')
420
+ return state
421
+
422
+ def __setstate__(self, state):
423
+ """Unlike a normal CookieJar, this class is pickleable."""
424
+ self.__dict__.update(state)
425
+ if '_cookies_lock' not in self.__dict__:
426
+ self._cookies_lock = threading.RLock()
427
+
428
+ def copy(self):
429
+ """Return a copy of this RequestsCookieJar."""
430
+ new_cj = RequestsCookieJar()
431
+ new_cj.update(self)
432
+ return new_cj
433
+
434
+
435
+ def _copy_cookie_jar(jar):
436
+ if jar is None:
437
+ return None
438
+
439
+ if hasattr(jar, 'copy'):
440
+ # We're dealing with an instance of RequestsCookieJar
441
+ return jar.copy()
442
+ # We're dealing with a generic CookieJar instance
443
+ new_jar = copy.copy(jar)
444
+ new_jar.clear()
445
+ for cookie in jar:
446
+ new_jar.set_cookie(copy.copy(cookie))
447
+ return new_jar
448
+
449
+
450
+ def create_cookie(name, value, **kwargs):
451
+ """Make a cookie from underspecified parameters.
452
+
453
+ By default, the pair of `name` and `value` will be set for the domain ''
454
+ and sent on every request (this is sometimes called a "supercookie").
455
+ """
456
+ result = dict(
457
+ version=0,
458
+ name=name,
459
+ value=value,
460
+ port=None,
461
+ domain='',
462
+ path='/',
463
+ secure=False,
464
+ expires=None,
465
+ discard=True,
466
+ comment=None,
467
+ comment_url=None,
468
+ rest={'HttpOnly': None},
469
+ rfc2109=False,)
470
+
471
+ badargs = set(kwargs) - set(result)
472
+ if badargs:
473
+ err = 'create_cookie() got unexpected keyword arguments: %s'
474
+ raise TypeError(err % list(badargs))
475
+
476
+ result.update(kwargs)
477
+ result['port_specified'] = bool(result['port'])
478
+ result['domain_specified'] = bool(result['domain'])
479
+ result['domain_initial_dot'] = result['domain'].startswith('.')
480
+ result['path_specified'] = bool(result['path'])
481
+
482
+ return cookielib.Cookie(**result)
483
+
484
+
485
+ def morsel_to_cookie(morsel):
486
+ """Convert a Morsel object into a Cookie containing the one k/v pair."""
487
+
488
+ expires = None
489
+ if morsel['max-age']:
490
+ try:
491
+ expires = int(time.time() + int(morsel['max-age']))
492
+ except ValueError:
493
+ raise TypeError('max-age: %s must be integer' % morsel['max-age'])
494
+ elif morsel['expires']:
495
+ time_template = '%a, %d-%b-%Y %H:%M:%S GMT'
496
+ expires = calendar.timegm(
497
+ time.strptime(morsel['expires'], time_template)
498
+ )
499
+ return create_cookie(
500
+ comment=morsel['comment'],
501
+ comment_url=bool(morsel['comment']),
502
+ discard=False,
503
+ domain=morsel['domain'],
504
+ expires=expires,
505
+ name=morsel.key,
506
+ path=morsel['path'],
507
+ port=None,
508
+ rest={'HttpOnly': morsel['httponly']},
509
+ rfc2109=False,
510
+ secure=bool(morsel['secure']),
511
+ value=morsel.value,
512
+ version=morsel['version'] or 0,
513
+ )
514
+
515
+
516
+ def cookiejar_from_dict(cookie_dict, cookiejar=None, overwrite=True):
517
+ """Returns a CookieJar from a key/value dictionary.
518
+
519
+ :param cookie_dict: Dict of key/values to insert into CookieJar.
520
+ :param cookiejar: (optional) A cookiejar to add the cookies to.
521
+ :param overwrite: (optional) If False, will not replace cookies
522
+ already in the jar with new ones.
523
+ """
524
+ if cookiejar is None:
525
+ cookiejar = RequestsCookieJar()
526
+
527
+ if cookie_dict is not None:
528
+ names_from_jar = [cookie.name for cookie in cookiejar]
529
+ for name in cookie_dict:
530
+ if overwrite or (name not in names_from_jar):
531
+ cookiejar.set_cookie(create_cookie(name, cookie_dict[name]))
532
+
533
+ return cookiejar
534
+
535
+
536
+ def merge_cookies(cookiejar, cookies):
537
+ """Add cookies to cookiejar and returns a merged CookieJar.
538
+
539
+ :param cookiejar: CookieJar object to add the cookies to.
540
+ :param cookies: Dictionary or CookieJar object to be added.
541
+ """
542
+ if not isinstance(cookiejar, cookielib.CookieJar):
543
+ raise ValueError('You can only merge into CookieJar')
544
+
545
+ if isinstance(cookies, dict):
546
+ cookiejar = cookiejar_from_dict(
547
+ cookies, cookiejar=cookiejar, overwrite=False)
548
+ elif isinstance(cookies, cookielib.CookieJar):
549
+ try:
550
+ cookiejar.update(cookies)
551
+ except AttributeError:
552
+ for cookie_in_jar in cookies:
553
+ cookiejar.set_cookie(cookie_in_jar)
554
+
555
+ return cookiejar
@@ -0,0 +1,136 @@
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
+ # -*- coding: utf-8 -*-
16
+
17
+ """
18
+ requests.exceptions
19
+ ~~~~~~~~~~~~~~~~~~~
20
+
21
+ This module contains the set of Requests' exceptions.
22
+ """
23
+ from .packages.urllib3.exceptions import HTTPError as BaseHTTPError
24
+
25
+
26
+ class RequestException(IOError):
27
+ """There was an ambiguous exception that occurred while handling your
28
+ request.
29
+ """
30
+
31
+ def __init__(self, *args, **kwargs):
32
+ """Initialize RequestException with `request` and `response` objects."""
33
+ response = kwargs.pop('response', None)
34
+ self.response = response
35
+ self.request = kwargs.pop('request', None)
36
+ if (response is not None and not self.request and
37
+ hasattr(response, 'request')):
38
+ self.request = self.response.request
39
+ super(RequestException, self).__init__(*args, **kwargs)
40
+
41
+
42
+ class HTTPError(RequestException):
43
+ """An HTTP error occurred."""
44
+
45
+
46
+ class ConnectionError(RequestException):
47
+ """A Connection error occurred."""
48
+
49
+
50
+ class ProxyError(ConnectionError):
51
+ """A proxy error occurred."""
52
+
53
+
54
+ class SSLError(ConnectionError):
55
+ """An SSL error occurred."""
56
+
57
+
58
+ class Timeout(RequestException):
59
+ """The request timed out.
60
+
61
+ Catching this error will catch both
62
+ :exc:`~requests.exceptions.ConnectTimeout` and
63
+ :exc:`~requests.exceptions.ReadTimeout` errors.
64
+ """
65
+
66
+
67
+ class ConnectTimeout(ConnectionError, Timeout):
68
+ """The request timed out while trying to connect to the remote server.
69
+
70
+ Requests that produced this error are safe to retry.
71
+ """
72
+
73
+
74
+ class ReadTimeout(Timeout):
75
+ """The server did not send any data in the allotted amount of time."""
76
+
77
+
78
+ class URLRequired(RequestException):
79
+ """A valid URL is required to make a request."""
80
+
81
+
82
+ class TooManyRedirects(RequestException):
83
+ """Too many redirects."""
84
+
85
+
86
+ class MissingSchema(RequestException, ValueError):
87
+ """The URL schema (e.g. http or https) is missing."""
88
+
89
+
90
+ class InvalidSchema(RequestException, ValueError):
91
+ """See defaults.py for valid schemas."""
92
+
93
+
94
+ class InvalidURL(RequestException, ValueError):
95
+ """The URL provided was somehow invalid."""
96
+
97
+
98
+ class InvalidHeader(RequestException, ValueError):
99
+ """The header value provided was somehow invalid."""
100
+
101
+
102
+ class ChunkedEncodingError(RequestException):
103
+ """The server declared chunked encoding but sent an invalid chunk."""
104
+
105
+
106
+ class ContentDecodingError(RequestException, BaseHTTPError):
107
+ """Failed to decode response content"""
108
+
109
+
110
+ class StreamConsumedError(RequestException, TypeError):
111
+ """The content for this response was already consumed"""
112
+
113
+
114
+ class RetryError(RequestException):
115
+ """Custom retries logic failed"""
116
+
117
+
118
+ class UnrewindableBodyError(RequestException):
119
+ """Requests encountered an error when trying to rewind a body"""
120
+
121
+ # Warnings
122
+
123
+
124
+ class RequestsWarning(Warning):
125
+ """Base warning for Requests."""
126
+ pass
127
+
128
+
129
+ class FileModeWarning(RequestsWarning, DeprecationWarning):
130
+ """A file was opened in text mode, but Requests determined its binary length."""
131
+ pass
132
+
133
+
134
+ class RequestsDependencyWarning(RequestsWarning):
135
+ """An imported dependency doesn't match the expected version range."""
136
+ pass