-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy path__init__.py
More file actions
144 lines (119 loc) · 4.62 KB
/
__init__.py
File metadata and controls
144 lines (119 loc) · 4.62 KB
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
# -*- coding: utf-8 -*-
# Copyright 2017-TODAY LasLabs Inc.
# License MIT (https://opensource.org/licenses/MIT).
import properties
class BaseModel(properties.HasProperties):
"""This is the core interface to be inherited by all models."""
# The authenticated Five9 interface that should be used for the
# underlying data operations. This is set using the
five9 = None
@staticmethod
def api(method):
"""Use this decorator to wrap methods that interact with the API.
These methods should always be called with an authenticated API
interface for their first parameter. This interface will be set as
the ``five9`` object, with the rest of the arguments being passed to
the actual method.
"""
def decorated_method(self, api, *args, **kwargs):
self.five9 = api
return method(*args, **kwargs)
return decorated_method
@classmethod
def parse_response(cls, fields, records):
"""Parse an API response into usable objects.
Args:
fields (list[str]): List of strings indicating the fields that
are represented in the records, in the order presented in
the records.::
[
'number1',
'number2',
'number3',
'first_name',
'last_name',
'company',
'street',
'city',
'state',
'zip',
]
records (list[dict]): A really crappy data structure representing
records as returned by Five9::
[
{
'values': {
'data': [
'8881234567',
None,
None,
'Dave',
'Lasley',
'LasLabs Inc',
None,
'Las Vegas',
'NV',
'89123',
]
}
}
]
Returns:
list[BaseModel]: List of parsed records.
"""
data = [i['values']['data'] for i in records]
return [
cls(**{fields[idx]: row for idx, row in enumerate(d)})
for d in data
]
def to_api(self):
"""Return all of the properties and values in a dictionary."""
return {
attr: getattr(self, attr) for attr in self._props.keys(),
}
def __getattr__(self, item):
"""Allow for dynamic attributes, because the fields can be changed.
Items beginning with ``_`` are excluded from this logic.
"""
if item.startswith('_'):
return super(BaseModel, self).__getattr__(item)
try:
return super(BaseModel, self).__getattr__(item)
except AttributeError:
private_attr = '_%s' % item
try:
return super(BaseModel, self).__getattr__(private_attr)
except AttributeError:
new_attr = properties.basic.DynamicProperty(
'This is a dynamically created attribute.',
)
setattr(self, private_attr, new_attr)
self._props[item] = new_attr
return super(BaseModel, self).__getattr__(private_attr)
# CRUD Interface
def create(self):
"""Create the object on the API. Children should implement this."""
raise NotImplementedError
def delete(self):
"""Delete the object from the API. Chilren should implement this."""
raise NotImplementedError
@classmethod
def get(cls, identifier):
"""Get the object from the API. Children should implement this.
Args:
identifier (mixed): The identifier to send as the search key.
Returns:
list[BaseModel]: Recordset matching identifier.
"""
raise NotImplementedError
@classmethod
def search(cls, query):
"""Search for the query."""
raise NotImplementedError
def update(self):
"""Update the object on the API. Children should implement this."""
raise NotImplementedError
def upsert(self):
"""Create or Update the object on the API. Children should implement.
"""
raise NotImplementedError