Skip to content

Commit cf74671

Browse files
authored
feat: rename ApiException.code to ApiException.status_code (#185)
This commit fixes a small inconsistency in the naming of the HTTP status code attributes between the successful and the error responses. From now on, the right way to obtain the status code from the `ApiException` is, using the `status_code` attribute. `code` will remain for some time, but now it's deprecated and it will be removed in the near future. Signed-off-by: Norbert Biczo <[email protected]>
1 parent e31c85e commit cf74671

File tree

5 files changed

+26
-6
lines changed

5 files changed

+26
-6
lines changed

ibm_cloud_sdk_core/api_exception.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17+
import warnings
1718
from http import HTTPStatus
1819
from typing import Optional
1920

@@ -39,15 +40,28 @@ def __init__(self, code: int, *, message: Optional[str] = None, http_response: O
3940
# Call the base class constructor with the parameters it needs
4041
super().__init__(message)
4142
self.message = message
42-
self.code = code
43+
self.status_code = code
4344
self.http_response = http_response
4445
self.global_transaction_id = None
4546
if http_response is not None:
4647
self.global_transaction_id = http_response.headers.get('X-Global-Transaction-ID')
4748
self.message = self.message if self.message else self._get_error_message(http_response)
4849

50+
# pylint: disable=fixme
51+
# TODO: delete this by the end of 2024.
52+
@property
53+
def code(self):
54+
"""The old `code` property with a deprecation warning."""
55+
56+
warnings.warn(
57+
'Using the `code` attribute on the `ApiException` is deprecated and'
58+
'will be removed in the future. Use `status_code` instead.',
59+
DeprecationWarning,
60+
)
61+
return self.status_code
62+
4963
def __str__(self) -> str:
50-
msg = 'Error: ' + str(self.message) + ', Code: ' + str(self.code)
64+
msg = 'Error: ' + str(self.message) + ', Status code: ' + str(self.status_code)
5165
if self.global_transaction_id is not None:
5266
msg += ' , X-global-transaction-id: ' + str(self.global_transaction_id)
5367
return msg

ibm_cloud_sdk_core/detailed_response.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def get_headers(self) -> Optional[dict]:
6565
"""
6666
return self.headers
6767

68-
def get_status_code(self) -> int:
68+
def get_status_code(self) -> Union[int, None]:
6969
"""The HTTP status code of the service request.
7070
7171
Returns:

test/test_api_exception.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,4 @@ def test_api_exception():
8585
mock_response = requests.get('https://test-for-text.com', timeout=None)
8686
exception = ApiException(500, http_response=mock_response)
8787
assert exception.message == 'plain text error'
88-
assert str(exception) == 'Error: plain text error, Code: 500 , X-global-transaction-id: xx'
88+
assert str(exception) == 'Error: plain text error, Status code: 500 , X-global-transaction-id: xx'

test/test_base_service.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# coding=utf-8
2-
# pylint: disable=missing-docstring,protected-access,too-few-public-methods
2+
# pylint: disable=missing-docstring,protected-access,too-few-public-methods,too-many-lines
33
import gzip
44
import json
55
import os
@@ -357,6 +357,7 @@ def test_request_server_error():
357357
prepped = service.prepare_request('GET', url='')
358358
service.send(prepped)
359359
assert err.value.code == 500
360+
assert err.value.status_code == 500
360361
assert err.value.http_response.headers['Content-Type'] == 'application/json'
361362
assert err.value.message == 'internal server error'
362363

@@ -399,6 +400,7 @@ def test_request_success_invalid_json():
399400
prepped = service.prepare_request('GET', url='')
400401
service.send(prepped)
401402
assert err.value.code == 200
403+
assert err.value.status_code == 200
402404
assert err.value.http_response.headers['Content-Type'] == 'application/json; charset=utf8'
403405
assert isinstance(err.value.__cause__, requests.exceptions.JSONDecodeError)
404406
assert "Expecting ':' delimiter: line 1" in str(err.value.__cause__)
@@ -453,6 +455,7 @@ def test_request_fail_401_nonerror_json():
453455
prepped = service.prepare_request('GET', url='')
454456
service.send(prepped)
455457
assert err.value.code == 401
458+
assert err.value.status_code == 401
456459
assert err.value.http_response.headers['Content-Type'] == 'application/json'
457460
assert err.value.message == error_msg
458461

@@ -473,6 +476,7 @@ def test_request_fail_401_error_json():
473476
prepped = service.prepare_request('GET', url='')
474477
service.send(prepped)
475478
assert err.value.code == 401
479+
assert err.value.status_code == 401
476480
assert err.value.http_response.headers['Content-Type'] == 'application/json'
477481
assert err.value.message == error_msg
478482

@@ -492,6 +496,7 @@ def test_request_fail_401_nonjson():
492496
prepped = service.prepare_request('GET', url='')
493497
service.send(prepped)
494498
assert err.value.code == 401
499+
assert err.value.status_code == 401
495500
assert err.value.http_response.headers['Content-Type'] == 'text/plain'
496501
assert err.value.message == response_body
497502

@@ -514,6 +519,7 @@ def test_request_fail_401_badjson():
514519
prepped = service.prepare_request('GET', url='')
515520
service.send(prepped)
516521
assert err.value.code == 401
522+
assert err.value.status_code == 401
517523
assert err.value.http_response.headers['Content-Type'] == 'application/json'
518524
assert err.value.message == response_body
519525

test/test_container_token_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ def test_authenticate_fail_iam():
251251
with pytest.raises(ApiException) as err:
252252
authenticator.authenticate(request)
253253

254-
assert str(err.value) == 'Error: Bad Request, Code: 400'
254+
assert str(err.value) == 'Error: Bad Request, Status code: 400'
255255

256256

257257
@mock_iam_response

0 commit comments

Comments
 (0)