summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvgeny Zinoviev <me@ch1p.io>2021-03-23 01:57:20 +0300
committerEvgeny Zinoviev <me@ch1p.io>2021-03-23 01:57:20 +0300
commit8c86e34794918e4b2aafbb65c09d16486c1e80a8 (patch)
treec6b7bbfcc1649d6142c72a4bb7878076db87de7f
parentbfc0d6ecd36713b38e2ab87473ba32e854b79bd5 (diff)
handle api errors
-rw-r--r--e3372.py52
1 files changed, 43 insertions, 9 deletions
diff --git a/e3372.py b/e3372.py
index 38a042b..35eb411 100644
--- a/e3372.py
+++ b/e3372.py
@@ -1,4 +1,5 @@
import requests
+import sys
from datetime import datetime
from bs4 import BeautifulSoup
@@ -27,13 +28,13 @@ class E3372:
self.headers['__RequestVerificationToken'] = token
self.headers['Content-Type'] = 'text/xml'
- def device_information(self):
- response = self.request('device/information')
- print(response)
-
- def device_signal(self):
- response = self.request('device/signal')
- print(response)
+ # def device_information(self):
+ # response = self.request('device/information')
+ # print(response)
+ #
+ # def device_signal(self):
+ # response = self.request('device/signal')
+ # print(response)
def get_sms(self, count=10, page=1):
request = build_request({
@@ -65,7 +66,27 @@ class E3372:
def request(self, endpoint: str, data=None):
url = f'http://{self.ip}/api/{endpoint}'
r = requests.get(url) if data is None else requests.post(url, data=data)
- return BeautifulSoup(r.text, 'lxml-xml').find('response')
+
+ soup = BeautifulSoup(r.text, 'lxml-xml')
+
+ error = soup.find('error')
+ if error:
+ code = 0
+ message = ''
+
+ code_node = error.find('code')
+ message_node = error.find('message')
+
+ if code_node:
+ code = int(code_node.get_text())
+
+ if message_node:
+ message = message_node.get_text()
+
+ raise E3372Error(code, message=message)
+
+ return soup.find('response')
+
class SMS:
@@ -77,4 +98,17 @@ class SMS:
def timestamp(self):
# input example: 2020-08-11 14:55:51
- return int(datetime.strptime(self.date, '%Y-%m-%d %H-%M-%S').strftime("%s")) \ No newline at end of file
+ return int(datetime.strptime(self.date, '%Y-%m-%d %H-%M-%S').strftime("%s"))
+
+
+class E3372Error(Exception):
+ def __init__(self, error_code, message='', *args, **kwargs):
+ self.error_code = error_code
+ self.traceback = sys.exc_info()
+
+ try:
+ msg = '[{0}] {1}'.format(error_code.name, message.format(*args, **kwargs))
+ except (IndexError, KeyError):
+ msg = '[{0}] {1}'.format(error_code.name, message)
+
+ super().__init__(msg) \ No newline at end of file