Skip to content

Commit 7ffbc34

Browse files
committed
Allowing direct imports of models.
For example, `from intercom import User` instead of `from intercom.user import User`.
1 parent 2442244 commit 7ffbc34

21 files changed

+93
-63
lines changed

intercom/__init__.py

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
11
# -*- coding: utf-8 -*-
22

33
from datetime import datetime
4-
from json import JSONEncoder
54
from .errors import ArgumentError
65
from .errors import HttpError # noqa
76
from .lib.setter_property import SetterProperty
7+
from .request import Request
8+
from .admin import Admin
9+
from .company import Company
10+
from .event import Event
11+
from .message import Message
12+
from .note import Note
13+
from .notification import Notification
14+
from .user import User
15+
from .subscription import Subscription
16+
from .tag import Tag
817

918
import copy
10-
import json
1119
import random
1220
import re
13-
import requests
1421
import time
1522

1623
__version__ = '2.0.alpha'
1724

25+
__all__ = (
26+
Admin, Company, Event, Message, Note, Notification, Subscription, Tag, User
27+
)
28+
1829

1930
RELATED_DOCS_TEXT = "See https://github.com/jkeyes/python-intercom \
2031
for usage examples."
@@ -35,7 +46,6 @@ class _Config(object):
3546
endpoints = None
3647
current_endpoint = None
3748
target_base_url = None
38-
timeout = 10
3949
endpoint_randomized_at = None
4050

4151

@@ -57,6 +67,10 @@ def app_api_key(self):
5767
def app_api_key(self, value):
5868
self._config.app_api_key = value
5969

70+
@property
71+
def _auth(self):
72+
return (self.app_id, self.app_api_key)
73+
6074
@property
6175
def _random_endpoint(self):
6276
if self.endpoints:
@@ -144,54 +158,25 @@ class Intercom(object):
144158
__metaclass__ = IntercomType
145159

146160
@classmethod
147-
def send_request_to_path(cls, method, path, params=None):
148-
""" Construct an API request, send it to the API, and parse the
149-
response. """
150-
req_params = {}
161+
def get_url(cls, path):
151162
if '://' in path:
152163
url = path
153164
else:
154165
url = cls.current_endpoint + path
155-
156-
headers = {
157-
'User-Agent': 'python-intercom/' + __version__,
158-
'Accept': 'application/json'
159-
}
160-
if method in ('POST', 'PUT', 'DELETE'):
161-
headers['content-type'] = 'application/json'
162-
req_params['data'] = json.dumps(params, cls=ResourceEncoder)
163-
elif method == 'GET':
164-
req_params['params'] = params
165-
req_params['headers'] = headers
166-
167-
resp = requests.request(
168-
method, url, timeout=cls._config.timeout,
169-
auth=(Intercom.app_id, Intercom.app_api_key), **req_params)
170-
171-
print resp.status_code, resp.content
172-
if resp.content:
173-
return json.loads(resp.content)
166+
return url
174167

175168
@classmethod
176169
def get(cls, path, **params):
177-
return cls.send_request_to_path('GET', path, params)
170+
return Request.send_request_to_path('GET', cls.get_url(path), cls._auth, params)
178171

179172
@classmethod
180173
def post(cls, path, **params):
181-
return cls.send_request_to_path('POST', path, params)
174+
return Request.send_request_to_path('POST', cls.get_url(path), cls._auth, params)
182175

183176
@classmethod
184177
def put(cls, path, **params):
185-
return cls.send_request_to_path('PUT', path, params)
178+
return Request.send_request_to_path('PUT', cls.get_url(path), cls._auth, params)
186179

187180
@classmethod
188181
def delete(cls, path, **params):
189-
return cls.send_request_to_path('DELETE', path, params)
190-
191-
192-
class ResourceEncoder(JSONEncoder):
193-
def default(self, o):
194-
if hasattr(o, 'attributes'):
195-
# handle API resources
196-
return o.attributes
197-
return super(ResourceEncoder, self).default(o)
182+
return Request.send_request_to_path('DELETE', cls.get_url(path), cls._auth, params)

intercom/api_operations/count.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
from intercom import Intercom
21
from intercom import utils
32

43

54
class Count(object):
65

76
@classmethod
87
def count(cls):
8+
from intercom import Intercom
99
response = Intercom.get("/counts/")
1010
return response[utils.resource_class_to_name(cls)]['count']

