-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f89e9a1
commit aaabc2f
Showing
4 changed files
with
82 additions
and
60 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,61 @@ | ||
# GOAL: Handle exceptions for SmtApi. | ||
|
||
# Take a HTTP response object and translate it into an Exception | ||
# instance. | ||
def handle_error_response(resp): | ||
# Mapping of API response codes to exception classes | ||
codes = { | ||
-1: SmtApiError, | ||
'400': BadRequest, | ||
'401': AuthFail, | ||
'403': HeaderMissing, | ||
'500': GenericError, | ||
} | ||
|
||
error = resp.json().get('error', {}) | ||
message = error.get('errorMessage') | ||
code = error.get('errorCode', -1) | ||
data = error.get('errorKey', {}) | ||
|
||
# Build the appropriate exception class with as much | ||
# data as we can pull from the API response and raise | ||
# it. | ||
raise codes[code](message=message, code=code, data=data, response=resp) | ||
|
||
|
||
class SmtApiError(Exception): | ||
response = None | ||
data = {} | ||
code = -1 | ||
message = "An unknown error occurred" | ||
|
||
def __init__(self, message=None, code=None, data={}, response=None): | ||
self.response = response | ||
if message: | ||
self.message = message | ||
if code: | ||
self.code = code | ||
if data: | ||
self.data = data | ||
|
||
def __str__(self): | ||
if self.code: | ||
return '{}: {}'.format(self.code, self.message) | ||
return self.message | ||
|
||
# Specific exception classes | ||
|
||
class BadRequest(SmtApiError): | ||
pass | ||
|
||
|
||
class AuthFail(SmtApiError): | ||
pass | ||
|
||
|
||
class HeaderMissing(SmtApiError): | ||
pass | ||
|
||
|
||
class GenericError(SmtApiError): | ||
# GOAL: Handle exceptions for SmtApi. | ||
|
||
# Take a HTTP response object and translate it into an Exception | ||
# instance. | ||
def handle_error_response(resp): | ||
# Mapping of API response codes to exception classes | ||
codes = { | ||
-1: SmtApiError, | ||
'400': BadRequest, | ||
'401': AuthFail, | ||
'403': HeaderMissing, | ||
'500': GenericError, | ||
} | ||
|
||
error = resp.json().get('error', {}) | ||
message = error.get('errorMessage') | ||
code = error.get('errorCode', -1) | ||
data = error.get('errorKey', {}) | ||
|
||
# Build the appropriate exception class with as much | ||
# data as we can pull from the API response and raise | ||
# it. | ||
raise codes[code](message=message, code=code, data=data, response=resp) | ||
|
||
|
||
class SmtApiError(Exception): | ||
response = None | ||
data = {} | ||
code = -1 | ||
message = "An unknown error occurred" | ||
|
||
def __init__(self, message=None, code=None, data={}, response=None): | ||
self.response = response | ||
if message: | ||
self.message = message | ||
if code: | ||
self.code = code | ||
if data: | ||
self.data = data | ||
|
||
def __str__(self): | ||
if self.code: | ||
return '{}: {}'.format(self.code, self.message) | ||
return self.message | ||
|
||
# Specific exception classes | ||
|
||
class BadRequest(SmtApiError): | ||
pass | ||
|
||
|
||
class AuthFail(SmtApiError): | ||
pass | ||
|
||
|
||
class HeaderMissing(SmtApiError): | ||
pass | ||
|
||
|
||
class GenericError(SmtApiError): | ||
pass |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import setuptools | ||
|
||
with open("README.md", "r", encoding="utf-8") as fh: | ||
long_description = fh.read() | ||
|
||
setuptools.setup( | ||
name="SmtApi", # Replace with your own username | ||
version="v0.1-alpha", | ||
author="Joshua Edwards", | ||
author_email="[email protected]", | ||
description="An API for SmartMeterTexas", | ||
long_description=long_description, | ||
long_description_content_type="text/markdown", | ||
url="https://github.com/pypa/sampleproject", | ||
packages=setuptools.find_packages(), | ||
classifiers=[ | ||
"Programming Language :: Python :: 3", | ||
"License :: OSI Approved :: MIT License", | ||
"Operating System :: OS Independent", | ||
], | ||
python_requires='>=3.6', | ||
) |