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.
- samples/__init__.py +1 -0
- samples/data/__init__.py +1 -0
- samples/data/data_client.py +57 -0
- samples/data/data_streaming_client.py +86 -0
- samples/data/data_streaming_client_async.py +101 -0
- samples/trade/__init__.py +0 -0
- samples/trade/trade_client.py +163 -0
- samples/trade/trade_client_v2.py +181 -0
- samples/trade/trade_event_client.py +47 -0
- webull/__init__.py +1 -0
- webull/core/__init__.py +12 -0
- webull/core/auth/__init__.py +0 -0
- webull/core/auth/algorithm/__init__.py +0 -0
- webull/core/auth/algorithm/sha_hmac1.py +65 -0
- webull/core/auth/algorithm/sha_hmac256.py +75 -0
- webull/core/auth/composer/__init__.py +0 -0
- webull/core/auth/composer/default_signature_composer.py +125 -0
- webull/core/auth/credentials.py +46 -0
- webull/core/auth/signers/__init__.py +0 -0
- webull/core/auth/signers/app_key_signer.py +72 -0
- webull/core/auth/signers/signer.py +48 -0
- webull/core/auth/signers/signer_factory.py +58 -0
- webull/core/cache/__init__.py +225 -0
- webull/core/client.py +410 -0
- webull/core/common/__init__.py +0 -0
- webull/core/common/api_type.py +19 -0
- webull/core/common/easy_enum.py +35 -0
- webull/core/common/region.py +7 -0
- webull/core/compat.py +85 -0
- webull/core/context/__init__.py +0 -0
- webull/core/context/request_context_holder.py +33 -0
- webull/core/data/endpoints.json +22 -0
- webull/core/data/retry_config.json +15 -0
- webull/core/endpoint/__init__.py +8 -0
- webull/core/endpoint/chained_endpoint_resolver.py +57 -0
- webull/core/endpoint/default_endpoint_resolver.py +60 -0
- webull/core/endpoint/local_config_regional_endpoint_resolver.py +77 -0
- webull/core/endpoint/resolver_endpoint_request.py +46 -0
- webull/core/endpoint/user_customized_endpoint_resolver.py +55 -0
- webull/core/exception/__init__.py +0 -0
- webull/core/exception/error_code.py +23 -0
- webull/core/exception/error_msg.py +21 -0
- webull/core/exception/exceptions.py +53 -0
- webull/core/headers.py +57 -0
- webull/core/http/__init__.py +0 -0
- webull/core/http/initializer/__init__.py +0 -0
- webull/core/http/initializer/client_initializer.py +79 -0
- webull/core/http/initializer/token/__init__.py +0 -0
- webull/core/http/initializer/token/bean/__init__.py +0 -0
- webull/core/http/initializer/token/bean/access_token.py +40 -0
- webull/core/http/initializer/token/bean/check_token_request.py +44 -0
- webull/core/http/initializer/token/bean/create_token_request.py +45 -0
- webull/core/http/initializer/token/bean/refresh_token_request.py +44 -0
- webull/core/http/initializer/token/token_manager.py +208 -0
- webull/core/http/initializer/token/token_operation.py +72 -0
- webull/core/http/method_type.py +43 -0
- webull/core/http/protocol_type.py +43 -0
- webull/core/http/request.py +121 -0
- webull/core/http/response.py +166 -0
- webull/core/request.py +278 -0
- webull/core/retry/__init__.py +0 -0
- webull/core/retry/backoff_strategy.py +102 -0
- webull/core/retry/retry_condition.py +214 -0
- webull/core/retry/retry_policy.py +63 -0
- webull/core/retry/retry_policy_context.py +51 -0
- webull/core/utils/__init__.py +0 -0
- webull/core/utils/common.py +62 -0
- webull/core/utils/data.py +25 -0
- webull/core/utils/desensitize.py +33 -0
- webull/core/utils/validation.py +49 -0
- webull/core/vendored/__init__.py +0 -0
- webull/core/vendored/requests/__init__.py +94 -0
- webull/core/vendored/requests/__version__.py +28 -0
- webull/core/vendored/requests/_internal_utils.py +56 -0
- webull/core/vendored/requests/adapters.py +539 -0
- webull/core/vendored/requests/api.py +166 -0
- webull/core/vendored/requests/auth.py +307 -0
- webull/core/vendored/requests/certs.py +34 -0
- webull/core/vendored/requests/compat.py +85 -0
- webull/core/vendored/requests/cookies.py +555 -0
- webull/core/vendored/requests/exceptions.py +136 -0
- webull/core/vendored/requests/help.py +134 -0
- webull/core/vendored/requests/hooks.py +48 -0
- webull/core/vendored/requests/models.py +960 -0
- webull/core/vendored/requests/packages/__init__.py +17 -0
- webull/core/vendored/requests/packages/certifi/__init__.py +17 -0
- webull/core/vendored/requests/packages/certifi/__main__.py +16 -0
- webull/core/vendored/requests/packages/certifi/cacert.pem +4433 -0
- webull/core/vendored/requests/packages/certifi/core.py +51 -0
- webull/core/vendored/requests/packages/chardet/__init__.py +53 -0
- webull/core/vendored/requests/packages/chardet/big5freq.py +400 -0
- webull/core/vendored/requests/packages/chardet/big5prober.py +61 -0
- webull/core/vendored/requests/packages/chardet/chardistribution.py +247 -0
- webull/core/vendored/requests/packages/chardet/charsetgroupprober.py +120 -0
- webull/core/vendored/requests/packages/chardet/charsetprober.py +159 -0
- webull/core/vendored/requests/packages/chardet/cli/__init__.py +1 -0
- webull/core/vendored/requests/packages/chardet/cli/chardetect.py +99 -0
- webull/core/vendored/requests/packages/chardet/codingstatemachine.py +102 -0
- webull/core/vendored/requests/packages/chardet/compat.py +48 -0
- webull/core/vendored/requests/packages/chardet/cp949prober.py +63 -0
- webull/core/vendored/requests/packages/chardet/enums.py +90 -0
- webull/core/vendored/requests/packages/chardet/escprober.py +115 -0
- webull/core/vendored/requests/packages/chardet/escsm.py +260 -0
- webull/core/vendored/requests/packages/chardet/eucjpprober.py +106 -0
- webull/core/vendored/requests/packages/chardet/euckrfreq.py +209 -0
- webull/core/vendored/requests/packages/chardet/euckrprober.py +61 -0
- webull/core/vendored/requests/packages/chardet/euctwfreq.py +401 -0
- webull/core/vendored/requests/packages/chardet/euctwprober.py +60 -0
- webull/core/vendored/requests/packages/chardet/gb2312freq.py +297 -0
- webull/core/vendored/requests/packages/chardet/gb2312prober.py +60 -0
- webull/core/vendored/requests/packages/chardet/hebrewprober.py +306 -0
- webull/core/vendored/requests/packages/chardet/jisfreq.py +339 -0
- webull/core/vendored/requests/packages/chardet/jpcntx.py +247 -0
- webull/core/vendored/requests/packages/chardet/langbulgarianmodel.py +242 -0
- webull/core/vendored/requests/packages/chardet/langcyrillicmodel.py +347 -0
- webull/core/vendored/requests/packages/chardet/langgreekmodel.py +239 -0
- webull/core/vendored/requests/packages/chardet/langhebrewmodel.py +214 -0
- webull/core/vendored/requests/packages/chardet/langhungarianmodel.py +239 -0
- webull/core/vendored/requests/packages/chardet/langthaimodel.py +213 -0
- webull/core/vendored/requests/packages/chardet/langturkishmodel.py +207 -0
- webull/core/vendored/requests/packages/chardet/latin1prober.py +159 -0
- webull/core/vendored/requests/packages/chardet/mbcharsetprober.py +105 -0
- webull/core/vendored/requests/packages/chardet/mbcsgroupprober.py +68 -0
- webull/core/vendored/requests/packages/chardet/mbcssm.py +586 -0
- webull/core/vendored/requests/packages/chardet/sbcharsetprober.py +146 -0
- webull/core/vendored/requests/packages/chardet/sbcsgroupprober.py +87 -0
- webull/core/vendored/requests/packages/chardet/sjisprober.py +106 -0
- webull/core/vendored/requests/packages/chardet/universaldetector.py +300 -0
- webull/core/vendored/requests/packages/chardet/utf8prober.py +96 -0
- webull/core/vendored/requests/packages/chardet/version.py +23 -0
- webull/core/vendored/requests/packages/urllib3/__init__.py +114 -0
- webull/core/vendored/requests/packages/urllib3/_collections.py +346 -0
- webull/core/vendored/requests/packages/urllib3/connection.py +405 -0
- webull/core/vendored/requests/packages/urllib3/connectionpool.py +910 -0
- webull/core/vendored/requests/packages/urllib3/contrib/__init__.py +0 -0
- webull/core/vendored/requests/packages/urllib3/contrib/_appengine_environ.py +44 -0
- webull/core/vendored/requests/packages/urllib3/contrib/_securetransport/__init__.py +0 -0
- webull/core/vendored/requests/packages/urllib3/contrib/_securetransport/bindings.py +607 -0
- webull/core/vendored/requests/packages/urllib3/contrib/_securetransport/low_level.py +360 -0
- webull/core/vendored/requests/packages/urllib3/contrib/appengine.py +303 -0
- webull/core/vendored/requests/packages/urllib3/contrib/ntlmpool.py +125 -0
- webull/core/vendored/requests/packages/urllib3/contrib/pyopenssl.py +484 -0
- webull/core/vendored/requests/packages/urllib3/contrib/securetransport.py +818 -0
- webull/core/vendored/requests/packages/urllib3/contrib/socks.py +206 -0
- webull/core/vendored/requests/packages/urllib3/exceptions.py +260 -0
- webull/core/vendored/requests/packages/urllib3/fields.py +192 -0
- webull/core/vendored/requests/packages/urllib3/filepost.py +112 -0
- webull/core/vendored/requests/packages/urllib3/packages/__init__.py +19 -0
- webull/core/vendored/requests/packages/urllib3/packages/backports/__init__.py +0 -0
- webull/core/vendored/requests/packages/urllib3/packages/backports/makefile.py +67 -0
- webull/core/vendored/requests/packages/urllib3/packages/ordered_dict.py +273 -0
- webull/core/vendored/requests/packages/urllib3/packages/six.py +882 -0
- webull/core/vendored/requests/packages/urllib3/packages/socks.py +887 -0
- webull/core/vendored/requests/packages/urllib3/packages/ssl_match_hostname/__init__.py +19 -0
- webull/core/vendored/requests/packages/urllib3/packages/ssl_match_hostname/_implementation.py +170 -0
- webull/core/vendored/requests/packages/urllib3/poolmanager.py +467 -0
- webull/core/vendored/requests/packages/urllib3/request.py +164 -0
- webull/core/vendored/requests/packages/urllib3/response.py +721 -0
- webull/core/vendored/requests/packages/urllib3/util/__init__.py +68 -0
- webull/core/vendored/requests/packages/urllib3/util/connection.py +148 -0
- webull/core/vendored/requests/packages/urllib3/util/queue.py +35 -0
- webull/core/vendored/requests/packages/urllib3/util/request.py +132 -0
- webull/core/vendored/requests/packages/urllib3/util/response.py +101 -0
- webull/core/vendored/requests/packages/urllib3/util/retry.py +426 -0
- webull/core/vendored/requests/packages/urllib3/util/selectors.py +601 -0
- webull/core/vendored/requests/packages/urllib3/util/ssl_.py +396 -0
- webull/core/vendored/requests/packages/urllib3/util/timeout.py +256 -0
- webull/core/vendored/requests/packages/urllib3/util/url.py +252 -0
- webull/core/vendored/requests/packages/urllib3/util/wait.py +164 -0
- webull/core/vendored/requests/packages.py +28 -0
- webull/core/vendored/requests/sessions.py +750 -0
- webull/core/vendored/requests/status_codes.py +105 -0
- webull/core/vendored/requests/structures.py +119 -0
- webull/core/vendored/requests/utils.py +916 -0
- webull/core/vendored/six.py +905 -0
- webull/data/__init__.py +3 -0
- webull/data/common/__init__.py +0 -0
- webull/data/common/category.py +26 -0
- webull/data/common/connect_ack.py +29 -0
- webull/data/common/direction.py +25 -0
- webull/data/common/exchange_code.py +33 -0
- webull/data/common/exercise_style.py +22 -0
- webull/data/common/expiration_cycle.py +26 -0
- webull/data/common/instrument_status.py +23 -0
- webull/data/common/option_type.py +20 -0
- webull/data/common/subscribe_type.py +22 -0
- webull/data/common/timespan.py +29 -0
- webull/data/data_client.py +35 -0
- webull/data/data_streaming_client.py +89 -0
- webull/data/internal/__init__.py +0 -0
- webull/data/internal/default_retry_policy.py +84 -0
- webull/data/internal/exceptions.py +60 -0
- webull/data/internal/quotes_client.py +314 -0
- webull/data/internal/quotes_decoder.py +40 -0
- webull/data/internal/quotes_payload_decoder.py +35 -0
- webull/data/internal/quotes_topic.py +36 -0
- webull/data/quotes/__init__.py +0 -0
- webull/data/quotes/instrument.py +33 -0
- webull/data/quotes/market_data.py +187 -0
- webull/data/quotes/market_streaming_data.py +66 -0
- webull/data/quotes/subscribe/__init__.py +0 -0
- webull/data/quotes/subscribe/ask_bid_result.py +49 -0
- webull/data/quotes/subscribe/basic_result.py +45 -0
- webull/data/quotes/subscribe/broker_result.py +33 -0
- webull/data/quotes/subscribe/message_pb2.py +37 -0
- webull/data/quotes/subscribe/order_result.py +30 -0
- webull/data/quotes/subscribe/payload_type.py +19 -0
- webull/data/quotes/subscribe/quote_decoder.py +28 -0
- webull/data/quotes/subscribe/quote_result.py +47 -0
- webull/data/quotes/subscribe/snapshot_decoder.py +30 -0
- webull/data/quotes/subscribe/snapshot_result.py +69 -0
- webull/data/quotes/subscribe/tick_decoder.py +29 -0
- webull/data/quotes/subscribe/tick_result.py +47 -0
- webull/data/request/__init__.py +0 -0
- webull/data/request/get_batch_historical_bars_request.py +43 -0
- webull/data/request/get_corp_action_request.py +47 -0
- webull/data/request/get_eod_bars_request.py +32 -0
- webull/data/request/get_historical_bars_request.py +43 -0
- webull/data/request/get_instruments_request.py +30 -0
- webull/data/request/get_quotes_request.py +35 -0
- webull/data/request/get_snapshot_request.py +38 -0
- webull/data/request/get_tick_request.py +37 -0
- webull/data/request/subscribe_request.py +43 -0
- webull/data/request/unsubscribe_request.py +42 -0
- webull/trade/__init__.py +2 -0
- webull/trade/common/__init__.py +0 -0
- webull/trade/common/account_type.py +22 -0
- webull/trade/common/category.py +29 -0
- webull/trade/common/combo_ticker_type.py +23 -0
- webull/trade/common/combo_type.py +31 -0
- webull/trade/common/currency.py +24 -0
- webull/trade/common/forbid_reason.py +27 -0
- webull/trade/common/instrument_type.py +27 -0
- webull/trade/common/markets.py +27 -0
- webull/trade/common/order_entrust_type.py +21 -0
- webull/trade/common/order_side.py +23 -0
- webull/trade/common/order_status.py +25 -0
- webull/trade/common/order_tif.py +24 -0
- webull/trade/common/order_type.py +30 -0
- webull/trade/common/trade_policy.py +22 -0
- webull/trade/common/trading_date_type.py +24 -0
- webull/trade/common/trailing_type.py +23 -0
- webull/trade/events/__init__.py +0 -0
- webull/trade/events/default_retry_policy.py +64 -0
- webull/trade/events/events_pb2.py +43 -0
- webull/trade/events/events_pb2_grpc.py +66 -0
- webull/trade/events/signature_composer.py +61 -0
- webull/trade/events/types.py +21 -0
- webull/trade/request/__init__.py +0 -0
- webull/trade/request/cancel_order_request.py +28 -0
- webull/trade/request/get_account_balance_request.py +28 -0
- webull/trade/request/get_account_positions_request.py +30 -0
- webull/trade/request/get_account_profile_request.py +26 -0
- webull/trade/request/get_app_subscriptions.py +28 -0
- webull/trade/request/get_open_orders_request.py +30 -0
- webull/trade/request/get_order_detail_request.py +27 -0
- webull/trade/request/get_today_orders_request.py +31 -0
- webull/trade/request/get_trade_calendar_request.py +30 -0
- webull/trade/request/get_trade_instrument_detail_request.py +24 -0
- webull/trade/request/get_trade_security_detail_request.py +42 -0
- webull/trade/request/get_tradeable_instruments_request.py +27 -0
- webull/trade/request/palce_order_request.py +91 -0
- webull/trade/request/place_order_request_v2.py +58 -0
- webull/trade/request/replace_order_request.py +73 -0
- webull/trade/request/replace_order_request_v2.py +38 -0
- webull/trade/request/v2/__init__.py +0 -0
- webull/trade/request/v2/cancel_option_request.py +28 -0
- webull/trade/request/v2/cancel_order_request.py +28 -0
- webull/trade/request/v2/get_account_balance_request.py +28 -0
- webull/trade/request/v2/get_account_list.py +23 -0
- webull/trade/request/v2/get_account_positions_request.py +24 -0
- webull/trade/request/v2/get_order_detail_request.py +26 -0
- webull/trade/request/v2/get_order_history_request.py +35 -0
- webull/trade/request/v2/palce_order_request.py +87 -0
- webull/trade/request/v2/place_option_request.py +64 -0
- webull/trade/request/v2/preview_option_request.py +28 -0
- webull/trade/request/v2/preview_order_request.py +59 -0
- webull/trade/request/v2/replace_option_request.py +28 -0
- webull/trade/request/v2/replace_order_request.py +57 -0
- webull/trade/trade/__init__.py +0 -0
- webull/trade/trade/account_info.py +83 -0
- webull/trade/trade/order_operation.py +246 -0
- webull/trade/trade/trade_calendar.py +37 -0
- webull/trade/trade/trade_instrument.py +72 -0
- webull/trade/trade/v2/__init__.py +0 -0
- webull/trade/trade/v2/account_info_v2.py +55 -0
- webull/trade/trade/v2/order_operation_v2.py +206 -0
- webull/trade/trade_client.py +43 -0
- webull/trade/trade_events_client.py +233 -0
- webull_openapi_python_sdk-1.0.0.dist-info/METADATA +28 -0
- webull_openapi_python_sdk-1.0.0.dist-info/RECORD +295 -0
- webull_openapi_python_sdk-1.0.0.dist-info/WHEEL +5 -0
- webull_openapi_python_sdk-1.0.0.dist-info/licenses/LICENSE +202 -0
- webull_openapi_python_sdk-1.0.0.dist-info/licenses/NOTICE +56 -0
- webull_openapi_python_sdk-1.0.0.dist-info/top_level.txt +2 -0
|
@@ -0,0 +1,206 @@
|
|
|
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
|
+
This module contains provisional support for SOCKS proxies from within
|
|
18
|
+
urllib3. This module supports SOCKS4 (specifically the SOCKS4A variant) and
|
|
19
|
+
SOCKS5. To enable its functionality, either install PySocks or install this
|
|
20
|
+
module with the ``socks`` extra.
|
|
21
|
+
|
|
22
|
+
The SOCKS implementation supports the full range of urllib3 features. It also
|
|
23
|
+
supports the following SOCKS features:
|
|
24
|
+
|
|
25
|
+
- SOCKS4
|
|
26
|
+
- SOCKS4a
|
|
27
|
+
- SOCKS5
|
|
28
|
+
- Usernames and passwords for the SOCKS proxy
|
|
29
|
+
|
|
30
|
+
Known Limitations:
|
|
31
|
+
|
|
32
|
+
- Currently PySocks does not support contacting remote websites via literal
|
|
33
|
+
IPv6 addresses. Any such connection attempt will fail. You must use a domain
|
|
34
|
+
name.
|
|
35
|
+
- Currently PySocks does not support IPv6 connections to the SOCKS proxy. Any
|
|
36
|
+
such connection attempt will fail.
|
|
37
|
+
"""
|
|
38
|
+
from __future__ import absolute_import
|
|
39
|
+
|
|
40
|
+
try:
|
|
41
|
+
from ..packages import socks
|
|
42
|
+
except ImportError:
|
|
43
|
+
import warnings
|
|
44
|
+
from ..exceptions import DependencyWarning
|
|
45
|
+
|
|
46
|
+
warnings.warn((
|
|
47
|
+
'SOCKS support in urllib3 requires the installation of optional '
|
|
48
|
+
'dependencies: specifically, PySocks. For more information, see '
|
|
49
|
+
'https://urllib3.readthedocs.io/en/latest/contrib.html#socks-proxies'
|
|
50
|
+
),
|
|
51
|
+
DependencyWarning
|
|
52
|
+
)
|
|
53
|
+
raise
|
|
54
|
+
|
|
55
|
+
from socket import error as SocketError, timeout as SocketTimeout
|
|
56
|
+
|
|
57
|
+
from ..connection import (
|
|
58
|
+
HTTPConnection, HTTPSConnection
|
|
59
|
+
)
|
|
60
|
+
from ..connectionpool import (
|
|
61
|
+
HTTPConnectionPool, HTTPSConnectionPool
|
|
62
|
+
)
|
|
63
|
+
from ..exceptions import ConnectTimeoutError, NewConnectionError
|
|
64
|
+
from ..poolmanager import PoolManager
|
|
65
|
+
from ..util.url import parse_url
|
|
66
|
+
|
|
67
|
+
try:
|
|
68
|
+
import ssl
|
|
69
|
+
except ImportError:
|
|
70
|
+
ssl = None
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class SOCKSConnection(HTTPConnection):
|
|
74
|
+
"""
|
|
75
|
+
A plain-text HTTP connection that connects via a SOCKS proxy.
|
|
76
|
+
"""
|
|
77
|
+
def __init__(self, *args, **kwargs):
|
|
78
|
+
self._socks_options = kwargs.pop('_socks_options')
|
|
79
|
+
super(SOCKSConnection, self).__init__(*args, **kwargs)
|
|
80
|
+
|
|
81
|
+
def _new_conn(self):
|
|
82
|
+
"""
|
|
83
|
+
Establish a new connection via the SOCKS proxy.
|
|
84
|
+
"""
|
|
85
|
+
extra_kw = {}
|
|
86
|
+
if self.source_address:
|
|
87
|
+
extra_kw['source_address'] = self.source_address
|
|
88
|
+
|
|
89
|
+
if self.socket_options:
|
|
90
|
+
extra_kw['socket_options'] = self.socket_options
|
|
91
|
+
|
|
92
|
+
try:
|
|
93
|
+
conn = socks.create_connection(
|
|
94
|
+
(self.host, self.port),
|
|
95
|
+
proxy_type=self._socks_options['socks_version'],
|
|
96
|
+
proxy_addr=self._socks_options['proxy_host'],
|
|
97
|
+
proxy_port=self._socks_options['proxy_port'],
|
|
98
|
+
proxy_username=self._socks_options['username'],
|
|
99
|
+
proxy_password=self._socks_options['password'],
|
|
100
|
+
proxy_rdns=self._socks_options['rdns'],
|
|
101
|
+
timeout=self.timeout,
|
|
102
|
+
**extra_kw
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
except SocketTimeout:
|
|
106
|
+
raise ConnectTimeoutError(
|
|
107
|
+
self, "Connection to %s timed out. (connect timeout=%s)" %
|
|
108
|
+
(self.host, self.timeout))
|
|
109
|
+
|
|
110
|
+
except socks.ProxyError as e:
|
|
111
|
+
# This is fragile as hell, but it seems to be the only way to raise
|
|
112
|
+
# useful errors here.
|
|
113
|
+
if e.socket_err:
|
|
114
|
+
error = e.socket_err
|
|
115
|
+
if isinstance(error, SocketTimeout):
|
|
116
|
+
raise ConnectTimeoutError(
|
|
117
|
+
self,
|
|
118
|
+
"Connection to %s timed out. (connect timeout=%s)" %
|
|
119
|
+
(self.host, self.timeout)
|
|
120
|
+
)
|
|
121
|
+
else:
|
|
122
|
+
raise NewConnectionError(
|
|
123
|
+
self,
|
|
124
|
+
"Failed to establish a new connection: %s" % error
|
|
125
|
+
)
|
|
126
|
+
else:
|
|
127
|
+
raise NewConnectionError(
|
|
128
|
+
self,
|
|
129
|
+
"Failed to establish a new connection: %s" % e
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
except SocketError as e: # Defensive: PySocks should catch all these.
|
|
133
|
+
raise NewConnectionError(
|
|
134
|
+
self, "Failed to establish a new connection: %s" % e)
|
|
135
|
+
|
|
136
|
+
return conn
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
# We don't need to duplicate the Verified/Unverified distinction from
|
|
140
|
+
# urllib3/connection.py here because the HTTPSConnection will already have been
|
|
141
|
+
# correctly set to either the Verified or Unverified form by that module. This
|
|
142
|
+
# means the SOCKSHTTPSConnection will automatically be the correct type.
|
|
143
|
+
class SOCKSHTTPSConnection(SOCKSConnection, HTTPSConnection):
|
|
144
|
+
pass
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
class SOCKSHTTPConnectionPool(HTTPConnectionPool):
|
|
148
|
+
ConnectionCls = SOCKSConnection
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
class SOCKSHTTPSConnectionPool(HTTPSConnectionPool):
|
|
152
|
+
ConnectionCls = SOCKSHTTPSConnection
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
class SOCKSProxyManager(PoolManager):
|
|
156
|
+
"""
|
|
157
|
+
A version of the urllib3 ProxyManager that routes connections via the
|
|
158
|
+
defined SOCKS proxy.
|
|
159
|
+
"""
|
|
160
|
+
pool_classes_by_scheme = {
|
|
161
|
+
'http': SOCKSHTTPConnectionPool,
|
|
162
|
+
'https': SOCKSHTTPSConnectionPool,
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
def __init__(self, proxy_url, username=None, password=None,
|
|
166
|
+
num_pools=10, headers=None, **connection_pool_kw):
|
|
167
|
+
parsed = parse_url(proxy_url)
|
|
168
|
+
|
|
169
|
+
if username is None and password is None and parsed.auth is not None:
|
|
170
|
+
split = parsed.auth.split(':')
|
|
171
|
+
if len(split) == 2:
|
|
172
|
+
username, password = split
|
|
173
|
+
if parsed.scheme == 'socks5':
|
|
174
|
+
socks_version = socks.PROXY_TYPE_SOCKS5
|
|
175
|
+
rdns = False
|
|
176
|
+
elif parsed.scheme == 'socks5h':
|
|
177
|
+
socks_version = socks.PROXY_TYPE_SOCKS5
|
|
178
|
+
rdns = True
|
|
179
|
+
elif parsed.scheme == 'socks4':
|
|
180
|
+
socks_version = socks.PROXY_TYPE_SOCKS4
|
|
181
|
+
rdns = False
|
|
182
|
+
elif parsed.scheme == 'socks4a':
|
|
183
|
+
socks_version = socks.PROXY_TYPE_SOCKS4
|
|
184
|
+
rdns = True
|
|
185
|
+
else:
|
|
186
|
+
raise ValueError(
|
|
187
|
+
"Unable to determine SOCKS version from %s" % proxy_url
|
|
188
|
+
)
|
|
189
|
+
|
|
190
|
+
self.proxy_url = proxy_url
|
|
191
|
+
|
|
192
|
+
socks_options = {
|
|
193
|
+
'socks_version': socks_version,
|
|
194
|
+
'proxy_host': parsed.host,
|
|
195
|
+
'proxy_port': parsed.port,
|
|
196
|
+
'username': username,
|
|
197
|
+
'password': password,
|
|
198
|
+
'rdns': rdns
|
|
199
|
+
}
|
|
200
|
+
connection_pool_kw['_socks_options'] = socks_options
|
|
201
|
+
|
|
202
|
+
super(SOCKSProxyManager, self).__init__(
|
|
203
|
+
num_pools, headers, **connection_pool_kw
|
|
204
|
+
)
|
|
205
|
+
|
|
206
|
+
self.pool_classes_by_scheme = SOCKSProxyManager.pool_classes_by_scheme
|
|
@@ -0,0 +1,260 @@
|
|
|
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
|
+
from __future__ import absolute_import
|
|
16
|
+
from .packages.six.moves.http_client import (
|
|
17
|
+
IncompleteRead as httplib_IncompleteRead
|
|
18
|
+
)
|
|
19
|
+
# Base Exceptions
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class HTTPError(Exception):
|
|
23
|
+
"Base exception used by this module."
|
|
24
|
+
pass
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class HTTPWarning(Warning):
|
|
28
|
+
"Base warning used by this module."
|
|
29
|
+
pass
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class PoolError(HTTPError):
|
|
33
|
+
"Base exception for errors caused within a pool."
|
|
34
|
+
def __init__(self, pool, message):
|
|
35
|
+
self.pool = pool
|
|
36
|
+
HTTPError.__init__(self, "%s: %s" % (pool, message))
|
|
37
|
+
|
|
38
|
+
def __reduce__(self):
|
|
39
|
+
# For pickling purposes.
|
|
40
|
+
return self.__class__, (None, None)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class RequestError(PoolError):
|
|
44
|
+
"Base exception for PoolErrors that have associated URLs."
|
|
45
|
+
def __init__(self, pool, url, message):
|
|
46
|
+
self.url = url
|
|
47
|
+
PoolError.__init__(self, pool, message)
|
|
48
|
+
|
|
49
|
+
def __reduce__(self):
|
|
50
|
+
# For pickling purposes.
|
|
51
|
+
return self.__class__, (None, self.url, None)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class SSLError(HTTPError):
|
|
55
|
+
"Raised when SSL certificate fails in an HTTPS connection."
|
|
56
|
+
pass
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class ProxyError(HTTPError):
|
|
60
|
+
"Raised when the connection to a proxy fails."
|
|
61
|
+
pass
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class DecodeError(HTTPError):
|
|
65
|
+
"Raised when automatic decoding based on Content-Type fails."
|
|
66
|
+
pass
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class ProtocolError(HTTPError):
|
|
70
|
+
"Raised when something unexpected happens mid-request/response."
|
|
71
|
+
pass
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
#: Renamed to ProtocolError but aliased for backwards compatibility.
|
|
75
|
+
ConnectionError = ProtocolError
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
# Leaf Exceptions
|
|
79
|
+
|
|
80
|
+
class MaxRetryError(RequestError):
|
|
81
|
+
"""Raised when the maximum number of retries is exceeded.
|
|
82
|
+
|
|
83
|
+
:param pool: The connection pool
|
|
84
|
+
:type pool: :class:`~urllib3.connectionpool.HTTPConnectionPool`
|
|
85
|
+
:param string url: The requested Url
|
|
86
|
+
:param exceptions.Exception reason: The underlying error
|
|
87
|
+
|
|
88
|
+
"""
|
|
89
|
+
|
|
90
|
+
def __init__(self, pool, url, reason=None):
|
|
91
|
+
self.reason = reason
|
|
92
|
+
|
|
93
|
+
message = "Max retries exceeded with url: %s (Caused by %r)" % (
|
|
94
|
+
url, reason)
|
|
95
|
+
|
|
96
|
+
RequestError.__init__(self, pool, url, message)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
class HostChangedError(RequestError):
|
|
100
|
+
"Raised when an existing pool gets a request for a foreign host."
|
|
101
|
+
|
|
102
|
+
def __init__(self, pool, url, retries=3):
|
|
103
|
+
message = "Tried to open a foreign host with url: %s" % url
|
|
104
|
+
RequestError.__init__(self, pool, url, message)
|
|
105
|
+
self.retries = retries
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
class TimeoutStateError(HTTPError):
|
|
109
|
+
""" Raised when passing an invalid state to a timeout """
|
|
110
|
+
pass
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
class TimeoutError(HTTPError):
|
|
114
|
+
""" Raised when a socket timeout error occurs.
|
|
115
|
+
|
|
116
|
+
Catching this error will catch both :exc:`ReadTimeoutErrors
|
|
117
|
+
<ReadTimeoutError>` and :exc:`ConnectTimeoutErrors <ConnectTimeoutError>`.
|
|
118
|
+
"""
|
|
119
|
+
pass
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
class ReadTimeoutError(TimeoutError, RequestError):
|
|
123
|
+
"Raised when a socket timeout occurs while receiving data from a server"
|
|
124
|
+
pass
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
# This timeout error does not have a URL attached and needs to inherit from the
|
|
128
|
+
# base HTTPError
|
|
129
|
+
class ConnectTimeoutError(TimeoutError):
|
|
130
|
+
"Raised when a socket timeout occurs while connecting to a server"
|
|
131
|
+
pass
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
class NewConnectionError(ConnectTimeoutError, PoolError):
|
|
135
|
+
"Raised when we fail to establish a new connection. Usually ECONNREFUSED."
|
|
136
|
+
pass
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
class EmptyPoolError(PoolError):
|
|
140
|
+
"Raised when a pool runs out of connections and no more are allowed."
|
|
141
|
+
pass
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
class ClosedPoolError(PoolError):
|
|
145
|
+
"Raised when a request enters a pool after the pool has been closed."
|
|
146
|
+
pass
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
class LocationValueError(ValueError, HTTPError):
|
|
150
|
+
"Raised when there is something wrong with a given URL input."
|
|
151
|
+
pass
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
class LocationParseError(LocationValueError):
|
|
155
|
+
"Raised when get_host or similar fails to parse the URL input."
|
|
156
|
+
|
|
157
|
+
def __init__(self, location):
|
|
158
|
+
message = "Failed to parse: %s" % location
|
|
159
|
+
HTTPError.__init__(self, message)
|
|
160
|
+
|
|
161
|
+
self.location = location
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
class ResponseError(HTTPError):
|
|
165
|
+
"Used as a container for an error reason supplied in a MaxRetryError."
|
|
166
|
+
GENERIC_ERROR = 'too many error responses'
|
|
167
|
+
SPECIFIC_ERROR = 'too many {status_code} error responses'
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
class SecurityWarning(HTTPWarning):
|
|
171
|
+
"Warned when performing security reducing actions"
|
|
172
|
+
pass
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
class SubjectAltNameWarning(SecurityWarning):
|
|
176
|
+
"Warned when connecting to a host with a certificate missing a SAN."
|
|
177
|
+
pass
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
class InsecureRequestWarning(SecurityWarning):
|
|
181
|
+
"Warned when making an unverified HTTPS request."
|
|
182
|
+
pass
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
class SystemTimeWarning(SecurityWarning):
|
|
186
|
+
"Warned when system time is suspected to be wrong"
|
|
187
|
+
pass
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
class InsecurePlatformWarning(SecurityWarning):
|
|
191
|
+
"Warned when certain SSL configuration is not available on a platform."
|
|
192
|
+
pass
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
class SNIMissingWarning(HTTPWarning):
|
|
196
|
+
"Warned when making a HTTPS request without SNI available."
|
|
197
|
+
pass
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
class DependencyWarning(HTTPWarning):
|
|
201
|
+
"""
|
|
202
|
+
Warned when an attempt is made to import a module with missing optional
|
|
203
|
+
dependencies.
|
|
204
|
+
"""
|
|
205
|
+
pass
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
class ResponseNotChunked(ProtocolError, ValueError):
|
|
209
|
+
"Response needs to be chunked in order to read it as chunks."
|
|
210
|
+
pass
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
class BodyNotHttplibCompatible(HTTPError):
|
|
214
|
+
"""
|
|
215
|
+
Body should be httplib.HTTPResponse like (have an fp attribute which
|
|
216
|
+
returns raw chunks) for read_chunked().
|
|
217
|
+
"""
|
|
218
|
+
pass
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
class IncompleteRead(HTTPError, httplib_IncompleteRead):
|
|
222
|
+
"""
|
|
223
|
+
Response length doesn't match expected Content-Length
|
|
224
|
+
|
|
225
|
+
Subclass of http_client.IncompleteRead to allow int value
|
|
226
|
+
for `partial` to avoid creating large objects on streamed
|
|
227
|
+
reads.
|
|
228
|
+
"""
|
|
229
|
+
def __init__(self, partial, expected):
|
|
230
|
+
super(IncompleteRead, self).__init__(partial, expected)
|
|
231
|
+
|
|
232
|
+
def __repr__(self):
|
|
233
|
+
return ('IncompleteRead(%i bytes read, '
|
|
234
|
+
'%i more expected)' % (self.partial, self.expected))
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
class InvalidHeader(HTTPError):
|
|
238
|
+
"The header provided was somehow invalid."
|
|
239
|
+
pass
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
class ProxySchemeUnknown(AssertionError, ValueError):
|
|
243
|
+
"ProxyManager does not support the supplied scheme"
|
|
244
|
+
# TODO(t-8ch): Stop inheriting from AssertionError in v2.0.
|
|
245
|
+
|
|
246
|
+
def __init__(self, scheme):
|
|
247
|
+
message = "Not supported proxy scheme %s" % scheme
|
|
248
|
+
super(ProxySchemeUnknown, self).__init__(message)
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
class HeaderParsingError(HTTPError):
|
|
252
|
+
"Raised by assert_header_parsing, but we convert it to a log.warning statement."
|
|
253
|
+
def __init__(self, defects, unparsed_data):
|
|
254
|
+
message = '%s, unparsed data: %r' % (defects or 'Unknown', unparsed_data)
|
|
255
|
+
super(HeaderParsingError, self).__init__(message)
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
class UnrewindableBodyError(HTTPError):
|
|
259
|
+
"urllib3 encountered an error when trying to rewind a body"
|
|
260
|
+
pass
|
|
@@ -0,0 +1,192 @@
|
|
|
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
|
+
from __future__ import absolute_import
|
|
16
|
+
import email.utils
|
|
17
|
+
import mimetypes
|
|
18
|
+
|
|
19
|
+
from .packages import six
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def guess_content_type(filename, default='application/octet-stream'):
|
|
23
|
+
"""
|
|
24
|
+
Guess the "Content-Type" of a file.
|
|
25
|
+
|
|
26
|
+
:param filename:
|
|
27
|
+
The filename to guess the "Content-Type" of using :mod:`mimetypes`.
|
|
28
|
+
:param default:
|
|
29
|
+
If no "Content-Type" can be guessed, default to `default`.
|
|
30
|
+
"""
|
|
31
|
+
if filename:
|
|
32
|
+
return mimetypes.guess_type(filename)[0] or default
|
|
33
|
+
return default
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def format_header_param(name, value):
|
|
37
|
+
"""
|
|
38
|
+
Helper function to format and quote a single header parameter.
|
|
39
|
+
|
|
40
|
+
Particularly useful for header parameters which might contain
|
|
41
|
+
non-ASCII values, like file names. This follows RFC 2231, as
|
|
42
|
+
suggested by RFC 2388 Section 4.4.
|
|
43
|
+
|
|
44
|
+
:param name:
|
|
45
|
+
The name of the parameter, a string expected to be ASCII only.
|
|
46
|
+
:param value:
|
|
47
|
+
The value of the parameter, provided as a unicode string.
|
|
48
|
+
"""
|
|
49
|
+
if not any(ch in value for ch in '"\\\r\n'):
|
|
50
|
+
result = '%s="%s"' % (name, value)
|
|
51
|
+
try:
|
|
52
|
+
result.encode('ascii')
|
|
53
|
+
except (UnicodeEncodeError, UnicodeDecodeError):
|
|
54
|
+
pass
|
|
55
|
+
else:
|
|
56
|
+
return result
|
|
57
|
+
if not six.PY3 and isinstance(value, six.text_type): # Python 2:
|
|
58
|
+
value = value.encode('utf-8')
|
|
59
|
+
value = email.utils.encode_rfc2231(value, 'utf-8')
|
|
60
|
+
value = '%s*=%s' % (name, value)
|
|
61
|
+
return value
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class RequestField(object):
|
|
65
|
+
"""
|
|
66
|
+
A data container for request body parameters.
|
|
67
|
+
|
|
68
|
+
:param name:
|
|
69
|
+
The name of this request field.
|
|
70
|
+
:param data:
|
|
71
|
+
The data/value body.
|
|
72
|
+
:param filename:
|
|
73
|
+
An optional filename of the request field.
|
|
74
|
+
:param headers:
|
|
75
|
+
An optional dict-like object of headers to initially use for the field.
|
|
76
|
+
"""
|
|
77
|
+
def __init__(self, name, data, filename=None, headers=None):
|
|
78
|
+
self._name = name
|
|
79
|
+
self._filename = filename
|
|
80
|
+
self.data = data
|
|
81
|
+
self.headers = {}
|
|
82
|
+
if headers:
|
|
83
|
+
self.headers = dict(headers)
|
|
84
|
+
|
|
85
|
+
@classmethod
|
|
86
|
+
def from_tuples(cls, fieldname, value):
|
|
87
|
+
"""
|
|
88
|
+
A :class:`~urllib3.fields.RequestField` factory from old-style tuple parameters.
|
|
89
|
+
|
|
90
|
+
Supports constructing :class:`~urllib3.fields.RequestField` from
|
|
91
|
+
parameter of key/value strings AND key/filetuple. A filetuple is a
|
|
92
|
+
(filename, data, MIME type) tuple where the MIME type is optional.
|
|
93
|
+
For example::
|
|
94
|
+
|
|
95
|
+
'foo': 'bar',
|
|
96
|
+
'fakefile': ('foofile.txt', 'contents of foofile'),
|
|
97
|
+
'realfile': ('barfile.txt', open('realfile').read()),
|
|
98
|
+
'typedfile': ('bazfile.bin', open('bazfile').read(), 'image/jpeg'),
|
|
99
|
+
'nonamefile': 'contents of nonamefile field',
|
|
100
|
+
|
|
101
|
+
Field names and filenames must be unicode.
|
|
102
|
+
"""
|
|
103
|
+
if isinstance(value, tuple):
|
|
104
|
+
if len(value) == 3:
|
|
105
|
+
filename, data, content_type = value
|
|
106
|
+
else:
|
|
107
|
+
filename, data = value
|
|
108
|
+
content_type = guess_content_type(filename)
|
|
109
|
+
else:
|
|
110
|
+
filename = None
|
|
111
|
+
content_type = None
|
|
112
|
+
data = value
|
|
113
|
+
|
|
114
|
+
request_param = cls(fieldname, data, filename=filename)
|
|
115
|
+
request_param.make_multipart(content_type=content_type)
|
|
116
|
+
|
|
117
|
+
return request_param
|
|
118
|
+
|
|
119
|
+
def _render_part(self, name, value):
|
|
120
|
+
"""
|
|
121
|
+
Overridable helper function to format a single header parameter.
|
|
122
|
+
|
|
123
|
+
:param name:
|
|
124
|
+
The name of the parameter, a string expected to be ASCII only.
|
|
125
|
+
:param value:
|
|
126
|
+
The value of the parameter, provided as a unicode string.
|
|
127
|
+
"""
|
|
128
|
+
return format_header_param(name, value)
|
|
129
|
+
|
|
130
|
+
def _render_parts(self, header_parts):
|
|
131
|
+
"""
|
|
132
|
+
Helper function to format and quote a single header.
|
|
133
|
+
|
|
134
|
+
Useful for single headers that are composed of multiple items. E.g.,
|
|
135
|
+
'Content-Disposition' fields.
|
|
136
|
+
|
|
137
|
+
:param header_parts:
|
|
138
|
+
A sequence of (k, v) tuples or a :class:`dict` of (k, v) to format
|
|
139
|
+
as `k1="v1"; k2="v2"; ...`.
|
|
140
|
+
"""
|
|
141
|
+
parts = []
|
|
142
|
+
iterable = header_parts
|
|
143
|
+
if isinstance(header_parts, dict):
|
|
144
|
+
iterable = header_parts.items()
|
|
145
|
+
|
|
146
|
+
for name, value in iterable:
|
|
147
|
+
if value is not None:
|
|
148
|
+
parts.append(self._render_part(name, value))
|
|
149
|
+
|
|
150
|
+
return '; '.join(parts)
|
|
151
|
+
|
|
152
|
+
def render_headers(self):
|
|
153
|
+
"""
|
|
154
|
+
Renders the headers for this request field.
|
|
155
|
+
"""
|
|
156
|
+
lines = []
|
|
157
|
+
|
|
158
|
+
sort_keys = ['Content-Disposition', 'Content-Type', 'Content-Location']
|
|
159
|
+
for sort_key in sort_keys:
|
|
160
|
+
if self.headers.get(sort_key, False):
|
|
161
|
+
lines.append('%s: %s' % (sort_key, self.headers[sort_key]))
|
|
162
|
+
|
|
163
|
+
for header_name, header_value in self.headers.items():
|
|
164
|
+
if header_name not in sort_keys:
|
|
165
|
+
if header_value:
|
|
166
|
+
lines.append('%s: %s' % (header_name, header_value))
|
|
167
|
+
|
|
168
|
+
lines.append('\r\n')
|
|
169
|
+
return '\r\n'.join(lines)
|
|
170
|
+
|
|
171
|
+
def make_multipart(self, content_disposition=None, content_type=None,
|
|
172
|
+
content_location=None):
|
|
173
|
+
"""
|
|
174
|
+
Makes this request field into a multipart request field.
|
|
175
|
+
|
|
176
|
+
This method overrides "Content-Disposition", "Content-Type" and
|
|
177
|
+
"Content-Location" headers to the request parameter.
|
|
178
|
+
|
|
179
|
+
:param content_type:
|
|
180
|
+
The 'Content-Type' of the request body.
|
|
181
|
+
:param content_location:
|
|
182
|
+
The 'Content-Location' of the request body.
|
|
183
|
+
|
|
184
|
+
"""
|
|
185
|
+
self.headers['Content-Disposition'] = content_disposition or 'form-data'
|
|
186
|
+
self.headers['Content-Disposition'] += '; '.join([
|
|
187
|
+
'', self._render_parts(
|
|
188
|
+
(('name', self._name), ('filename', self._filename))
|
|
189
|
+
)
|
|
190
|
+
])
|
|
191
|
+
self.headers['Content-Type'] = content_type
|
|
192
|
+
self.headers['Content-Location'] = content_location
|