Skip to content

Commit 25b6b3f

Browse files
committed
Moving to a client model like intercom-ruby.
1 parent 467bb83 commit 25b6b3f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+686
-602
lines changed

intercom/__init__.py

Lines changed: 2 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,12 @@
11
# -*- coding: utf-8 -*-
22

3-
from datetime import datetime
3+
# from datetime import datetime
44
from .errors import (ArgumentError, AuthenticationError, # noqa
55
BadGatewayError, BadRequestError, HttpError, IntercomError,
66
MultipleMatchingUsersError, RateLimitExceeded, ResourceNotFound,
77
ServerError, ServiceUnavailableError, UnexpectedError)
8-
from .lib.setter_property import SetterProperty
9-
from .request import Request
10-
from .admin import Admin # noqa
11-
from .company import Company # noqa
12-
from .count import Count # noqa
13-
from .conversation import Conversation # noqa
14-
from .event import Event # noqa
15-
from .message import Message # noqa
16-
from .note import Note # noqa
17-
from .notification import Notification # noqa
18-
from .user import User # noqa
19-
from .segment import Segment # noqa
20-
from .subscription import Subscription # noqa
21-
from .tag import Tag # noqa
228

23-
import copy
24-
import random
25-
import re
26-
import six
27-
import time
28-
29-
__version__ = '2.1.1'
9+
__version__ = '3.0-dev'
3010

3111

3212
RELATED_DOCS_TEXT = "See https://github.com/jkeyes/python-intercom \
@@ -38,142 +18,3 @@
3818
Intercom.app_api_key and don't set Intercom.api_key."
3919
CONFIGURATION_REQUIRED_TEXT = "You must set both Intercom.app_id and \
4020
Intercom.app_api_key to use this client."
41-
42-
43-
class IntercomType(type): # noqa
44-
45-
app_id = None
46-
app_api_key = None
47-
_hostname = "api.intercom.io"
48-
_protocol = "https"
49-
_endpoints = None
50-
_current_endpoint = None
51-
_target_base_url = None
52-
_endpoint_randomized_at = 0
53-
_rate_limit_details = {}
54-
55-
@property
56-
def _auth(self):
57-
return (self.app_id, self.app_api_key)
58-
59-
@property
60-
def _random_endpoint(self):
61-
if self.endpoints:
62-
endpoints = copy.copy(self.endpoints)
63-
random.shuffle(endpoints)
64-
return endpoints[0]
65-
66-
@property
67-
def _alternative_random_endpoint(self):
68-
endpoints = copy.copy(self.endpoints)
69-
if self.current_endpoint in endpoints:
70-
endpoints.remove(self.current_endpoint)
71-
random.shuffle(endpoints)
72-
if endpoints:
73-
return endpoints[0]
74-
75-
@property
76-
def target_base_url(self):
77-
if None in [self.app_id, self.app_api_key]:
78-
raise ArgumentError('%s %s' % (
79-
CONFIGURATION_REQUIRED_TEXT, RELATED_DOCS_TEXT))
80-
if self._target_base_url is None:
81-
basic_auth_part = '%s:%s@' % (self.app_id, self.app_api_key)
82-
if self.current_endpoint:
83-
self._target_base_url = re.sub(
84-
r'(https?:\/\/)(.*)',
85-
'\g<1>%s\g<2>' % (basic_auth_part),
86-
self.current_endpoint)
87-
return self._target_base_url
88-
89-
@property
90-
def hostname(self):
91-
return self._hostname
92-
93-
@hostname.setter
94-
def hostname(self, value):
95-
self._hostname = value
96-
self.current_endpoint = None
97-
self.endpoints = None
98-
99-
@property
100-
def rate_limit_details(self):
101-
return self._rate_limit_details
102-
103-
@rate_limit_details.setter
104-
def rate_limit_details(self, value):
105-
self._rate_limit_details = value
106-
107-
@property
108-
def protocol(self):
109-
return self._protocol
110-
111-
@protocol.setter
112-
def protocol(self, value):
113-
self._protocol = value
114-
self.current_endpoint = None
115-
self.endpoints = None
116-
117-
@property
118-
def current_endpoint(self):
119-
now = time.mktime(datetime.utcnow().timetuple())
120-
expired = self._endpoint_randomized_at < (now - (60 * 5))
121-
if self._endpoint_randomized_at is None or expired:
122-
self._endpoint_randomized_at = now
123-
self._current_endpoint = self._random_endpoint
124-
return self._current_endpoint
125-
126-
@current_endpoint.setter
127-
def current_endpoint(self, value):
128-
self._current_endpoint = value
129-
self._target_base_url = None
130-
131-
@property
132-
def endpoints(self):
133-
if not self._endpoints:
134-
return ['%s://%s' % (self.protocol, self.hostname)]
135-
else:
136-
return self._endpoints
137-
138-
@endpoints.setter
139-
def endpoints(self, value):
140-
self._endpoints = value
141-
self.current_endpoint = self._random_endpoint
142-
143-
@SetterProperty
144-
def endpoint(self, value):
145-
self.endpoints = [value]
146-
147-
148-
@six.add_metaclass(IntercomType)
149-
class Intercom(object):
150-
_class_register = {}
151-
152-
@classmethod
153-
def get_url(cls, path):
154-
if '://' in path:
155-
url = path
156-
else:
157-
url = cls.current_endpoint + path
158-
return url
159-
160-
@classmethod
161-
def request(cls, method, path, params):
162-
return Request.send_request_to_path(
163-
method, cls.get_url(path), cls._auth, params)
164-
165-
@classmethod
166-
def get(cls, path, **params):
167-
return cls.request('GET', path, params)
168-
169-
@classmethod
170-
def post(cls, path, **params):
171-
return cls.request('POST', path, params)
172-
173-
@classmethod
174-
def put(cls, path, **params):
175-
return cls.request('PUT', path, params)
176-
177-
@classmethod
178-
def delete(cls, path, **params):
179-
return cls.request('DELETE', path, params)

