Skip to content

Commit 0a16528

Browse files
author
方佳
committed
Merge branch 'main_merge_250801' into 'main'
feat: add method to set headers See merge request webull/openapi-python-sdk!15
2 parents c8e25e7 + 4f55ae5 commit 0a16528

File tree

23 files changed

+229
-59
lines changed

23 files changed

+229
-59
lines changed

webull-python-sdk-core/webullsdkcore/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.1.14"
1+
__version__ = "0.1.15"
22

33
import logging
44

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
import threading
18+
19+
class RequestContextHolder:
20+
_thread_local = threading.local()
21+
22+
@classmethod
23+
def get(cls) -> dict:
24+
# Initializes the headers dictionary for the current thread.
25+
if not hasattr(cls._thread_local, 'headers'):
26+
cls._thread_local.headers = {}
27+
return cls._thread_local.headers
28+
29+
@classmethod
30+
def clear(cls):
31+
# Delete the current thread's headers field to prevent threads from reusing residual data.
32+
if hasattr(cls._thread_local, 'headers'):
33+
del cls._thread_local.headers

webull-python-sdk-demos/setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
LONG_DESCRIPTION = fp.read()
1616

1717
requires = [
18-
"webull-python-sdk-mdata==0.1.14",
19-
"webull-python-sdk-trade==0.1.14"
18+
"webull-python-sdk-mdata==0.1.15",
19+
"webull-python-sdk-trade==0.1.15"
2020
]
2121

2222
setup_args = {

webull-python-sdk-demos/tests/trade/request/test_order_operation.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import uuid
1717

1818
from webullsdkcore.client import ApiClient
19+
from webullsdkmdata.common.category import Category
1920

2021
from webullsdktrade.trade.order_operation import OrderOperation
2122

@@ -44,7 +45,12 @@ def test_order_operation(self):
4445
"extended_hours_trading": False,
4546
}
4647
}
47-
res = order_operation.place_order(account_id, stock_order['stock_order'])
48+
49+
# This is an optional feature; you can still make a request without setting it.
50+
custom_headers_map = {"category": Category.US_STOCK.name}
51+
order_operation.add_custom_headers(custom_headers_map)
52+
res = order_operation.place_order(account_id, **stock_order['stock_order'])
53+
order_operation.remove_custom_headers()
4854
if res.status_code == 200:
4955
print('place order status:', res.json())
5056

webull-python-sdk-demos/tests/trade/request/test_place_option.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
from webullsdkcore.client import ApiClient
1919
from webullsdkcore.exception.exceptions import ServerException
20+
from webullsdkmdata.common.category import Category
2021
from webullsdktrade.request.v2.place_option_request import PlaceOptionRequest
2122

2223
optional_api_endpoint = "<api_endpoint>"
@@ -61,12 +62,15 @@ def test_preview_order(self):
6162
request.set_endpoint(optional_api_endpoint)
6263
request.set_account_id(account_id)
6364
request.set_new_orders(new_orders)
64-
request.set_custom_header(new_orders)
6565
post_body = request.get_body_params()
6666
print(json.dumps(post_body, indent=4))
6767
params = request.get_query_params()
6868
print(params)
6969

70+
# This is an optional feature; you can still make a request without setting it.
71+
custom_headers_map = {"category": Category.US_OPTION.name}
72+
request.add_custom_headers(custom_headers_map)
73+
7074
try:
7175
response = api_client.get_response(request)
7276
print(response.json())

webull-python-sdk-demos/tests/trade/request/test_place_order.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from webullsdkcore.client import ApiClient
1818
from webullsdkcore.exception.exceptions import ServerException
19+
from webullsdkmdata.common.category import Category
1920
from webullsdktrade.request.palce_order_request import PlaceOrderRequest
2021

2122

@@ -59,6 +60,10 @@ def test_order(self):
5960
# request.set_trailing_type(stock_order['stock_order']['trailing_type'])
6061
# request.set_trailing_stop_step(stock_order['stock_order']['trailing_stop_step'])
6162

63+
# This is an optional feature; you can still make a request without setting it.
64+
custom_headers_map = {"category": Category.US_STOCK.name}
65+
request.add_custom_headers(custom_headers_map)
66+
6267
try:
6368
response = api_client.get_response(request)
6469
print(response.json())

webull-python-sdk-demos/tests/trade/request/v2/test_place_order.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
import uuid
1717

1818
from webullsdkcore.client import ApiClient
19+
from webullsdkcore.context.request_context_holder import RequestContextHolder
1920
from webullsdkcore.exception.exceptions import ServerException
21+
from webullsdkmdata.common.category import Category
2022
from webullsdktrade.request.v2.palce_order_request import PlaceOrderRequest
2123

