|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 |
|
3 | | -from datetime import datetime |
| 3 | +# from datetime import datetime |
4 | 4 | from .errors import (ArgumentError, AuthenticationError, # noqa |
5 | 5 | BadGatewayError, BadRequestError, HttpError, IntercomError, |
6 | 6 | MultipleMatchingUsersError, RateLimitExceeded, ResourceNotFound, |
7 | 7 | 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 |
22 | 8 |
|
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' |
30 | 10 |
|
31 | 11 |
|
32 | 12 | RELATED_DOCS_TEXT = "See https://github.com/jkeyes/python-intercom \ |
|
38 | 18 | Intercom.app_api_key and don't set Intercom.api_key." |
39 | 19 | CONFIGURATION_REQUIRED_TEXT = "You must set both Intercom.app_id and \ |
40 | 20 | 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) |
0 commit comments