intercom/admin.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
# -*- coding: utf-8 -*-
22

3-
from intercom.api_operations.all import All
4-
from intercom.api_operations.find import Find
53
from intercom.traits.api_resource import Resource
64

75

8-
class Admin(Resource, Find, All):
6+
class Admin(Resource):
97
pass

intercom/api_operations/all.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
class All(object):
88

9-
@classmethod
10-
def all(cls):
11-
collection = utils.resource_class_to_collection_name(cls)
9+
def all(self):
10+
collection = utils.resource_class_to_collection_name(
11+
self.collection_class)
1212
finder_url = "/%s" % (collection)
13-
return CollectionProxy(cls, collection, finder_url)
13+
return CollectionProxy(
14+
self.client, self.collection_class, collection, finder_url)

intercom/api_operations/delete.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
class Delete(object):
77

8-
def delete(self):
9-
from intercom import Intercom
10-
collection = utils.resource_class_to_collection_name(self.__class__)
11-
Intercom.delete("/%s/%s/" % (collection, self.id))
12-
return self
8+
def delete(self, obj):
9+
collection = utils.resource_class_to_collection_name(
10+
self.collection_class)
11+
self.client.delete("/%s/%s/" % (collection, obj.id))
12+
return obj

intercom/api_operations/find.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66

77
class Find(object):
88

9-
@classmethod
10-
def find(cls, **params):
11-
from intercom import Intercom
12-
collection = utils.resource_class_to_collection_name(cls)
9+
def find(self, **params):
10+
collection = utils.resource_class_to_collection_name(
11+
self.collection_class)
1312
if 'id' in params:
14-
response = Intercom.get("/%s/%s" % (collection, params['id']))
13+
response = self.client.get(
14+
"/%s/%s" % (collection, params['id']), {})
1515
else:
16-
response = Intercom.get("/%s" % (collection), **params)
16+
response = self.client.get("/%s" % (collection), params)
1717

1818
if response is None:
1919
raise HttpError('Http Error - No response entity returned')
2020

21-
return cls(**response)
21+
return self.collection_class(**response)

intercom/api_operations/find_all.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66

77
class FindAll(object):
88

9-
@classmethod
10-
def find_all(cls, **params):
11-
collection = utils.resource_class_to_collection_name(cls)
9+
def find_all(self, **params):
10+
collection = utils.resource_class_to_collection_name(
11+
self.collection_class)
1212
if 'id' in params and 'type' not in params:
1313
finder_url = "/%s/%s" % (collection, params['id'])
1414
else:
1515
finder_url = "/%s" % (collection)
1616
finder_params = params
17-
return CollectionProxy(cls, collection, finder_url, finder_params)
17+
return CollectionProxy(
18+
self.client, self.collection_class, collection,
19+
finder_url, finder_params)

intercom/api_operations/load.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,19 @@
66

77
class Load(object):
88

