Skip to content

Commit edd3bf3

Browse files
authored
Merge pull request #26 from IBM/rc-changes-2
logging, service_url and auth_type case insensitive changes
2 parents 98046ed + 459513e commit edd3bf3

File tree

4 files changed

+57
-19
lines changed

4 files changed

+57
-19
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,35 @@ authenticator = NoAuthAuthenticator()
7373
If you encounter an issue with this project, you are welcome to submit a [bug report](https://github.com/IBM/python-sdk-core/issues).
7474
Before opening a new issue, please search for similar issues. It's possible that someone has already reported it.
7575

76+
## Logging
77+
78+
### Enable logging
79+
80+
```python
81+
import logging
82+
logging.basicConfig(level=logging.DEBUG)
83+
```
84+
85+
This would show output of the form:
86+
```
87+
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): iam.cloud.ibm.com:443
88+
DEBUG:urllib3.connectionpool:https://iam.cloud.ibm.com:443 "POST /identity/token HTTP/1.1" 200 1809
89+
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): gateway.watsonplatform.net:443
90+
DEBUG:urllib3.connectionpool:https://gateway.watsonplatform.net:443 "POST /assistant/api/v1/workspaces?version=2018-07-10 HTTP/1.1" 201 None
91+
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): gateway.watsonplatform.net:443
92+
DEBUG:urllib3.connectionpool:https://gateway.watsonplatform.net:443 "GET /assistant/api/v1/workspaces/883a2a44-eb5f-4b1a-96b0-32a90b475ea8?version=2018-07-10&export=true HTTP/1.1" 200 None
93+
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): gateway.watsonplatform.net:443
94+
DEBUG:urllib3.connectionpool:https://gateway.watsonplatform.net:443 "DELETE /assistant/api/v1/workspaces/883a2a44-eb5f-4b1a-96b0-32a90b475ea8?version=2018-07-10 HTTP/1.1" 200 28
95+
```
96+
97+
### Low level request and response dump
98+
To get low level information of the requests/ responses:
99+
100+
```python
101+
from http.client import HTTPConnection
102+
HTTPConnection.debuglevel = 1
103+
```
104+
76105
## Open source @ IBM
77106

78107
Find more open source projects on the [IBM Github Page](http://github.com/IBM)

ibm_cloud_sdk_core/base_service.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class BaseService(object):
3737
SDK_NAME = 'ibm-python-sdk-core'
3838

3939
def __init__(self,
40-
url,
40+
service_url,
4141
authenticator=None,
4242
disable_ssl_verification=False,
4343
display_name=None):
@@ -47,7 +47,7 @@ def __init__(self,
4747
:attr bool disable_ssl_verification: enables/ disables ssl verification
4848
:attr str display_name the name used for mapping services in environment file
4949
"""
50-
self.url = url
50+
self.service_url = service_url
5151
self.http_config = {}
5252
self.jar = CookieJar()
5353
self.authenticator = authenticator
@@ -100,16 +100,16 @@ def set_disable_ssl_verification(self, status=False):
100100
"""
101101
self.disable_ssl_verification = status
102102

103-
def set_url(self, url):
103+
def set_service_url(self, service_url):
104104
"""
105-
Sets the url
105+
Sets the service url
106106
"""
107-
if has_bad_first_or_last_char(url):
107+
if has_bad_first_or_last_char(service_url):
108108
raise ValueError(
109-
'The url shouldn\'t start or end with curly brackets or quotes. '
110-
'Be sure to remove any {} and \" characters surrounding your url'
109+
'The service url shouldn\'t start or end with curly brackets or quotes. '
110+
'Be sure to remove any {} and \" characters surrounding your service url'
111111
)
112-
self.url = url
112+
self.service_url = service_url
113113

114114
def get_authenticator(self):
115115
"""
@@ -164,7 +164,10 @@ def prepare_request(self, method, url, headers=None,
164164
params=None, data=None, files=None, **kwargs):
165165
request = {'method': method}
166166

167-
request['url'] = self.url + url
167+
# validate the service url is set
168+
if not self.service_url:
169+
raise ValueError('The service_url is required')
170+
request['url'] = self.service_url + url
168171

169172
headers = remove_null_values(headers) if headers else {}
170173
headers = cleanup_values(headers)

ibm_cloud_sdk_core/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,15 @@ def read_from_vcap_services(service_name):
151151
return vcap_service_credentials
152152

153153
def contruct_authenticator(config):
154-
auth_type = config.get('auth_type') if config.get('auth_type') else 'iam'
154+
auth_type = config.get('auth_type').lower() if config.get('auth_type') else 'iam'
155155
authenticator = None
156156
from .authenticators import BasicAuthenticator, BearerTokenAuthenticator, CloudPakForDataAuthenticator, IAMAuthenticator, NoAuthAuthenticator
157157

158158
if auth_type == 'basic':
159159
authenticator = BasicAuthenticator(
160160
username=config.get('username'),
161161
password=config.get('password'))
162-
elif auth_type == 'bearerToken':
162+
elif auth_type == 'bearertoken':
163163
authenticator = BearerTokenAuthenticator(
164164
bearer_token=config.get('bearer_token'))
165165
elif auth_type == 'cp4d':
@@ -175,7 +175,7 @@ def contruct_authenticator(config):
175175
client_id=config.get('client_id'),
176176
client_secret=config.get('client_secret'),
177177
disable_ssl_verification=config.get('auth_disable_ssl'))
178-
elif auth_type == 'noAuth':
178+
elif auth_type == 'noauth':
179179
authenticator = NoAuthAuthenticator()
180180

181181
return authenticator

test/test_base_service.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ class AnyServiceV1(BaseService):
1616

1717
def __init__(self,
1818
version,
19-
url=default_url,
19+
service_url=default_url,
2020
authenticator=None,
2121
disable_ssl_verification=False):
2222
BaseService.__init__(
2323
self,
24-
url=url,
24+
service_url=service_url,
2525
authenticator=authenticator,
2626
disable_ssl_verification=disable_ssl_verification,
2727
display_name='Watson')
@@ -162,7 +162,7 @@ def test_iam():
162162
status=200)
163163
responses.add(
164164
responses.GET,
165-
'https://gateway-s.watsonplatform.net/watson/api',
165+
url='https://gateway.watsonplatform.net/test/api',
166166
body=json.dumps({
167167
"foobar": "baz"
168168
}),
@@ -358,13 +358,13 @@ def test_default_headers():
358358
with pytest.raises(TypeError):
359359
service.set_default_headers('xxx')
360360

361-
def test_set_url():
361+
def test_set_service_url():
362362
service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())
363363
with pytest.raises(ValueError) as err:
364-
service.set_url('{url}')
365-
assert str(err.value) == 'The url shouldn\'t start or end with curly brackets or quotes. Be sure to remove any {} and \" characters surrounding your url'
364+
service.set_service_url('{url}')
365+
assert str(err.value) == 'The service url shouldn\'t start or end with curly brackets or quotes. Be sure to remove any {} and \" characters surrounding your service url'
366366

367-
service.set_url('my_url')
367+
service.set_service_url('my_url')
368368

369369
def test_get_authenticator():
370370
auth = BasicAuthenticator('my_username', 'my_password')
@@ -420,3 +420,9 @@ def test_json():
420420
service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())
421421
req = service.prepare_request('POST', url='', headers={'X-opt-out': True}, data={'hello': 'world'})
422422
assert req.get('data') == "{\"hello\": \"world\"}"
423+
424+
def test_service_url_not_set():
425+
service = BaseService(service_url='', authenticator=NoAuthAuthenticator(), display_name='Watson')
426+
with pytest.raises(ValueError) as err:
427+
service.prepare_request('POST', url='')
428+
assert str(err.value) == 'The service_url is required'

0 commit comments

Comments
 (0)