intercom/api_operations/delete.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
from intercom import Intercom
21
from intercom import utils
32

43

54
class Delete(object):
65

76
def delete(self):
7+
from intercom import Intercom
88
collection = utils.resource_class_to_collection_name(self.__class__)
99
Intercom.delete("/%s/%s/" % (collection, self.id))
1010
return self

intercom/api_operations/find.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
from intercom import HttpError
2-
from intercom import Intercom
32
from intercom import utils
43

54

65
class Find(object):
76

87
@classmethod
98
def find(cls, **params):
9+
from intercom import Intercom
1010
collection = utils.resource_class_to_collection_name(cls)
1111
if 'id' in params:
1212
response = Intercom.get("/%s/%s" % (collection, params['id']))

intercom/api_operations/load.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from intercom import HttpError
2-
from intercom import Intercom
32
from intercom import utils
43

54

65
class Load(object):
76

87
def load(self):
8+
from intercom import Intercom
99
cls = self.__class__
1010
collection = utils.resource_class_to_collection_name(cls)
1111
if hasattr(self, 'id'):

intercom/api_operations/save.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
from intercom import Intercom
21
from intercom import utils
32

43

54
class Save(object):
65

76
@classmethod
87
def create(cls, **params):
8+
from intercom import Intercom
99
collection = utils.resource_class_to_collection_name(cls)
1010
response = Intercom.post("/%s/" % (collection), **params)
1111
return cls(**response)
@@ -34,6 +34,7 @@ def from_response(self, response):
3434
return self
3535

3636
def save(self):
37+
from intercom import Intercom
3738
collection = utils.resource_class_to_collection_name(self.__class__)
3839
params = self.attributes
3940
if self.id_present and not self.posted_updates:

intercom/collection_proxy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from intercom import HttpError
2-
from intercom import Intercom
32

43

54
class CollectionProxy(object):
@@ -58,6 +57,7 @@ def get_next_page(self):
5857

5958
def get_page(self, url, params={}):
6059
# get a page of results
60+
from intercom import Intercom
6161

6262
# if there is no url stop iterating
6363
if url is None:

intercom/company.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
from intercom.user import Resource
21
from intercom.api_operations.all import All
32
from intercom.api_operations.count import Count
43
from intercom.api_operations.find import Find
54
from intercom.api_operations.load import Load
65
from intercom.api_operations.save import Save
76
from intercom.extended_api_operations.users import Users
7+
from intercom.traits.api_resource import Resource
88

99

1010
class Company(Resource, Count, Find, All, Save, Load, Users):

intercom/extended_api_operations/reply.py

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

3-
from intercom import Intercom
43
from intercom import utils
54

65

76
class Reply(object):
87

98
@property
109
def reply(self, reply_data):
10+
from intercom import Intercom
1111
collection = utils.resource_class_to_collection_name(self.__class__)
1212
url = "/%s/%s/reply" % (collection, self.id)
1313
reply_data['conversation_id'] = self.id

intercom/request.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from .errors import HttpError # noqa
4+
5+
import json
6+
import requests
7+
8+
9+
class Request(object):
10+
11+
timeout = 10
12+
13+
@classmethod
14+
def send_request_to_path(cls, method, url, auth, params=None):
15+
""" Construct an API request, send it to the API, and parse the
16+
response. """
17+
from intercom import __version__
18+
req_params = {}
19+
20+
headers = {
21+
'User-Agent': 'python-intercom/' + __version__,
22+
'Accept': 'application/json'
23+
}
24+
if method in ('POST', 'PUT', 'DELETE'):
25+
headers['content-type'] = 'application/json'
26+
req_params['data'] = json.dumps(params, cls=ResourceEncoder)
27+
elif method == 'GET':
28+
req_params['params'] = params
29+
req_params['headers'] = headers
30+
31+
resp = requests.request(
32+
method, url, timeout=cls.timeout,
33+
auth=auth, **req_params)
34+
35+
if resp.content:
36+
return json.loads(resp.content)
37+
38+
39+
class ResourceEncoder(json.JSONEncoder):
40+
def default(self, o):
41+
if hasattr(o, 'attributes'):
42+
# handle API resources
43+
return o.attributes
44+
return super(ResourceEncoder, self).default(o)

0 commit comments

Comments
 (0)