2224
optional_api_endpoint = "<api_endpoint>"
@@ -30,17 +32,16 @@
3032
client_order_id = uuid.uuid4().hex
3133
new_orders = {
3234
"client_order_id": client_order_id,
33-
"symbol": "7011",
35+
"symbol": "AAPL",
3436
"instrument_type": "EQUITY",
35-
"market": "JP",
37+
"market": "US",
3638
"order_type": "LIMIT",
37-
"limit_price": "2080",
38-
"quantity": "100",
39+
"limit_price": "196",
40+
"quantity": "1",
3941
"support_trading_session": "N",
4042
"side": "BUY",
4143
"time_in_force": "DAY",
42-
"entrust_type": "QTY",
43-
"account_tax_type": "GENERAL"
44+
"entrust_type": "QTY"
4445
}
4546

4647

@@ -50,13 +51,16 @@ def test_place_order(self):
5051
request.set_endpoint(optional_api_endpoint)
5152
request.set_account_id(account_id)
5253
request.set_new_orders(new_orders)
53-
request.set_custom_header(new_orders)
5454
request.finalize_order()
5555
post_body = request.get_body_params()
5656
print(json.dumps(post_body, indent=4))
5757
params = request.get_query_params()
5858
print(params)
5959

60+
# This is an optional feature; you can still make a request without setting it.
61+
custom_headers_map = {"category": Category.US_STOCK.name}
62+
request.add_custom_headers(custom_headers_map)
63+
6064
try:
6165
response = api_client.get_response(request)
6266
print(response.json())

webull-python-sdk-demos/tests/trade/test_api.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,22 @@ def test_api(self):
221221
}
222222
"""
223223

224+
# This is an optional feature; you can still make a request without setting it.
225+
custom_headers_map = {"category": Category.US_STOCK.name}
226+
api.order.add_custom_headers(custom_headers_map)
224227
res = api.order.place_order(stock_order['account_id'], **stock_order['stock_order'])
228+
api.order.remove_custom_headers()
225229
if res.status_code == 200:
226230
print('place order res:', res.json())
227231
res = api.order.replace_order(stock_order['account_id'], **stock_order['stock_order'])
228232
if res.status_code == 200:
229233
print('replace order res:', res.json())
234+
235+
# This is an optional feature; you can still make a request without setting it.
236+
custom_headers_map = {"category": Category.US_STOCK.name}
237+
api.order.add_custom_headers(custom_headers_map)
230238
res = api.order.place_order_v2(stock_order['account_id'], stock_order['stock_order'])
239+
api.order.remove_custom_headers()
231240
if res.status_code == 200:
232241
print('place order v2 res:', res.json())
233242
res = api.order.replace_order_v2(stock_order['account_id'], stock_order['stock_order'])
@@ -298,7 +307,11 @@ def test_api(self):
298307
sleep(5)
299308

300309
# place
310+
# This is an optional feature; you can still make a request without setting it.
311+
custom_headers_map = {"category": Category.US_OPTION.name}
312+
api.order.add_custom_headers(custom_headers_map)
301313
res = api.order.place_option(account_id, option_new_orders)
314+
api.order.remove_custom_headers()
302315
if res.status_code == 200:
303316
print("place option=" + json.dumps(res.json(), indent=4))
304317
sleep(5)

webull-python-sdk-demos/tests/trade/test_api_v2.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,12 @@ def test_api(self):
8686
# {"party_id":"BNG144.666555","party_id_source":"D","party_role":"3"}
8787
# ]
8888
}
89+
90+
# This is an optional feature; you can still make a request without setting it.
91+
custom_headers_map = {"category": Category.US_STOCK.name}
92+
api.order_v2.add_custom_headers(custom_headers_map)
8993
res = api.order_v2.place_order(account_id=account_id, new_orders=new_orders)
94+
api.order_v2.remove_custom_headers()
9095
if res.status_code == 200:
9196
print("place_order_res=" + json.dumps(res.json(), indent=4))
9297
sleep(5)
@@ -158,7 +163,12 @@ def test_api(self):
158163
print("preview option=" + json.dumps(res.json(), indent=4))
159164
sleep(5)
160165
# place
166+
167+
# This is an optional feature; you can still make a request without setting it.
168+
custom_headers_map = {"category": Category.US_OPTION.name}
169+
api.order_v2.add_custom_headers(custom_headers_map)
161170
res = api.order_v2.place_option(account_id, option_new_orders)
171+
api.order_v2.remove_custom_headers()
162172
if res.status_code == 200:
163173
print("place option=" + json.dumps(res.json(), indent=4))
164174
sleep(5)

webull-python-sdk-mdata/setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
LONG_DESCRIPTION = fp.read()
1616

1717
requires = [
18-
"webull-python-sdk-core==0.1.14",
19-
"webull-python-sdk-quotes-core==0.1.14"
18+
"webull-python-sdk-core==0.1.15",
19+
"webull-python-sdk-quotes-core==0.1.15"
2020
]
2121

2222
setup_args = {

0 commit comments

Comments
 (0)