Skip to content

Commit

Permalink
fix: remove unnecessary logging of exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
padamstx committed Feb 1, 2021
1 parent d438ade commit e94d5ae
Showing 1 changed file with 31 additions and 35 deletions.
66 changes: 31 additions & 35 deletions ibm_cloud_sdk_core/base_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,8 @@
from requests.structures import CaseInsensitiveDict
from ibm_cloud_sdk_core.authenticators import Authenticator
from .version import __version__
from .utils import (
has_bad_first_or_last_char,
remove_null_values,
cleanup_values,
read_external_sources,
strip_extra_slashes
)
from .utils import (has_bad_first_or_last_char, remove_null_values,
cleanup_values, read_external_sources, strip_extra_slashes)
from .detailed_response import DetailedResponse
from .api_exception import ApiException
from .token_manager import TokenManager
Expand All @@ -42,6 +37,7 @@
# import http.client as http_client
# http_client.HTTPConnection.debuglevel = 1


#pylint: disable=too-many-instance-attributes
#pylint: disable=too-many-locals
class BaseService:
Expand Down Expand Up @@ -95,8 +91,7 @@ def __init__(self,
if not self.authenticator:
raise ValueError('authenticator must be provided')
if not isinstance(self.authenticator, Authenticator):
raise ValueError(
'authenticator should be of type Authenticator')
raise ValueError('authenticator should be of type Authenticator')

@staticmethod
def _get_system_info() -> str:
Expand Down Expand Up @@ -131,11 +126,10 @@ def configure_service(self, service_name: str) -> None:
if config.get('URL'):
self.set_service_url(config.get('URL'))
if config.get('DISABLE_SSL'):
self.set_disable_ssl_verification(
bool(config.get('DISABLE_SSL'))
)
self.set_disable_ssl_verification(bool(config.get('DISABLE_SSL')))
if config.get('ENABLE_GZIP') is not None:
self.set_enable_gzip_compression(config.get('ENABLE_GZIP') == 'True')
self.set_enable_gzip_compression(
config.get('ENABLE_GZIP') == 'True')

def _set_user_agent_header(self, user_agent_string: str) -> None:
self.user_agent_header = {'User-Agent': user_agent_string}
Expand All @@ -153,8 +147,10 @@ def set_http_config(self, http_config: dict) -> None:
"""
if isinstance(http_config, dict):
self.http_config = http_config
if (self.authenticator and hasattr(self.authenticator, 'token_manager') and
isinstance(self.authenticator.token_manager, TokenManager)):
if (self.authenticator
and hasattr(self.authenticator, 'token_manager')
and isinstance(self.authenticator.token_manager,
TokenManager)):
self.authenticator.token_manager.http_config = http_config
else:
raise TypeError("http_config parameter must be a dictionary")
Expand Down Expand Up @@ -201,7 +197,8 @@ def set_http_client(self, http_client: requests.sessions.Session) -> None:
if isinstance(http_client, requests.sessions.Session):
self.http_client = http_client
else:
raise TypeError("http_client parameter must be a requests.sessions.Session")
raise TypeError(
"http_client parameter must be a requests.sessions.Session")

def get_authenticator(self) -> Authenticator:
"""Get the authenticator currently used by the service.
Expand Down Expand Up @@ -246,7 +243,9 @@ def send(self, request: requests.Request, **kwargs) -> DetailedResponse:
stream_response = kwargs.get('stream') or False

try:
response = self.http_client.request(**request, cookies=self.jar, **kwargs)
response = self.http_client.request(**request,
cookies=self.jar,
**kwargs)

if 200 <= response.status_code <= 299:
if response.status_code == 204 or request['method'] == 'HEAD':
Expand All @@ -261,22 +260,18 @@ def send(self, request: requests.Request, **kwargs) -> DetailedResponse:
result = response.json()
except:
result = response
return DetailedResponse(response=result, headers=response.headers,
return DetailedResponse(response=result,
headers=response.headers,
status_code=response.status_code)

raise ApiException(
response.status_code, http_response=response)
raise ApiException(response.status_code, http_response=response)
except requests.exceptions.SSLError:
logging.exception(self.ERROR_MSG_DISABLE_SSL)
raise
except ApiException as err:
logging.exception(err.message)
raise
except:
logging.exception('Error in service call')
raise

def set_enable_gzip_compression(self, should_enable_compression: bool = False) -> None:
def set_enable_gzip_compression(self,
should_enable_compression: bool = False
) -> None:
"""Set value to enable gzip compression on request bodies"""
self.enable_gzip_compression = should_enable_compression

Expand All @@ -291,10 +286,10 @@ def prepare_request(self,
headers: Optional[dict] = None,
params: Optional[dict] = None,
data: Optional[Union[str, dict]] = None,
files: Optional[Union[
Dict[str, Tuple[str]],
List[Tuple[str, Tuple[str, ...]]]
]] = None,
files: Optional[Union[Dict[str, Tuple[str]],
List[Tuple[str,
Tuple[str,
...]]]]] = None,
**kwargs) -> dict:
"""Build a dict that represents an HTTP service request.
Expand Down Expand Up @@ -349,9 +344,9 @@ def prepare_request(self,
self.authenticator.authenticate(request)

# Compress the request body if applicable
if (self.get_enable_gzip_compression() and
'content-encoding' not in headers and
request['data'] is not None):
if (self.get_enable_gzip_compression()
and 'content-encoding' not in headers
and request['data'] is not None):
headers['content-encoding'] = 'gzip'
uncompressed_data = request['data']
request_body = gzip.compress(uncompressed_data)
Expand All @@ -371,7 +366,8 @@ def prepare_request(self,
files = files.items()
# Next, fill in any missing filenames from file tuples.
for part_name, file_tuple in files:
if file_tuple and len(file_tuple) == 3 and file_tuple[0] is None:
if file_tuple and len(
file_tuple) == 3 and file_tuple[0] is None:
file = file_tuple[1]
if file and hasattr(file, 'name'):
filename = basename(file.name)
Expand Down

0 comments on commit e94d5ae

Please sign in to comment.