Skip to content

Commit d02342b

Browse files
committed
feat(filename): Update filename when not present, errorMessage
1 parent 7c27097 commit d02342b

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

ibm_cloud_sdk_core/api_exception.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ def _get_error_message(self, response):
5757
error_message = error_json['error']
5858
elif 'message' in error_json:
5959
error_message = error_json['message']
60+
elif 'errorMessage' in error_json:
61+
error_message = error_json['errorMessage']
6062
return error_message
6163
except:
6264
return response.text or error_message

ibm_cloud_sdk_core/base_service.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# limitations under the License.
1616

1717
import os
18-
from os.path import dirname, isfile, join, expanduser, abspath
18+
from os.path import dirname, isfile, join, expanduser, abspath, basename
1919
import platform
2020
import json as json_import
2121
import sys
@@ -277,9 +277,9 @@ def request(self, method, url, accept_json=False, headers=None,
277277
params=None, json=None, data=None, files=None, **kwargs):
278278
full_url = self.url + url
279279

280-
headers = CaseInsensitiveDict(headers)
281-
headers = remove_null_values(headers)
280+
headers = remove_null_values(headers) if headers else {}
282281
headers = cleanup_values(headers)
282+
headers = CaseInsensitiveDict(headers)
283283

284284
if self.default_headers is not None:
285285
headers.update(self.default_headers)
@@ -319,6 +319,14 @@ def request(self, method, url, accept_json=False, headers=None,
319319
if self.verify is not None:
320320
kwargs['verify'] = self.verify
321321

322+
if files is not None:
323+
for k, file_tuple in files.items():
324+
if file_tuple and len(file_tuple) == 3 and file_tuple[0] is None:
325+
file = file_tuple[1]
326+
if file and hasattr(file, 'name'):
327+
filename = basename(file.name)
328+
files[k] = (filename, file_tuple[1], file_tuple[2])
329+
322330
response = requests.request(method=method, url=full_url,
323331
cookies=self.jar, auth=auth,
324332
headers=headers,

test/test_api_exception.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ def test_api_exception():
4949
exception = ApiException(500, http_response=mock_response)
5050
assert exception.message == 'Unknown error'
5151

52+
responses.add(responses.GET,
53+
'https://test-errormessage.com',
54+
status=500,
55+
body=json.dumps({'errorMessage': 'IAM error message'}),
56+
content_type='application/json')
57+
mock_response = requests.get('https://test-errormessage.com')
58+
exception = ApiException(500, http_response=mock_response)
59+
assert exception.message == 'IAM error message'
60+
5261
responses.add(responses.GET,
5362
'https://test-for-text.com',
5463
status=500,

test/test_base_service.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,3 +387,18 @@ def test_user_agent_header():
387387

388388
response = service.request('GET', url='', headers=None)
389389
assert response.get_result().request.headers.__getitem__('user-agent') == user_agent_header['User-Agent']
390+
391+
@responses.activate
392+
def test_files():
393+
service = AnyServiceV1('2018-11-20', username='username', password='password')
394+
395+
responses.add(responses.GET,
396+
'https://gateway.watsonplatform.net/test/api',
397+
status=200,
398+
body=json.dumps({'foo': 'bar'}),
399+
content_type='application/json')
400+
form_data = {}
401+
file = os.path.join(os.path.dirname(__file__), '../resources/ibm-credentials.env')
402+
form_data['file1'] = (None, file, 'application/octet-stream')
403+
form_data['string1'] = (None, 'hello', 'text.plain')
404+
service.request('GET', url='', headers={'X-opt-out': True}, files=form_data)

0 commit comments

Comments
 (0)