Skip to content

Commit

Permalink
Merge pull request #22 from IBM/rc-changes
Browse files Browse the repository at this point in the history
vcap and json loads changes
  • Loading branch information
ehdsouza authored Aug 30, 2019
2 parents 7eb7e88 + 8ef48a1 commit 5e6879a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
8 changes: 6 additions & 2 deletions ibm_cloud_sdk_core/base_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __init__(self,
"""
:attr str url: The url for service api calls
:attr Authenticator authenticator: The authenticator for authentication
:attr bool disable_ssl_verification: enables/ disabled ssl verification
:attr bool disable_ssl_verification: enables/ disables ssl verification
:attr str display_name the name used for mapping services in environment file
"""
self.url = url
Expand Down Expand Up @@ -179,9 +179,13 @@ def prepare_request(self, method, url, headers=None,
params = cleanup_values(params)
request['params'] = params

data = remove_null_values(data)
if sys.version_info >= (3, 0) and isinstance(data, str):
data = data.encode('utf-8')

if data and isinstance(data, dict):
data = remove_null_values(data)
headers.update({'content-type': 'application/json'})
data = json_import.dumps(data)
request['data'] = data

if self.authenticator:
Expand Down
25 changes: 14 additions & 11 deletions ibm_cloud_sdk_core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def read_from_env_variables(service_name):
"""
:return dict config: parsed env variables
"""
service_name = service_name.lower()
service_name = service_name.replace(' ', '_').lower()
config = {}
for key, value in environ.items():
_parse_key_and_update_config(config, service_name.lower(), key.lower(), value)
Expand All @@ -94,7 +94,7 @@ def read_from_credential_file(service_name, separator='='):
:param str service_name: The service name
:return dict config: parsed key values pairs
"""
service_name = service_name.lower()
service_name = service_name.replace(' ', '_').lower()
DEFAULT_CREDENTIALS_FILE_NAME = 'ibm-credentials.env'

# File path specified by an env variable
Expand Down Expand Up @@ -131,20 +131,23 @@ def _parse_key_and_update_config(config, service_name, key, value):
config[key[index + 1:]] = value

def read_from_vcap_services(service_name):
service_name = service_name.replace(' ', '_').lower()
vcap_services = getenv('VCAP_SERVICES')
vcap_service_credentials = None
if vcap_services:
services = json_import.loads(vcap_services)

if service_name in services:
vcap_service_credentials = services[service_name][0]['credentials']
if vcap_service_credentials is not None and isinstance(vcap_service_credentials, dict):
if vcap_service_credentials.get('username') and vcap_service_credentials.get('password'): # cf
vcap_service_credentials['auth_type'] = 'basic'
elif vcap_service_credentials.get('apikey'): # rc
vcap_service_credentials['auth_type'] = 'iam'
else: # no other auth mechanism is supported
vcap_service_credentials = None
for key in services.keys():
name = key.replace('-', '_')
if name == service_name:
vcap_service_credentials = services[key][0]['credentials']
if vcap_service_credentials is not None and isinstance(vcap_service_credentials, dict):
if vcap_service_credentials.get('username') and vcap_service_credentials.get('password'): # cf
vcap_service_credentials['auth_type'] = 'basic'
elif vcap_service_credentials.get('apikey'): # rc
vcap_service_credentials['auth_type'] = 'iam'
else: # no other auth mechanism is supported
vcap_service_credentials = None
return vcap_service_credentials

def contruct_authenticator(config):
Expand Down
6 changes: 6 additions & 0 deletions test/test_base_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,3 +414,9 @@ def test_files():
form_data['file1'] = (None, file, 'application/octet-stream')
form_data['string1'] = (None, 'hello', 'text.plain')
service.prepare_request('GET', url='', headers={'X-opt-out': True}, files=form_data)


def test_json():
service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())
req = service.prepare_request('POST', url='', headers={'X-opt-out': True}, data={'hello': 'world'})
assert req.get('data') == "{\"hello\": \"world\"}"
9 changes: 9 additions & 0 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,12 @@ def test_vcap_credentials():
assert authenticator is not None
assert authenticator.token_manager.apikey == 'bogus apikey'
del os.environ['VCAP_SERVICES']

vcap_services = '{"test":[{"credentials":{ \
"url":"https://gateway.watsonplatform.net/compare-comply/api",\
"iam_apikey":"bogus apikey"}}]}'

os.environ['VCAP_SERVICES'] = vcap_services
authenticator = get_authenticator_from_environment('test')
assert authenticator is None
del os.environ['VCAP_SERVICES']

0 comments on commit 5e6879a

Please sign in to comment.