Skip to content

Commit e5d9066

Browse files
author
Ricardo
authored
Merge pull request #15 from cuenca-mx/exceptions
Exceptions
2 parents ebf9eda + be772ae commit e5d9066

19 files changed

+2819
-20
lines changed

ivoy/client.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from requests import Response, Session
55

6-
from .exc import ExpiredTokens, IvoyException
6+
from .exc import ExpiredTokens, raise_ivoy_exception
77
from .resources import Budget, CarrierLocation, Order, OrderSharing, Resource
88

99
API_URL = os.environ['IVOY_URL']
@@ -126,7 +126,5 @@ def _check_response(response: Response) -> None:
126126
# All error codes come from API as negative numbers.
127127
raise ExpiredTokens(156, 'Tokens have expired.')
128128
else:
129-
raise IvoyException(
130-
json['code'], 'iVoy API error: {}'.format(json['message'])
131-
)
129+
raise_ivoy_exception(json['code'], json['message'])
132130
response.raise_for_status()

ivoy/exc.py

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,171 @@ class ExpiredTokens(IvoyException):
1515

1616
class NotEnoughAddresses(IvoyException):
1717
"""Addresses Needed to create Budget/Order"""
18+
19+
20+
class InvalidPhone(IvoyException):
21+
def __init__(self, **kwargs):
22+
message = f'Invalid or incomplete Phone Number'
23+
super().__init__(message=message, **kwargs)
24+
25+
26+
class InvalidInformation(IvoyException):
27+
def __init__(self, **kwargs):
28+
message = f'Invalid Information Provided'
29+
super().__init__(message=message, **kwargs)
30+
31+
32+
class NotAvailable(IvoyException):
33+
def __init__(self, **kwargs):
34+
message = f'System Not Available, try again later'
35+
super().__init__(message=message, **kwargs)
36+
37+
38+
class OutOFRange(IvoyException):
39+
def __init__(self, **kwargs):
40+
message = f'Address is out of range'
41+
super().__init__(message=message, **kwargs)
42+
43+
44+
class InvalidCode(IvoyException):
45+
def __init__(self, **kwargs):
46+
message = f'This code is not valid or already used'
47+
super().__init__(message=message, **kwargs)
48+
49+
50+
class AlreadyExists(IvoyException):
51+
def __init__(self, **kwargs):
52+
message = f'User with this information already exists'
53+
super().__init__(message=message, **kwargs)
54+
55+
56+
class MissingInformation(IvoyException):
57+
def __init__(self, **kwargs):
58+
message = f'Incomplete or missing information'
59+
super().__init__(message=message, **kwargs)
60+
61+
62+
class InvoiceError(IvoyException):
63+
def __init__(self, **kwargs):
64+
message = f'Invoice cannot be created for this order'
65+
super().__init__(message=message, **kwargs)
66+
67+
68+
class InvalidDate(IvoyException):
69+
def __init__(self, **kwargs):
70+
message = f'Invalid date try a different date'
71+
super().__init__(message=message, **kwargs)
72+
73+
74+
class InvalidVehicle(IvoyException):
75+
def __init__(self, **kwargs):
76+
message = f'Error on vehicle or not available'
77+
super().__init__(message=message, **kwargs)
78+
79+
80+
class DoesNotExists(IvoyException):
81+
def __init__(self, **kwargs):
82+
message = f'Could not find anything with the information provided'
83+
super().__init__(message=message, **kwargs)
84+
85+
86+
class UnableToCreate(IvoyException):
87+
def __init__(self, **kwargs):
88+
message = f'Unable to create or process try again later'
89+
super().__init__(message=message, **kwargs)
90+
91+
92+
class InsufficientFunds(IvoyException):
93+
def __init__(self, **kwargs):
94+
message = f'Insufficient Founds'
95+
super().__init__(message=message, **kwargs)
96+
97+
98+
class NotRegistered(IvoyException):
99+
def __init__(self, **kwargs):
100+
message = f'Found not registered with the information provided'
101+
super().__init__(message=message, **kwargs)
102+
103+
104+
IVOY_EXCEPTIONS = {
105+
-101: InvalidPhone,
106+
-102: InvalidPhone,
107+
-103: InvalidPhone,
108+
-104: AlreadyExists,
109+
-111: InvalidInformation,
110+
-112: InvalidDate,
111+
-113: OutOFRange,
112+
-114: OutOFRange,
113+
-117: NotAvailable,
114+
-118: NotAvailable,
115+
-119: InvalidCode,
116+
-120: InvalidCode,
117+
-121: NotRegistered,
118+
-122: InvalidCode,
119+
-123: AlreadyExists,
120+
-124: InvalidCode,
121+
-126: NotRegistered,
122+
-127: NotAvailable,
123+
-128: UnableToCreate,
124+
-132: InvalidInformation,
125+
-133: UnableToCreate,
126+
-134: UnableToCreate,
127+
-135: NotAvailable,
128+
-136: OutOFRange,
129+
-137: UnableToCreate,
130+
-139: InsufficientFunds,
131+
-141: InvalidInformation,
132+
-142: InvalidInformation,
133+
-144: NotRegistered,
134+
-145: OutOFRange,
135+
-150: OutOFRange,
136+
-152: InvalidCode,
137+
-153: InvalidCode,
138+
-154: InvalidCode,
139+
-157: InvoiceError,
140+
-159: InvoiceError,
141+
-160: NotAvailable,
142+
-161: UnableToCreate,
143+
-162: InvoiceError,
144+
-163: DoesNotExists,
145+
-164: NotAvailable,
146+
-169: InvalidDate,
147+
-170: AlreadyExists,
148+
-171: NotRegistered,
149+
-172: MissingInformation,
150+
-173: UnableToCreate,
151+
-174: DoesNotExists,
152+
-175: InvalidCode,
153+
-176: InvoiceError,
154+
-177: UnableToCreate,
155+
-178: InvoiceError,
156+
-179: UnableToCreate,
157+
-180: UnableToCreate,
158+
-182: MissingInformation,
159+
-192: InvalidInformation,
160+
-193: DoesNotExists,
161+
-194: InvalidInformation,
162+
-197: MissingInformation,
163+
-199: MissingInformation,
164+
-200: NotAvailable,
165+
-202: AlreadyExists,
166+
-208: AlreadyExists,
167+
-209: DoesNotExists,
168+
-213: OutOFRange,
169+
-214: OutOFRange,
170+
-247: MissingInformation,
171+
-248: MissingInformation,
172+
-249: MissingInformation,
173+
-250: InvalidInformation,
174+
-251: UnableToCreate,
175+
-252: InvalidVehicle,
176+
-258: InvalidVehicle,
177+
}
178+
179+
180+
def raise_ivoy_exception(code, message):
181+
if code in IVOY_EXCEPTIONS:
182+
ex = IVOY_EXCEPTIONS[code]
183+
raise ex(code=code)
184+
else:
185+
raise IvoyException(code, 'iVoy API error: {}'.format(message))

