Skip to content

Commit 98e7b5f

Browse files
committed
Refactor package exception class names to improve clarity
Change package exception names to more clearly delineate the top level ‘package’ exception class (now ‘ciscosparkapiException’) from the exceptions raised in response to error response codes from the Spark cloud (now ‘SparkApiError’). Also update module code organization to consistently place module constants immediately following the module’s import statements.
1 parent 6798e69 commit 98e7b5f

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

ciscosparkapi/exceptions.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
"""ciscosparkapi package-level exceptions."""
2-
3-
4-
class CiscoSparkApiException(Exception):
5-
"""Base class for all ciscosparkapi package exceptions."""
6-
7-
def __init__(self, *args, **kwargs):
8-
super(CiscoSparkApiException, self).__init__(*args, **kwargs)
1+
"""ciscosparkapi exception classes."""
92

103

114
SPARK_RESPONSE_CODES = {
@@ -26,7 +19,14 @@ def __init__(self, *args, **kwargs):
2619
}
2720

2821

29-
class CiscoSparkApiError(CiscoSparkApiException):
22+
class ciscosparkapiException(Exception):
23+
"""Base class for all ciscosparkapi package exceptions."""
24+
25+
def __init__(self, *args, **kwargs):
26+
super(ciscosparkapiException, self).__init__(*args, **kwargs)
27+
28+
29+
class SparkApiError(ciscosparkapiException):
3030
"""Errors returned by requests to the Cisco Spark cloud APIs."""
3131

3232
def __init__(self, response_code, request=None, response=None):
@@ -37,7 +37,9 @@ def __init__(self, response_code, request=None, response=None):
3737
response_text = SPARK_RESPONSE_CODES.get(response_code)
3838
if response_text:
3939
self.response_text = response_text
40-
error_message = "[%s]: %s" % (response_code, response_text)
40+
error_message = "Response Code [%s] - %s" % \
41+
(response_code, response_text)
4142
else:
42-
error_message = "[%s]: Unknown Response Code" % response_code
43-
super(CiscoSparkApiError, self).__init__(error_message)
43+
error_message = "Response Code [%s] - Unknown Response Code" % \
44+
response_code
45+
super(SparkApiError, self).__init__(error_message)

ciscosparkapi/restsession.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
33

44
import urlparse
55
import requests
6-
import exceptions
6+
from .exceptions import ciscosparkapiException, SparkApiError
77

88

99
# Default api.ciscospark.com base URL
1010
DEFAULT_API_URL = 'https://api.ciscospark.com/v1/'
1111

1212
# Cisco Spark cloud Expected Response Codes (HTTP Response Codes)
13-
ERC = {'GET': 200,
14-
'POST': 200,
15-
'PUT': 200,
16-
'DELETE': 204}
13+
ERC = {
14+
'GET': 200,
15+
'POST': 200,
16+
'PUT': 200,
17+
'DELETE': 204
18+
}
1719

1820

1921
def _validate_base_url(base_url):
@@ -23,7 +25,7 @@ def _validate_base_url(base_url):
2325
else:
2426
error_message = "base_url must contain a valid scheme (protocol " \
2527
"specifier) and network location (hostname)"
26-
raise exceptions.CiscoSparkApiException(error_message)
28+
raise ciscosparkapiException(error_message)
2729

2830

2931
def _raise_if_extra_kwargs(kwargs):
@@ -33,9 +35,9 @@ def _raise_if_extra_kwargs(kwargs):
3335

3436
def _check_response_code(response, erc):
3537
if response.status_code != erc:
36-
raise exceptions.CiscoSparkApiError(response.status_code,
37-
request=response.request,
38-
response=response)
38+
raise SparkApiError(response.status_code,
39+
request=response.request,
40+
response=response)
3941

4042

4143
def _extract_and_parse_json(response):
@@ -137,7 +139,7 @@ def get_items(self, url, params=None, **kwargs):
137139
else:
138140
error_message = "'items' object not found in JSON data: %r" \
139141
% json_page
140-
raise exceptions.CiscoSparkApiException(error_message)
142+
raise ciscosparkapiException(error_message)
141143

142144
def post(self, url, json_dict, **kwargs):
143145
# Process args

0 commit comments

Comments
 (0)