27
27
from requests .structures import CaseInsensitiveDict
28
28
from ibm_cloud_sdk_core .authenticators import Authenticator
29
29
from .version import __version__
30
- from .utils import (
31
- has_bad_first_or_last_char ,
32
- remove_null_values ,
33
- cleanup_values ,
34
- read_external_sources ,
35
- strip_extra_slashes
36
- )
30
+ from .utils import (has_bad_first_or_last_char , remove_null_values ,
31
+ cleanup_values , read_external_sources , strip_extra_slashes )
37
32
from .detailed_response import DetailedResponse
38
33
from .api_exception import ApiException
39
34
from .token_manager import TokenManager
42
37
# import http.client as http_client
43
38
# http_client.HTTPConnection.debuglevel = 1
44
39
40
+
45
41
#pylint: disable=too-many-instance-attributes
46
42
#pylint: disable=too-many-locals
47
43
class BaseService :
@@ -95,8 +91,7 @@ def __init__(self,
95
91
if not self .authenticator :
96
92
raise ValueError ('authenticator must be provided' )
97
93
if not isinstance (self .authenticator , Authenticator ):
98
- raise ValueError (
99
- 'authenticator should be of type Authenticator' )
94
+ raise ValueError ('authenticator should be of type Authenticator' )
100
95
101
96
@staticmethod
102
97
def _get_system_info () -> str :
@@ -131,11 +126,10 @@ def configure_service(self, service_name: str) -> None:
131
126
if config .get ('URL' ):
132
127
self .set_service_url (config .get ('URL' ))
133
128
if config .get ('DISABLE_SSL' ):
134
- self .set_disable_ssl_verification (
135
- bool (config .get ('DISABLE_SSL' ))
136
- )
129
+ self .set_disable_ssl_verification (bool (config .get ('DISABLE_SSL' )))
137
130
if config .get ('ENABLE_GZIP' ) is not None :
138
- self .set_enable_gzip_compression (config .get ('ENABLE_GZIP' ) == 'True' )
131
+ self .set_enable_gzip_compression (
132
+ config .get ('ENABLE_GZIP' ) == 'True' )
139
133
140
134
def _set_user_agent_header (self , user_agent_string : str ) -> None :
141
135
self .user_agent_header = {'User-Agent' : user_agent_string }
@@ -153,8 +147,10 @@ def set_http_config(self, http_config: dict) -> None:
153
147
"""
154
148
if isinstance (http_config , dict ):
155
149
self .http_config = http_config
156
- if (self .authenticator and hasattr (self .authenticator , 'token_manager' ) and
157
- isinstance (self .authenticator .token_manager , TokenManager )):
150
+ if (self .authenticator
151
+ and hasattr (self .authenticator , 'token_manager' )
152
+ and isinstance (self .authenticator .token_manager ,
153
+ TokenManager )):
158
154
self .authenticator .token_manager .http_config = http_config
159
155
else :
160
156
raise TypeError ("http_config parameter must be a dictionary" )
@@ -201,7 +197,8 @@ def set_http_client(self, http_client: requests.sessions.Session) -> None:
201
197
if isinstance (http_client , requests .sessions .Session ):
202
198
self .http_client = http_client
203
199
else :
204
- raise TypeError ("http_client parameter must be a requests.sessions.Session" )
200
+ raise TypeError (
201
+ "http_client parameter must be a requests.sessions.Session" )
205
202
206
203
def get_authenticator (self ) -> Authenticator :
207
204
"""Get the authenticator currently used by the service.
@@ -246,7 +243,9 @@ def send(self, request: requests.Request, **kwargs) -> DetailedResponse:
246
243
stream_response = kwargs .get ('stream' ) or False
247
244
248
245
try :
249
- response = self .http_client .request (** request , cookies = self .jar , ** kwargs )
246
+ response = self .http_client .request (** request ,
247
+ cookies = self .jar ,
248
+ ** kwargs )
250
249
251
250
if 200 <= response .status_code <= 299 :
252
251
if response .status_code == 204 or request ['method' ] == 'HEAD' :
@@ -261,22 +260,18 @@ def send(self, request: requests.Request, **kwargs) -> DetailedResponse:
261
260
result = response .json ()
262
261
except :
263
262
result = response
264
- return DetailedResponse (response = result , headers = response .headers ,
263
+ return DetailedResponse (response = result ,
264
+ headers = response .headers ,
265
265
status_code = response .status_code )
266
266
267
- raise ApiException (
268
- response .status_code , http_response = response )
267
+ raise ApiException (response .status_code , http_response = response )
269
268
except requests .exceptions .SSLError :
270
269
logging .exception (self .ERROR_MSG_DISABLE_SSL )
271
270
raise
272
- except ApiException as err :
273
- logging .exception (err .message )
274
- raise
275
- except :
276
- logging .exception ('Error in service call' )
277
- raise
278
271
279
- def set_enable_gzip_compression (self , should_enable_compression : bool = False ) -> None :
272
+ def set_enable_gzip_compression (self ,
273
+ should_enable_compression : bool = False
274
+ ) -> None :
280
275
"""Set value to enable gzip compression on request bodies"""
281
276
self .enable_gzip_compression = should_enable_compression
282
277
@@ -291,10 +286,10 @@ def prepare_request(self,
291
286
headers : Optional [dict ] = None ,
292
287
params : Optional [dict ] = None ,
293
288
data : Optional [Union [str , dict ]] = None ,
294
- files : Optional [Union [
295
- Dict [ str , Tuple [str ]] ,
296
- List [ Tuple [ str , Tuple [str , ...]]]
297
- ]] = None ,
289
+ files : Optional [Union [Dict [ str , Tuple [ str ]],
290
+ List [ Tuple [str ,
291
+ Tuple [str ,
292
+ ...]]] ]] = None ,
298
293
** kwargs ) -> dict :
299
294
"""Build a dict that represents an HTTP service request.
300
295
@@ -349,9 +344,9 @@ def prepare_request(self,
349
344
self .authenticator .authenticate (request )
350
345
351
346
# Compress the request body if applicable
352
- if (self .get_enable_gzip_compression () and
353
- 'content-encoding' not in headers and
354
- request ['data' ] is not None ):
347
+ if (self .get_enable_gzip_compression ()
348
+ and 'content-encoding' not in headers
349
+ and request ['data' ] is not None ):
355
350
headers ['content-encoding' ] = 'gzip'
356
351
uncompressed_data = request ['data' ]
357
352
request_body = gzip .compress (uncompressed_data )
@@ -371,7 +366,8 @@ def prepare_request(self,
371
366
files = files .items ()
372
367
# Next, fill in any missing filenames from file tuples.
373
368
for part_name , file_tuple in files :
374
- if file_tuple and len (file_tuple ) == 3 and file_tuple [0 ] is None :
369
+ if file_tuple and len (
370
+ file_tuple ) == 3 and file_tuple [0 ] is None :
375
371
file = file_tuple [1 ]
376
372
if file and hasattr (file , 'name' ):
377
373
filename = basename (file .name )
0 commit comments