Skip to content

feat(disable-ssl-verification): log error for ssl error and others #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 23, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 35 additions & 21 deletions ibm_cloud_sdk_core/base_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from .api_exception import ApiException
from .authenticators import Authenticator
from http.cookiejar import CookieJar
import logging

# Uncomment this to enable http debugging
# import http.client as http_client
Expand All @@ -35,6 +36,9 @@
class BaseService(object):

SDK_NAME = 'ibm-python-sdk-core'
ERROR_MSG_DISABLE_SSL = 'If you\'re trying to call a service on ICP or Cloud Pak for Data, you may not have a valid SSL certificate. '\
'If you need to access the service without setting that up, try using the disable_ssl_verification option in your authentication '\
'configuration and/or setting set_disable_ssl_verification(True) on your service.'

def __init__(self,
service_url=None,
Expand Down Expand Up @@ -125,28 +129,38 @@ def send(self, request, **kwargs):
if self.disable_ssl_verification:
kwargs['verify'] = False

response = requests.request(**request, cookies=self.jar, **kwargs)

if 200 <= response.status_code <= 299:
if response.status_code == 204 or request['method'] == 'HEAD':
# There is no body content for a HEAD request or a 204 response
result = None
elif not response.text:
result = None
try:
response = requests.request(**request, cookies=self.jar, **kwargs)

if 200 <= response.status_code <= 299:
if response.status_code == 204 or request['method'] == 'HEAD':
# There is no body content for a HEAD request or a 204 response
result = None
elif not response.text:
result = None
else:
try:
result = response.json()
except:
result = response
return DetailedResponse(result, response.headers,
response.status_code)
else:
try:
result = response.json()
except:
result = response
return DetailedResponse(result, response.headers,
response.status_code)
else:
error_message = None
if response.status_code == 401:
error_message = 'Unauthorized: Access is denied due to ' \
'invalid credentials'
raise ApiException(
response.status_code, error_message, http_response=response)
error_message = None
if response.status_code == 401:
error_message = 'Unauthorized: Access is denied due to ' \
'invalid credentials'
raise ApiException(
response.status_code, error_message, 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 prepare_request(self, method, url, headers=None,
Expand Down