forked from npinger/base-crm-api-client
-
Notifications
You must be signed in to change notification settings - Fork 2
/
prototype.py
349 lines (295 loc) · 12.1 KB
/
prototype.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/usr/bin/env python
"""Provides interfaces and prototypes for BaseAPI components"""
import logging
logger = logging.getLogger(__name__)
import abc
from copy import deepcopy
from datetime import datetime
import dateutil
__author__ = 'Clayton Daley III'
__copyright__ = "Copyright 2015, Clayton Daley III"
__license__ = "Apache License 2.0"
__version__ = "2.0.0"
__maintainer__ = "Clayton Daley III"
__status__ = "Development"
def _key_coded_dict(d):
new_dict = dict()
for k, v in d.iteritems():
if isinstance(d, dict):
for k2, v2 in v.iteritems():
new_dict['%s[%s]' % k, k2] = v2
else:
new_dict[k] = v
return new_dict
class AuthenticationError(Exception):
pass
class IBaseCrmAuthentication(object):
@abc.abstractmethod
def headers(self):
"""
Generate a string for
"""
pass
@abc.abstractmethod
def refresh(self):
pass
class BaseCrmAuthentication(IBaseCrmAuthentication):
def __init__(self):
self._access_token = None
class Entity(object):
"""
Makes it easy to check if an object is a BaseCRM Entity
"""
def URL(self, debug=False):
if debug:
url = 'https://api.sandbox.getbase.com'
else:
url = 'https://api.getbase.com'
if isinstance(self, Resource) and self.id is not None:
url += "/v%d/%s/%s" % (self.API_VERSION, self._PATH, self.id)
else:
url += "/v%d/%s" % (self.API_VERSION, self._PATH)
return url
class Resource(Entity):
"""
Entity is the base class for standard BaseCRM API Client entities.
"""
API_VERSION = 2
"""
This is the top-level key for arrays sent to and received from the API. This must be done because APIv1 responses
use a key that's specific to the response type. For example, an APIv1 contact response is:
{
'contact': {
'name': ...
...
}
}
Even though APIv2 standardizes on 'data', an APIv2 Entity must process a response like:
{
'data': {
'name': ...
...
}
}
"""
DATA_PARENT_KEY = 'data'
# For import reasons, classes need to setup a local table
RESOURCE_TYPES = {}
def __init__(self, entity_id=None):
if entity_id is not None and not isinstance(entity_id, int):
raise TypeError("entity_id must be None or int")
super(Resource, self).__init__()
self._data = dict()
self._dirty = dict()
self._loaded = False
# This way, entity.id will never tell the user to call get()
self._data['id'] = entity_id
self.__initialized = True
def __setattr__(self, key, value):
"""Enforce business rules on attributes and store them in a special location"""
if not '_Resource__initialized' in self.__dict__:
# If the __initialized flag isn't set yet, set the attribute normally
object.__setattr__(self, key, value)
return self
if key in self._dirty:
# Must nest because we don't want to check _data if a non-matching value is in _dirty
if value is self._dirty['key']:
# Compare using 'is' to ensure mutability is preserved, in which case we don't need to update
return self
elif key in self._data and value is self._data['key']:
# Compare using 'is' to ensure mutability is preserved, in which case we don't need to update
return self
if key in self.PROPERTIES:
# PROPERTIES contains a dict -- a list of key-value pairs describing an attributes (PROPERTIES key) and the
# business rules that apply to its value (PROPERTIES value)
if isinstance(self.PROPERTIES[key], dict):
rules = self.PROPERTIES[key]
# the 'type' key (optionally a list) contains all accepted types
if 'type' in rules:
# Get acceptable types and make sure they're in a list
type_ = rules['type']
if not isinstance(type_, list):
type_ = [type_]
match = False
for t in type_:
if isinstance(value, t):
match = True
break
if not match:
raise TypeError("%s is not a valid type for %s.%s" % (value, self.__class__.__name__, key))
# the 'in' key is a list of all acceptable values
if 'in' in rules and value not in rules['in']:
raise ValueError("%s is not a valid value for %s.%s" % (value, self.__class__.__name__, key))
# Otherwise, the PROPERTIES value is just the accepted type
elif not isinstance(value, self.PROPERTIES[key]):
raise TypeError("%s.%s must be of type %s" % (self.__class__.__name__, key, self.PROPERTIES[key].__name__))
self._dirty[key] = value
return self
elif '_%s' % key in self.PROPERTIES:
raise KeyError("%s is readonly for %s" % (key, self.__class__.__name__))
else:
raise AttributeError("%s is not a valid attribute for %s" % (key, self.__class__.__name__))
def __getattr__(self, key):
# If the value has been set locally, it will be found in dirty and may be returned
if key == '__deepcopy__':
return object.__deepcopy__
if key in self._dirty:
return self._dirty[key]
# 'id' must always return, either from data (where it is usually set) or None (thanks to PROPERTIES checks)
if key != 'id' and not self._loaded:
raise ReferenceError("Object has not been loaded. Use get() to populate data before requesting %s." % key)
if key in self._data:
return self._data[key]
# Acknowledge property is valid by returning None
if key in self.PROPERTIES:
return None
# Acknowledge readonly property is valid by returning None
if "_%s" % key in self.PROPERTIES:
return None
raise AttributeError("%s not a valid attribute of %s" % (key, self.__class__.__name__))
def set_data(self, data):
"""
Sets the local object to the values indicated in the 'data' array. This function uses the helper
format_data_set() to allow objects to, for example, convert elements of the response into more usable types.
For more information on this process, see format_data_set().
"""
# Assign actual data to self
self.__dict__['_data'] = self.format_data_set(data)
# Mark data as loaded
self.__dict__['loaded'] = True
return self # returned for setting and chaining convenience
def format_data_set(self, data):
"""
Objects should overload this function to adjust the input, including converting elements into custom types.
For example,
- The v2 Contact object wraps the address up into an Address object
- In v1, tags are sent as comma-separated lists that should be exploded into real lists
"""
for key, value in data.iteritems():
if isinstance(value, dict):
pass
elif key not in self.PROPERTIES:
# Assume this is a composite key to be used by another process like `resource`
pass
elif key == 'resource':
"""
Input is:
...
'resource_type': {
'type': basestring,
'in': RESOURCE_TYPES
},
'resource_id': int,
...
"""
class_ = self.RESOURCE_TYPES[data['resource']]
data['resource'] = class_(data['resource_id'])
elif issubclass(self.PROPERTIES[key], Resource):
instance = value()
instance.set_data(data[key])
data[key] = instance
elif issubclass(self.PROPERTIES[key], datetime.datetime):
data[key] = dateutil.parser.parse(data[key])
# This could be adjusted to delete a dynamic list of keys if the resource_id logic was ever proved unreliable
if 'resource_id' in data:
del data['resource_id']
return data # returned for setting and chaining convenience
def get_data(self):
data = self.format_data_get(deepcopy(self._dirty))
# If needed, ID is encoded in URL
return {self.DATA_PARENT_KEY: data}
def format_data_get(self, dirty):
data = dict()
for key, value in dirty.iteritems():
if key == 'resource':
data['resource_id'] = dirty['resource'].id
data['resource'] = dirty['resource'].__class__.__name__.lower()
elif isinstance(value, Resource):
data[key] = value.get_data()
elif issubclass(value, datetime):
data[key] = value.isoformat()
else:
data[key] = dirty[key]
return data
class ResourceV1(Resource):
API_VERSION = 1
# Needs a different URL builder
def URL(self, debug=False):
if debug:
raise ValueError("BaseCRM's v1 API does not support debug mode.")
url = 'https://app.futuresimple.com/apis/%s/api/v%d/%s' % (self.RESOURCE, self.API_VERSION, self._PATH)
if self.id is not None:
url += '/%d' % self.id
return url + '.json'
def get_data(self):
dirty = self.format_data_get(deepcopy(self._dirty))
data = {
self.DATA_PARENT_KEY: dirty
# e.g. 'contact': {'name': ...}
}
# to generate ?contact[name]=...
return _key_coded_dict(data)
class Collection(Entity):
"""
Entity is the base class for standard BaseCRM API Client entities.
"""
API_VERSION = 2
@property
def _PATH(self):
return self._ITEM._PATH
def __init__(self, **kwargs):
self.__dict__['filters'] = dict()
for key, value in kwargs:
self.key = value
def __setattr__(self, key, value):
# If the __initialized flag isn't set yet, set the attribute normally
if key in self.FILTERS:
# Arrays list acceptable values
if isinstance(self.FILTERS[key], list) and value in self.FILTERS[key]:
raise ValueError("%s is not a valid filter value for #s" % value, key)
if not isinstance(value, self.PROPERTIES[key]):
raise TypeError("%s must be of type %s" % (key, self.FILTERS[key].__name__))
self.filters[key] = value
else:
raise AttributeError("%s is not a valid filter for %s" % (key, self.__class__.__name__))
def get_data(self):
data = deepcopy(self.filters)
# If needed, ID is encoded in URL
return data
def format_page(self, data):
# Return a page containing API data processed into Resources and Collections
page = list()
for record in data:
entity = self._ITEM()
entity.set_data(record)
page.append(entity)
return page
class CollectionV1(Collection):
"""
Tweaks various functions for old v1 resource structure
"""
def URL(self, debug=False):
if debug:
raise ValueError("BaseCRM's v1 API does not support debug mode.")
url = 'https://app.futuresimple.com/apis/%s/api/v%d/%s' % (self.RESOURCE, self.API_VERSION, self._PATH)
return url + '/search.json'
def format_data_set(self):
data = {
self.RESPONSE_KEY: deepcopy(self.filters)
# e.g. 'contact': {'name': ...}
}
# to generate ?contact[name]=...
return _key_coded_dict(data)
class SyncService(object):
def __init__(self):
self.auth = None
self.device_id = None
def headers(self):
headers = self.auth.headers()
headers['X-Basecrm-Device-UUID'] = self.device_id
@abc.abstractmethod
def acks(self):
pass
@abc.abstractmethod
def types(self):
pass