Skip to content

Commit 14bcf41

Browse files
committed
feat(BaseService): use a requests.Session for retry and other configuration
1 parent dcb1ad3 commit 14bcf41

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

ibm_cloud_sdk_core/base_service.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ class BaseService:
6565
default_headers (dict): A dictionary of headers to be sent with every HTTP request to the service endpoint.
6666
jar (http.cookiejar.CookieJar): Stores cookies received from the service.
6767
http_config (dict): A dictionary containing values that control the timeout, proxies, and etc of HTTP requests.
68+
http_client (Session): A configurable session which can use Transport Adapters to configure retries, timeouts,
69+
proxies, etc. globally for all requests.
6870
enable_gzip_compression (bool): A flag that indicates whether to enable gzip compression on request bodies
6971
Raises:
7072
ValueError: If Authenticator is not provided or invalid type.
@@ -82,6 +84,7 @@ def __init__(self,
8284
disable_ssl_verification: bool = False,
8385
enable_gzip_compression: bool = False) -> None:
8486
self.set_service_url(service_url)
87+
self.http_client = requests.Session()
8588
self.http_config = {}
8689
self.jar = CookieJar()
8790
self.authenticator = authenticator
@@ -181,6 +184,25 @@ def set_service_url(self, service_url: str) -> None:
181184
)
182185
self.service_url = service_url
183186

187+
def get_http_client(self) -> requests.sessions.Session:
188+
"""Get the http client session currently used by the service.
189+
190+
Returns:
191+
The http client session currently used by the service.
192+
"""
193+
return self.http_client
194+
195+
def set_http_client(self, http_client: requests.sessions.Session) -> None:
196+
"""Set current http client session
197+
198+
Arguments:
199+
http_client: A new requests session client
200+
"""
201+
if isinstance(http_client, requests.sessions.Session):
202+
self.http_client = http_client
203+
else:
204+
raise TypeError("http_client parameter must be a requests.sessions.Session")
205+
184206
def get_authenticator(self) -> Authenticator:
185207
"""Get the authenticator currently used by the service.
186208
@@ -224,7 +246,7 @@ def send(self, request: requests.Request, **kwargs) -> DetailedResponse:
224246
stream_response = kwargs.get('stream') or False
225247

226248
try:
227-
response = requests.request(**request, cookies=self.jar, **kwargs)
249+
response = self.http_client.request(**request, cookies=self.jar, **kwargs)
228250

229251
if 200 <= response.status_code <= 299:
230252
if response.status_code == 204 or request['method'] == 'HEAD':

test/test_base_service.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,20 @@ def test_set_service_url():
472472

473473
service.set_service_url('my_url')
474474

475+
def test_http_client():
476+
auth = BasicAuthenticator('my_username', 'my_password')
477+
service = AnyServiceV1('2018-11-20', authenticator=auth)
478+
assert isinstance(service.get_http_client(), requests.sessions.Session)
479+
assert service.get_http_client().headers.get('Accept-Encoding') == 'gzip, deflate'
480+
481+
new_http_client = requests.Session()
482+
new_http_client.headers.update({'Accept-Encoding': 'gzip'})
483+
service.set_http_client(http_client=new_http_client)
484+
assert service.get_http_client().headers.get('Accept-Encoding') == 'gzip'
485+
486+
with pytest.raises(TypeError):
487+
service.set_http_client("bad_argument_type")
488+
475489
def test_get_authenticator():
476490
auth = BasicAuthenticator('my_username', 'my_password')
477491
service = AnyServiceV1('2018-11-20', authenticator=auth)

0 commit comments

Comments
 (0)