Skip to content

Commit

Permalink
added error catching json converserions
Browse files Browse the repository at this point in the history
  • Loading branch information
alanwia committed Sep 6, 2018
1 parent a2432f2 commit 5c39c74
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 deletions.
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
1.1.0
1.1.1
43 changes: 25 additions & 18 deletions wia/resource.py
@@ -1,6 +1,3 @@
import time
import logging

from wia import Wia
from wia.rest_client import post, get, put, delete
from wia.error import WiaError, WiaValidationError, WiaUnauthorisedError, WiaForbiddenError, WiaNotFoundError
Expand All @@ -27,6 +24,16 @@ def error_response(response):
else:
return WiaError(response)

@staticmethod
def json_converter(response):
resJson = None
try:
resJson = response.json()
except json.decoder.JSONDecodeError:
response = WiaResource.error_response(response)

return resJson or response

class WiaResourceDelete(WiaResource):
def __init__(self, **kwargs):
self.id = (kwargs['id'] if 'id' in kwargs else None)
Expand All @@ -49,7 +56,7 @@ def create(cls, **kwargs):
path = 'spaces'
response = post(path, kwargs)
if WiaResource.is_success(response):
return cls(**response.json())
return cls(**WiaResource.json_converter(response))
else:
return WiaResource.error_response(response)

Expand All @@ -63,7 +70,7 @@ def retrieve(cls, id):
path = 'spaces/' + id
response = get(path)
if WiaResource.is_success(response):
return cls(**response.json())
return cls(**WiaResource.json_converter(response))
else:
return WiaResource.error_response(response)

Expand All @@ -74,7 +81,7 @@ def update(cls, **kwargs):
del dictCopy['id']
response = put(path, dictCopy)
if WiaResource.is_success(response):
return cls(**response.json())
return cls(**WiaResource.json_converter(response))
else:
return WiaResource.error_response(response)

Expand Down Expand Up @@ -105,7 +112,7 @@ def create(cls, **kwargs):
path = 'devices'
response = post(path, kwargs)
if WiaResource.is_success(response):
return cls(**response.json())
return cls(**WiaResource.json_converter(response))
else:
return WiaResource.error_response(response)

Expand All @@ -114,7 +121,7 @@ def retrieve(cls, id):
path = 'devices/' + id
response = get(path)
if WiaResource.is_success(response):
return cls(**response.json())
return cls(**WiaResource.json_converter(response))
else:
return WiaResource.error_response(response)

Expand All @@ -125,7 +132,7 @@ def update(cls, **kwargs):
#del dictCopy['id']
response = put(path, dictCopy)
if WiaResource.is_success(response):
return cls(**response.json())
return cls(**WiaResource.json_converter(response))
else:
return WiaResource.error_response(response)

Expand Down Expand Up @@ -168,7 +175,7 @@ def publish(cls, **kwargs):
else:
response = post(path, kwargs)
if WiaResource.is_success(response):
return cls(**response.json())
return cls(**WiaResource.json_converter(response))
else:
return WiaResource.error_response(response)

Expand Down Expand Up @@ -223,7 +230,7 @@ def publish(cls, **kwargs):
else:
response = post(path, kwargs)
if WiaResource.is_success(response):
return cls(**response.json())
return cls(**WiaResource.json_converter(response))
else:
return WiaResource.error_response(response)

Expand Down Expand Up @@ -264,7 +271,7 @@ def create(cls, **kwargs):
path = 'commands'
response = post(path, kwargs)
if WiaResource.is_success(response):
return cls(**response.json())
return cls(**WiaResource.json_converter(response))
else:
return WiaResource.error_response(response)

Expand All @@ -273,7 +280,7 @@ def retrieve(cls, id):
path = 'commands/' + id
response = get(path)
if WiaResource.is_success(response):
return cls(**response.json())
return cls(**WiaResource.json_converter(response))
else:
return WiaResource.error_response(response)

Expand All @@ -284,7 +291,7 @@ def update(cls, **kwargs):
del dictCopy['id']
response = put(path, dictCopy)
if WiaResource.is_success(response):
return cls(**response.json())
return cls(**WiaResource.json_converter(response))
else:
return WiaResource.error_response(response)

Expand Down Expand Up @@ -329,7 +336,7 @@ def run(cls, **kwargs):
else:
response = post('commands/run', kwargs)
if WiaResource.is_success(response):
return cls(**response.json())
return cls(**WiaResource.json_converter(response))
else:
return WiaResource.error_response(response)

Expand All @@ -351,7 +358,7 @@ def run(cls, **kwargs):
# else:
# response = post(path, kwargs)
# if WiaResource.is_success(response):
# return cls(**response.json())
# return cls(**WiaResource.json_converter(response))
# else:
# return WiaResource.error_response(response)
#
Expand Down Expand Up @@ -400,7 +407,7 @@ def __init__(self, **kwargs):
def create(cls, kwargs):
response = post('auth/token', kwargs)
if WiaResource.is_success(response):
return cls(**response.json())
return cls(**WiaResource.json_converter(response))
else:
return WiaResource.error_response(response)

Expand All @@ -414,6 +421,6 @@ def __init__(self, **kwargs):
def retrieve(cls):
response = get('whoami')
if WiaResource.is_success(response):
return cls(**response.json())
return cls(**WiaResource.json_converter(response))
else:
return WiaResource.error_response(response)
6 changes: 4 additions & 2 deletions wia/test/resources/test_events.py
Expand Up @@ -24,15 +24,17 @@ def test_events_publish_file(self):
wia = Wia()
wia.access_token = os.environ['device_secret_key']
dir_path = os.path.dirname(os.path.realpath(__file__))
result = wia.Event.publish(name='test_event_other_filesud', data=1300, file=open(dir_path+'/test-file.txt', 'rb'))
event = Wia().Event.publish(name='test_event_other_filesud', data=1300, file=open(dir_path+'/test-file.txt', 'rb'))
self.assertTrue(event.id is not None)
wia.access_token = None


def test_events_publish_file_text(self):
wia = Wia()
wia.access_token = os.environ['device_secret_key']
dir_path = os.path.dirname(os.path.realpath(__file__))
result = wia.Event.publish(name='test_event_other_file', file=(dir_path+'/test-file.txt'))
event = wia.Event.publish(name='test_event_other_file', file=(dir_path+'/test-file.txt'))
self.assertTrue(event.id is not None)
wia.access_token = None

# ERROR TESTS
Expand Down
2 changes: 1 addition & 1 deletion wia/version.py
@@ -1 +1 @@
VERSION = '1.1.0'
VERSION = '1.1.1'

0 comments on commit 5c39c74

Please sign in to comment.