-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathquery.py
More file actions
213 lines (154 loc) · 6.81 KB
/
query.py
File metadata and controls
213 lines (154 loc) · 6.81 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
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
from datetime import datetime
from ..lib import QueryIterator, DefaultResponse, Command
from ..common import Object
def run(core, path, param):
return create_callback_function(core, path, callback_response=DefaultResponse)(param)
def create_callback_function(core, path, name=None, *, callback_response=None):
"""
Create a query callback function
:param cterasdk.objects.core.Portal core: Portal object
:param cterasdk.core.query.QueryParams param: Query paramter object
:param str,optional name: Schema method name
:param cterasdk.lib.iterator.BaseResponse callback_response: Class to consume callback response
:returns: Command object
"""
def database(core, path, name, param):
return callback_response(core.api.database(path, name, param))
def execute(core, path, name, param):
return callback_response(core.api.execute(path, name, param))
return Command(execute if name else database, core, path, name or 'query')
def iterator(core, path, param=None, name=None, *, callback_response=None):
"""
Create iterator
:param cterasdk.objects.core.Portal core: Portal object
:param str path: URL Path
:param str,optional name: Schema method name
:param cterasdk.core.query.QueryParams,optional param: Query paramter object
:param cterasdk.lib.iterator.BaseResponse callback_response: Class to consume callback response
:returns: Query iterator object
"""
callback_response = callback_response if callback_response else DefaultResponse
callback_function = create_callback_function(core, path, name, callback_response=callback_response)
return QueryIterator(callback_function, param if param else QueryParams())
class Restriction:
LIKE = "like"
UNLIKE = "notLike"
EQUALS = "eq"
NOT_EQUALS = "ne"
GREATER_EQUALS = "ge"
GREATER_THAN = "gt"
LESS_EQUALS = "le"
LESS_THAN = "lt"
class FilterType:
DateTime = 'DateTimeFilter'
Boolean = 'BooleanFilter'
Integer = 'IntFilter'
String = 'StringFilter'
RefFilter = 'RefFilter'
IntRefFilter = 'IntRefFilter'
BooleanRefFilter = 'BooleanRefFilter'
@staticmethod
def fromValue(value, ref):
if isinstance(value, datetime):
return FilterType.DateTime
if isinstance(value, bool):
return FilterType.BooleanRefFilter if ref else FilterType.Boolean
if isinstance(value, int):
return FilterType.IntRefFilter if ref else FilterType.Integer
if isinstance(value, str):
return FilterType.RefFilter if ref else FilterType.String
raise TypeError("value must be of one of the following types: datetime, bool, int or str")
class Filter(Object):
def __init__(self, field):
super().__init__()
self.field = field
class FilterBuilder(Object):
def __init__(self, name, reference=False):
super().__init__()
self.filter = Filter(name)
self.reference = reference
@staticmethod
def ref(name):
return FilterBuilder(name, True)
def like(self, value):
self.filter.restriction = Restriction.LIKE # pylint: disable=attribute-defined-outside-init
return self.setValue(value)
def notLike(self, value):
self.filter.restriction = Restriction.UNLIKE # pylint: disable=attribute-defined-outside-init
return self.setValue(value)
def eq(self, value):
self.filter.restriction = Restriction.EQUALS # pylint: disable=attribute-defined-outside-init
return self.setValue(value)
def ne(self, value):
self.filter.restriction = Restriction.NOT_EQUALS # pylint: disable=attribute-defined-outside-init
return self.setValue(value)
def ge(self, value):
self.filter.restriction = Restriction.GREATER_EQUALS # pylint: disable=attribute-defined-outside-init
return self.setValue(value)
def gt(self, value):
self.filter.restriction = Restriction.GREATER_THAN # pylint: disable=attribute-defined-outside-init
return self.setValue(value)
def le(self, value):
self.filter.restriction = Restriction.LESS_EQUALS # pylint: disable=attribute-defined-outside-init
return self.setValue(value)
def lt(self, value):
self.filter.restriction = Restriction.LESS_THAN # pylint: disable=attribute-defined-outside-init
return self.setValue(value)
def before(self, value):
self.filter.restriction = Restriction.LESS_THAN # pylint: disable=attribute-defined-outside-init
return self.setValue(value)
def after(self, value):
self.filter.restriction = Restriction.GREATER_THAN # pylint: disable=attribute-defined-outside-init
return self.setValue(value)
def setValue(self, value):
self.filter._classname = FilterType.fromValue(value, self.reference) # pylint: disable=protected-access,W0201
if isinstance(value, datetime):
value = value.strftime('%Y-%m-%dT%H:%M:%S')
self.filter.value = value # pylint: disable=attribute-defined-outside-init
return self.filter
class QueryParams(Object):
def __init__(self):
super().__init__()
self.startFrom = 0
self.countLimit = 50
def include_classname(self):
self._classname = self.__class__.__name__ # pylint: disable=attribute-defined-outside-init
def increment(self):
self.startFrom = self.startFrom + self.countLimit
class QueryParamBuilder:
def __init__(self):
self.param = QueryParams()
def include_classname(self):
self.param.include_classname()
return self
def startFrom(self, startFrom):
self.param.startFrom = startFrom
return self
def countLimit(self, countLimit):
self.param.countLimit = countLimit
return self
def include(self, include):
self.param.include = include # pylint: disable=attribute-defined-outside-init
return self
def addFilter(self, query_filter):
if not hasattr(self.param, 'filters'):
self.param.filters = [] # pylint: disable=attribute-defined-outside-init
self.param.filters.append(query_filter)
return self
def orFilter(self, orFilter):
self.param.orFilter = orFilter # pylint: disable=attribute-defined-outside-init
return self
def sortBy(self, sortBy):
self.param.sortBy = sortBy # pylint: disable=attribute-defined-outside-init
return self
def allPortals(self, allPortals):
self.param.allPortals = allPortals # pylint: disable=attribute-defined-outside-init
return self
def ownedBy(self, ownedBy):
self.param.ownedBy = ownedBy # pylint: disable=attribute-defined-outside-init
return self
def put(self, key, value):
setattr(self.param, key, value)
return self
def build(self):
return self.param