Skip to content

Commit eabf204

Browse files
committed
Adding HTTP error support.
1 parent 0ee783c commit eabf204

File tree

4 files changed

+112
-19
lines changed

4 files changed

+112
-19
lines changed

intercom/__init__.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
# -*- coding: utf-8 -*-
22

33
from datetime import datetime
4-
from .errors import ArgumentError
5-
from .errors import HttpError # noqa
4+
from .errors import (ArgumentError, HttpError, IntercomError, # noqa
5+
ResourceNotFound, AuthenticationError, ServerError, BadGatewayError,
6+
ServiceUnavailableError) # noqa
67
from .lib.setter_property import SetterProperty
78
from .request import Request
8-
from .admin import Admin
9-
from .company import Company
10-
from .conversation import Conversation
11-
from .event import Event
12-
from .message import Message
13-
from .note import Note
14-
from .notification import Notification
15-
from .user import User
16-
from .segment import Segment
17-
from .subscription import Subscription
18-
from .tag import Tag
9+
from .admin import Admin # noqa
10+
from .company import Company # noqa
11+
from .conversation import Conversation # noqa
12+
from .event import Event # noqa
13+
from .message import Message # noqa
14+
from .note import Note # noqa
15+
from .notification import Notification # noqa
16+
from .user import User # noqa
17+
from .segment import Segment # noqa
18+
from .subscription import Subscription # noqa
19+
from .tag import Tag # noqa
1920

2021
import copy
2122
import random
@@ -24,11 +25,6 @@
2425

2526
__version__ = '2.0.alpha'
2627

27-
__all__ = (
28-
Admin, Company, Conversation, Event, Message, Note, Notification,
29-
Segment, Subscription, Tag, User
30-
)
31-
3228

3329
RELATED_DOCS_TEXT = "See https://github.com/jkeyes/python-intercom \
3430
for usage examples."

intercom/errors.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,27 @@ class ArgumentError(ValueError):
77

88
class HttpError(Exception):
99
pass
10+
11+
12+
class IntercomError(Exception):
13+
pass
14+
15+
16+
class ResourceNotFound(IntercomError):
17+
pass
18+
19+
20+
class AuthenticationError(IntercomError):
21+
pass
22+
23+
24+
class ServerError(IntercomError):
25+
pass
26+
27+
28+
class BadGatewayError(IntercomError):
29+
pass
30+
31+
32+
class ServiceUnavailableError(IntercomError):
33+
pass

intercom/request.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
from .errors import HttpError # noqa
3+
from . import errors
44

55
import json
66
import requests
@@ -32,9 +32,26 @@ def send_request_to_path(cls, method, url, auth, params=None):
3232
method, url, timeout=cls.timeout,
3333
auth=auth, **req_params)
3434

35+
cls.raise_errors_on_failure(resp)
36+
3537
if resp.content:
3638
return json.loads(resp.content)
3739

40+
@classmethod
41+
def raise_errors_on_failure(cls, resp):
42+
if resp.status_code == 404:
43+
raise errors.ResourceNotFound('Resource Not Found')
44+
elif resp.status_code == 401:
45+
raise errors.AuthenticationError('Unauthorized')
46+
elif resp.status_code == 403:
47+
raise errors.AuthenticationError('Forbidden')
48+
elif resp.status_code == 500:
49+
raise errors.ServerError('Server Error')
50+
elif resp.status_code == 502:
51+
raise errors.BadGatewayError('Bad Gateway Error')
52+
elif resp.status_code == 503:
53+
raise errors.ServiceUnavailableError('Service Unavailable')
54+
3855

3956
class ResourceEncoder(json.JSONEncoder):
4057
def default(self, o):

tests/unit/request_spec.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import httpretty
4+
import intercom
5+
import re
6+
from describe import expect
7+
from intercom import Intercom
8+
9+
get = httpretty.GET
10+
post = httpretty.POST
11+
r = re.compile
12+
13+
14+
class DescribeRequest:
15+
16+
@httpretty.activate
17+
def it_raises_resource_not_found(self):
18+
httpretty.register_uri(
19+
get, r(r'/notes$'), body='', status=404)
20+
with expect.to_raise_error(intercom.ResourceNotFound):
21+
Intercom.get('/notes')
22+
23+
@httpretty.activate
24+
def it_raises_authentication_error_unauthorized(self):
25+
httpretty.register_uri(
26+
get, r(r'/notes$'), body='', status=401)
27+
with expect.to_raise_error(intercom.AuthenticationError):
28+
Intercom.get('/notes')
29+
30+
@httpretty.activate
31+
def it_raises_authentication_error_forbidden(self):
32+
httpretty.register_uri(
33+
get, r(r'/notes$'), body='', status=403)
34+
with expect.to_raise_error(intercom.AuthenticationError):
35+
Intercom.get('/notes')
36+
37+
@httpretty.activate
38+
def it_raises_server_error(self):
39+
httpretty.register_uri(
40+
get, r(r'/notes$'), body='', status=500)
41+
with expect.to_raise_error(intercom.ServerError):
42+
Intercom.get('/notes')
43+
44+
@httpretty.activate
45+
def it_raises_bad_gateway_error(self):
46+
httpretty.register_uri(
47+
get, r(r'/notes$'), body='', status=502)
48+
with expect.to_raise_error(intercom.BadGatewayError):
49+
Intercom.get('/notes')
50+
51+
@httpretty.activate
52+
def it_raises_service_unavailable_error(self):
53+
httpretty.register_uri(
54+
get, r(r'/notes$'), body='', status=503)
55+
with expect.to_raise_error(intercom.ServiceUnavailableError):
56+
Intercom.get('/notes')

0 commit comments

Comments
 (0)