|
5 | 5 | # License: http://jkeyes.mit-license.org/ |
6 | 6 | # |
7 | 7 |
|
8 | | -import datetime |
9 | | -import time |
10 | 8 | from intercom.api_operations.all import All |
11 | 9 | from intercom.api_operations.count import Count |
12 | 10 | from intercom.api_operations.delete import Delete |
13 | 11 | from intercom.api_operations.find import Find |
14 | 12 | from intercom.api_operations.find_all import FindAll |
15 | 13 | from intercom.api_operations.save import Save |
16 | | -from intercom.lib.flat_store import FlatStore |
17 | | -from intercom.lib.typed_json_deserializer import JsonDeserializer |
| 14 | +from intercom.traits.api_resource import Resource |
18 | 15 | from intercom.traits.incrementable_attributes import IncrementableAttributes |
19 | 16 |
|
20 | 17 |
|
21 | | -def timestamp_field(attribute): |
22 | | - return attribute.endswith('_at') |
23 | | - |
24 | | - |
25 | | -def type_field(attribute): |
26 | | - return attribute == "type" |
27 | | - |
28 | | - |
29 | | -def custom_attribute_field(attribute): |
30 | | - return attribute == 'custom_attributes' |
31 | | - |
32 | | - |
33 | | -def typed_value(value): |
34 | | - return hasattr(value, 'keys') and 'type' in value |
35 | | - |
36 | | - |
37 | | -def datetime_value(value): |
38 | | - return hasattr(value, "timetuple") |
39 | | - |
40 | | - |
41 | | -def to_datetime_value(value): |
42 | | - if value: |
43 | | - return datetime.datetime.fromtimestamp(int(value)) |
44 | | - |
45 | | - |
46 | | -class Resource(object): |
47 | | - |
48 | | - def __init__(self, **params): |
49 | | - self.from_dict(params) |
50 | | - |
51 | | - if hasattr(self, 'flat_store_attributes'): |
52 | | - for attr in self.flat_store_attributes: |
53 | | - if not hasattr(self, attr): |
54 | | - setattr(self, attr, FlatStore()) |
55 | | - |
56 | | - def _flat_store_attribute(self, attribute): |
57 | | - if hasattr(self, 'flat_store_attributes'): |
58 | | - return attribute in self.flat_store_attributes |
59 | | - return False |
60 | | - |
61 | | - |
62 | | - @classmethod |
63 | | - def from_api(cls, response): |
64 | | - obj = cls() |
65 | | - obj.from_response(response) |
66 | | - return obj |
67 | | - |
68 | | - def from_response(self, response): |
69 | | - self.from_dict(response) |
70 | | - |
71 | | - def from_dict(self, dict): |
72 | | - for attribute, value in dict.items(): |
73 | | - if type_field(attribute): |
74 | | - continue |
75 | | - setattr(self, attribute, value) |
76 | | - |
77 | | - @property |
78 | | - def attributes(self): |
79 | | - return self.__dict__ |
80 | | - |
81 | | - def __getattribute__(self, attribute): |
82 | | - value = super(Resource, self).__getattribute__(attribute) |
83 | | - if timestamp_field(attribute): |
84 | | - return to_datetime_value(value) |
85 | | - else: |
86 | | - return value |
87 | | - |
88 | | - def __setattr__(self, attribute, value): |
89 | | - if typed_value(value) and not custom_attribute_field(attribute): |
90 | | - value_to_set = JsonDeserializer(value).deserialize() |
91 | | - elif self._flat_store_attribute(attribute): |
92 | | - value_to_set = FlatStore(value) |
93 | | - elif timestamp_field(attribute) and datetime_value(value): |
94 | | - value_to_set = time.mktime(value.timetuple()) |
95 | | - else: |
96 | | - value_to_set = value |
97 | | - super(Resource, self).__setattr__(attribute, value_to_set) |
98 | | - |
99 | | - |
100 | 18 | CLASS_REGISTRY = {} |
101 | 19 |
|
102 | 20 | def create_class_instance(class_name): |
|
0 commit comments