@@ -65,6 +65,8 @@ class BaseService:
65
65
default_headers (dict): A dictionary of headers to be sent with every HTTP request to the service endpoint.
66
66
jar (http.cookiejar.CookieJar): Stores cookies received from the service.
67
67
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.
68
70
enable_gzip_compression (bool): A flag that indicates whether to enable gzip compression on request bodies
69
71
Raises:
70
72
ValueError: If Authenticator is not provided or invalid type.
@@ -82,6 +84,7 @@ def __init__(self,
82
84
disable_ssl_verification : bool = False ,
83
85
enable_gzip_compression : bool = False ) -> None :
84
86
self .set_service_url (service_url )
87
+ self .http_client = requests .Session ()
85
88
self .http_config = {}
86
89
self .jar = CookieJar ()
87
90
self .authenticator = authenticator
@@ -181,6 +184,25 @@ def set_service_url(self, service_url: str) -> None:
181
184
)
182
185
self .service_url = service_url
183
186
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
+
184
206
def get_authenticator (self ) -> Authenticator :
185
207
"""Get the authenticator currently used by the service.
186
208
@@ -224,7 +246,7 @@ def send(self, request: requests.Request, **kwargs) -> DetailedResponse:
224
246
stream_response = kwargs .get ('stream' ) or False
225
247
226
248
try :
227
- response = requests .request (** request , cookies = self .jar , ** kwargs )
249
+ response = self . http_client .request (** request , cookies = self .jar , ** kwargs )
228
250
229
251
if 200 <= response .status_code <= 299 :
230
252
if response .status_code == 204 or request ['method' ] == 'HEAD' :
0 commit comments