Skip to content

Commit 6626f17

Browse files
author
Rasmus Lager
committed
using GanException for validation errors and response.raise_for_status HTTPError for api problems
1 parent a4b15ec commit 6626f17

7 files changed

+30
-25
lines changed

ganapi/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
from list_manager import ListManager
55
from attribute import Attribute
66
from contact import Contact
7-
from list import List
7+
from list import List
8+
from gan_exception import GanException

ganapi/api.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import json
22
import requests
3-
import math
4-
from requests.exceptions import RequestException
5-
6-
3+
from gan_exception import GanException
74
class Api(object):
85
"""
96
Handles connection to the API
@@ -41,7 +38,7 @@ def call(self, method, resource_path, payload=None):
4138
:param resource_path string The path to the resource (e.g. contacts/john@example.com/)
4239
:param payload string The data that is sent to the service. Not used for GET or DELETE.
4340
:return response The Requests response object from the service.
44-
:raises Exception
41+
:raises GanException in case of invalid HTTP method or HTTPError
4542
"""
4643
uri = u'{base_uri}{resource_path}'.format(base_uri=self.base_uri,
4744
resource_path=resource_path)
@@ -59,8 +56,10 @@ def call(self, method, resource_path, payload=None):
5956
response = requests.put(uri, headers=self.headers, data=payload)
6057
elif method == 'PATCH':
6158
response = requests.patch(uri, headers=self.headers, data=payload)
59+
else:
60+
raise GanException(u'Invalid HTTP method',
61+
u'{method} is not a valid HTTP method! Valid HTTP methods are GET, POST, DELETE, PUT, PATCH.'.format(method=method))
6262

63-
if math.floor(response.status_code / 100) != 2:
64-
raise RequestException(response.status_code, response.content)
63+
response.raise_for_status()
6564

66-
return response
65+
return response

ganapi/entity_manager.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import urllib
22
from helpers import PaginatedResultSet
3-
3+
from api import GanException
44

55
class EntityManager(object):
66
"""
@@ -67,7 +67,7 @@ def lookup_path(self, entity):
6767

6868
lookup_field = self.lookup_field
6969
if not getattr(entity, lookup_field):
70-
raise Exception(u'Missing required property: {lookup_field}'.format(lookup_field=lookup_field))
70+
raise GanException(u'Missing required property', u'Missing: {lookup_field}'.format(lookup_field=lookup_field))
7171

7272
return self.get_path(getattr(entity, lookup_field))
7373

@@ -134,8 +134,8 @@ def save(self, entity, overwrite=False):
134134
:param entity: The entity object to update/save.
135135
:param overwrite: Set to True if you want
136136
:return: The updated/created entity.
137-
:raises RequestException if there is an error from the API
138-
:raises Exception if the normalixation fails, i.e. missing lookup field.
137+
:raises HTTPError if there is an error from the API
138+
:raises GanException if the normalization fails, i.e. missing lookup field.
139139
"""
140140
data = self.normalize_entity(entity)
141141
if overwrite:

ganapi/gan_exception.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class GanException(Exception):
2+
def __init__(self, error_type, error_message, *args, **kwargs):
3+
self.error_type = error_type
4+
self.error_message = error_message
5+
6+
def __str__(self):
7+
return u'({error_type}) {error_message}'.format(error_type=self.error_type,
8+
error_message=self.error_message)

ganapi/test_attribute_manager.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from attribute import Attribute
44
from attribute_manager import AttributeManager
55
from httmock import HTTMock, all_requests
6+
from requests import HTTPError
67

78

89
class ContactManagerTest(unittest.TestCase):
@@ -29,15 +30,15 @@ def test_get_existing_attribute(self):
2930

3031
@all_requests
3132
def get_non_existing_attribute_mock(self, url, request):
32-
self.assertEqual(url.path, self.start_path + '/attribute/non_existing/')
33+
self.assertEqual(url.path, self.start_path + '/attributes/non_existing/')
3334
status_code = 404
3435
content = '{"detail":"Not found."}'
3536
return {'status_code': status_code,
3637
'content': content}
3738

3839
def test_get_non_existing_attribute(self):
3940
with HTTMock(self.get_non_existing_attribute_mock):
40-
self.assertRaises(Exception, self.attribute_manager.get, 'not_existing')
41+
self.assertRaises(HTTPError, self.attribute_manager.get, 'non_existing')
4142

4243
@all_requests
4344
def create_attribute_mock(self, url, request):

ganapi/test_contact_manager.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import unittest
2-
from api import Api
2+
from api import Api, GanException
33
from contact import Contact
44
from contact_manager import ContactManager
55
from httmock import HTTMock, all_requests
6-
6+
from requests import HTTPError
77

88
class ContactManagerTest(unittest.TestCase):
99
def setUp(self):
@@ -48,11 +48,7 @@ def non_existing_contact_mock(self, url, request):
4848

4949
def test_non_existing_contact(self):
5050
with HTTMock(self.non_existing_contact_mock):
51-
try:
52-
non_existing_contact = self.contact_manager.get('noone@nothing.com')
53-
except Exception as e:
54-
self.assertEqual(e[0], 404)
55-
self.assertEqual(e[1], '{"detail":"Not found."}')
51+
self.assertRaises(HTTPError, self.contact_manager.get, 'noone@nothing.com')
5652

5753
@all_requests
5854
def create_new_contact_mock(self, url, request):
@@ -94,7 +90,7 @@ def test_update_without_email(self):
9490
contact = self.contact_manager.create()
9591
contact.first_name = 'John'
9692
contact.set_persisted()
97-
self.assertRaises(Exception, contact.save)
93+
self.assertRaises(GanException, contact.save)
9894

9995
@all_requests
10096
def overwrite_contact_mock(self, url, request):

ganapi/test_list_manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from list import List
44
from list_manager import ListManager
55
from httmock import HTTMock, all_requests
6-
6+
from gan_exception import GanException
77

88
class ListManagerTest(unittest.TestCase):
99
def setUp(self):
@@ -32,7 +32,7 @@ def test_update_without_hash(self):
3232
list.name = 'list'
3333
list.sender = 'John Doe'
3434
list.set_persisted()
35-
self.assertRaises(Exception, list.save)
35+
self.assertRaises(GanException, list.save)
3636

3737
@all_requests
3838
def create_new_list_mock(self, url, request):

0 commit comments

Comments
 (0)