9-
def load(self):
10-
from intercom import Intercom
11-
cls = self.__class__
12-
collection = utils.resource_class_to_collection_name(cls)
13-
if hasattr(self, 'id'):
14-
response = Intercom.get("/%s/%s" % (collection, self.id))
9+
def load(self, resource):
10+
collection = utils.resource_class_to_collection_name(
11+
self.collection_class)
12+
print "RESOURCE", resource, hasattr(resource, 'id')
13+
if hasattr(resource, 'id'):
14+
response = self.client.get("/%s/%s" % (collection, resource.id), {}) # noqa
15+
print "RESPONSE", response
1516
else:
1617
raise Exception(
17-
"Cannot load %s as it does not have a valid id." % (cls))
18+
"Cannot load %s as it does not have a valid id." % (
19+
self.collection_class))
1820

1921
if response is None:
2022
raise HttpError('Http Error - No response entity returned')
2123

22-
return cls(**response)
24+
return resource.from_response(response)

intercom/api_operations/save.py

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,49 @@
55

66
class Save(object):
77

8-
@classmethod
9-
def create(cls, **params):
10-
from intercom import Intercom
11-
collection = utils.resource_class_to_collection_name(cls)
12-
response = Intercom.post("/%s/" % (collection), **params)
8+
def create(self, **params):
9+
collection = utils.resource_class_to_collection_name(
10+
self.collection_class)
11+
response = self.client.post("/%s/" % (collection), params)
1312
if response: # may be empty if we received a 202
14-
return cls(**response)
13+
return self.collection_class(**response)
1514

16-
def from_dict(self, pdict):
17-
for key, value in list(pdict.items()):
18-
setattr(self, key, value)
15+
# def from_dict(self, pdict):
16+
# for key, value in list(pdict.items()):
17+
# setattr(self, key, value)
1918

20-
@property
21-
def to_dict(self):
22-
a_dict = {}
23-
for name in list(self.__dict__.keys()):
24-
if name == "changed_attributes":
25-
continue
26-
a_dict[name] = self.__dict__[name] # direct access
27-
return a_dict
19+
# @property
20+
# def to_dict(self):
21+
# a_dict = {}
22+
# for name in list(self.__dict__.keys()):
23+
# if name == "changed_attributes":
24+
# continue
25+
# a_dict[name] = self.__dict__[name] # direct access
26+
# return a_dict
2827

29-
@classmethod
30-
def from_api(cls, response):
31-
obj = cls()
32-
obj.from_response(response)
33-
return obj
28+
# @classmethod
29+
# def from_api(cls, response):
30+
# obj = cls()
31+
# obj.from_response(response)
32+
# return obj
3433

35-
def from_response(self, response):
36-
self.from_dict(response)
37-
return self
34+
# def from_response(self, response):
35+
# self.from_dict(response)
36+
# return self
3837

39-
def save(self):
40-
from intercom import Intercom
41-
collection = utils.resource_class_to_collection_name(self.__class__)
42-
params = self.attributes
43-
if self.id_present and not self.posted_updates:
38+
def save(self, obj):
39+
collection = utils.resource_class_to_collection_name(
40+
self.collection_class)
41+
params = obj.attributes
42+
if hasattr(obj, 'id_present') and not hasattr(obj, 'posted_updates'):
4443
# update
45-
response = Intercom.put('/%s/%s' % (collection, self.id), **params)
44+
response = self.client.put('/%s/%s' % (collection, obj.id), params)
4645
else:
4746
# create
48-
params.update(self.identity_hash)
49-
response = Intercom.post('/%s' % (collection), **params)
47+
params.update(self.identity_hash(obj))
48+
response = self.client.post('/%s' % (collection), params)
5049
if response:
51-
return self.from_response(response)
50+
return obj.from_response(response)
5251

5352
@property
5453
def id_present(self):
@@ -58,12 +57,11 @@ def id_present(self):
5857
def posted_updates(self):
5958
return getattr(self, 'update_verb', None) == 'post'
6059

61-
@property
62-
def identity_hash(self):
63-
identity_vars = getattr(self, 'identity_vars', [])
60+
def identity_hash(self, obj):
61+
identity_vars = getattr(obj, 'identity_vars', [])
6462
parts = {}
6563
for var in identity_vars:
66-
id_var = getattr(self, var, None)
64+
id_var = getattr(obj, var, None)
6765
if id_var: # only present id var if it is not blank or None
6866
parts[var] = id_var
6967
return parts

0 commit comments

Comments
 (0)