ivoy/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.1.0'
1+
__version__ = '0.2.0'
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
interactions:
2+
- request:
3+
body: '{"data": {"systemRequest": {"user": "USER", "password": "PASS"}}}'
4+
headers:
5+
Accept:
6+
- '*/*'
7+
Accept-Encoding:
8+
- gzip, deflate
9+
Connection:
10+
- keep-alive
11+
Content-Length:
12+
- '91'
13+
Content-Type:
14+
- application/json
15+
User-Agent:
16+
- python-requests/2.22.0
17+
method: POST
18+
uri: https://api.ivoy.dev/api/login/loginClient/json/web
19+
response:
20+
body:
21+
string: '{"code": 0, "token": {"access_token": "123"}}'
22+
headers:
23+
Connection:
24+
- keep-alive
25+
Content-Length:
26+
- '4891'
27+
Content-Security-Policy:
28+
- default-src 'self'
29+
Content-Type:
30+
- application/json
31+
Date:
32+
- Tue, 28 Jan 2020 19:06:55 GMT
33+
Referrer-Policy:
34+
- origin-when-cross-origin, strict-origin-when-cross-origin
35+
Vary:
36+
- Origin
37+
Via:
38+
- 1.1 5c66b6bab6d91e5c9630aaf5c6b029af.cloudfront.net (CloudFront)
39+
X-Amz-Cf-Id:
40+
- in66r6RjWhlw2CEw9fuhSVQu6scukbY6z_zLAyzRLa5fw89PfARZww==
41+
X-Amz-Cf-Pop:
42+
- IAH50-C1
43+
X-Cache:
44+
- Miss from cloudfront
45+
X-Content-Type-Options:
46+
- nosniff
47+
X-Frame-Options:
48+
- DENY
49+
X-Permitted-Cross-Domain-Policies:
50+
- all
51+
X-XSS-Protection:
52+
- 1; mode=block
53+
x-amz-apigw-id:
54+
- HBnsVEYaIAMFn6w=
55+
x-amzn-Remapped-Content-Length:
56+
- '4891'
57+
x-amzn-Remapped-Date:
58+
- Tue, 28 Jan 2020 19:06:55 GMT
59+
x-amzn-RequestId:
60+
- d76f2227-4932-45b7-b0fd-2a3f99cd249e
61+
status:
62+
code: 200
63+
message: OK
64+
- request:
65+
body: '{"data": {"systemRequest": {"user": "USER", "password": "PASS"}}}'
66+
headers:
67+
Accept:
68+
- '*/*'
69+
Accept-Encoding:
70+
- gzip, deflate
71+
Connection:
72+
- keep-alive
73+
Content-Length:
74+
- '91'
75+
Content-Type:
76+
- application/json
77+
User-Agent:
78+
- python-requests/2.22.0
79+
method: POST
80+
uri: https://api.ivoy.dev/api/login/loginClient/json/web
81+
response:
82+
body:
83+
string: '{"code": 0, "token": {"access_token": "123"}}'
84+
headers:
85+
Connection:
86+
- keep-alive
87+
Content-Length:
88+
- '4890'
89+
Content-Security-Policy:
90+
- default-src 'self'
91+
Content-Type:
92+
- application/json
93+
Date:
94+
- Tue, 28 Jan 2020 19:06:55 GMT
95+
Referrer-Policy:
96+
- origin-when-cross-origin, strict-origin-when-cross-origin
97+
Vary:
98+
- Origin
99+
Via:
100+
- 1.1 5c66b6bab6d91e5c9630aaf5c6b029af.cloudfront.net (CloudFront)
101+
X-Amz-Cf-Id:
102+
- 2__DW2Ch-ZlEpfOWWaNNBg2ShJWZaWSwtIwfAe2miPhnKuan4DYwnw==
103+
X-Amz-Cf-Pop:
104+
- IAH50-C1
105+
X-Cache:
106+
- Miss from cloudfront
107+
X-Content-Type-Options:
108+
- nosniff
109+
X-Frame-Options:
110+
- DENY
111+
X-Permitted-Cross-Domain-Policies:
112+
- all
113+
X-XSS-Protection:
114+
- 1; mode=block
115+
x-amz-apigw-id:
116+
- HBnsbHgboAMFQ4g=
117+
x-amzn-Remapped-Content-Length:
118+
- '4890'
119+
x-amzn-Remapped-Date:
120+
- Tue, 28 Jan 2020 19:06:55 GMT
121+
x-amzn-RequestId:
122+
- 51b84790-5383-4b33-a7e4-12d4d0498281
123+
status:
124+
code: 200
125+
message: OK
126+
- request:
127+
body: '{"data": {"bOrder": {"idOrder": "wrong"}}}'
128+
headers:
129+
Accept:
130+
- '*/*'
131+
Accept-Encoding:
132+
- gzip, deflate
133+
Connection:
134+
- keep-alive
135+
Content-Length:
136+
- '788'
137+
Content-type:
138+
- application/json
139+
User-Agent:
140+
- python-requests/2.22.0
141+
method: PUT
142+
uri: https://api.ivoy.dev/api/order/newOrder/json/web
143+
response:
144+
body:
145+
string: '{"code":-208,"message":"La dirección frecuente ya existe."}'
146+
headers:
147+
Connection:
148+
- keep-alive
149+
Content-Length:
150+
- '3859'
151+
Content-Security-Policy:
152+
- default-src 'self'
153+
Content-Type:
154+
- application/json
155+
Date:
156+
- Tue, 28 Jan 2020 19:06:57 GMT
157+
Referrer-Policy:
158+
- origin-when-cross-origin, strict-origin-when-cross-origin
159+
Vary:
160+
- Origin
161+
Via:
162+
- 1.1 5c66b6bab6d91e5c9630aaf5c6b029af.cloudfront.net (CloudFront)
163+
X-Amz-Cf-Id:
164+
- gcJOKalXOpeEZpiZNUHbvagl98iUuiVWk5EZrGtSSjO5_LcrRbhMJQ==
165+
X-Amz-Cf-Pop:
166+
- IAH50-C1
167+
X-Cache:
168+
- Miss from cloudfront
169+
X-Content-Type-Options:
170+
- nosniff
171+
X-Frame-Options:
172+
- DENY
173+
X-Permitted-Cross-Domain-Policies:
174+
- all
175+
X-XSS-Protection:
176+
- 1; mode=block
177+
x-amz-apigw-id:
178+
- HBnsgEGIoAMFzHw=
179+
x-amzn-Remapped-Content-Length:
180+
- '3859'
181+
x-amzn-Remapped-Date:
182+
- Tue, 28 Jan 2020 19:06:57 GMT
183+
x-amzn-RequestId:
184+
- 0adc601e-5536-4005-9d1c-6a1c231fa79b
185+
status:
186+
code: 200
187+
message: OK
188+
version: 1

0 commit comments

Comments
 (0)