Skip to content
This repository was archived by the owner on Apr 15, 2020. It is now read-only.

Commit 893b2b7

Browse files
committed
Support customized requests session
1 parent 7ed2abb commit 893b2b7

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

bosonnlp/client.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ class BosonNLP(object):
5757
5858
"""
5959

60-
def __init__(self, token, bosonnlp_url=DEFAULT_BOSONNLP_URL, compress=True):
60+
def __init__(self, token, bosonnlp_url=DEFAULT_BOSONNLP_URL, compress=True, session=None):
6161
self.token = token
6262
self.bosonnlp_url = bosonnlp_url.rstrip('/')
6363
self.compress = compress
6464

6565
# Enable keep-alive and connection-pooling.
66-
self.session = requests.session()
66+
self.session = session or requests.session()
6767
self.session.headers['X-Token'] = token
6868
self.session.headers['Accept'] = 'application/json'
6969
self.session.headers['User-Agent'] = 'bosonnlp.py/{} {}'.format(

tests.py

+11
Original file line numberDiff line numberDiff line change
@@ -321,3 +321,14 @@ def test_comments_task_with_multiple_push(nlp, input1, input2):
321321
comments.clear()
322322
assert len(result) == 4
323323
assert {'opinion', '_id', 'list', 'num'} == set(result[0].keys())
324+
325+
326+
def test_custom_requests_session():
327+
import requests
328+
329+
session = requests.session()
330+
nlp = BosonNLP(os.environ['BOSON_API_TOKEN'], session=session)
331+
assert nlp.session == session
332+
result = nlp.sentiment(['再也不来了', '美好的世界'])
333+
assert result[0][1] > result[0][0]
334+
assert result[1][0] > result[1][1]

0 commit comments

Comments
 (0)