This repository has been archived by the owner on Sep 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathodata_service_context.py
375 lines (342 loc) · 15.1 KB
/
odata_service_context.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# OData Python Client and Server Libraries ver. 1.0.0
# Copyright (c) Microsoft Corporation
# All rights reserved.
# MIT License
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
import odata_client_python
import urllib
class odata_service_context(object):
def __init__(self, base_address, options=odata_client_python.client_options()):
self._client = odata_client_python.odata_client(base_address, options)
self._model = None
def get_edm_model(self):
if self._model is None:
self._model = self._client.get_model().get()
return self._model
def get_client(self):
return self._client
def get_root_url(self):
return self._client.get_service_root_url()
def get_relative_path(self, full_path):
return self._client.get_relative_path(full_path)
def get_expand_option_expression(self, path, filter=None, top=None, skip=None, select=None, orderby=None, expand=None):
query_ex = path
is_first = True
if filter is not None:
if is_first:
query_ex += '('
is_first = False
else:
query_ex += ';'
query_ex += "$filter=" + filter
if top is not None:
if is_first:
query_ex += '('
is_first = False
else:
query_ex += ';'
query_ex += "$top=" + str(top)
if skip is not None:
if is_first:
query_ex += '('
is_first = False
else:
query_ex += ';'
query_ex += "$skip=" + str(skip)
if select is not None:
if is_first:
query_ex += '('
is_first = False
else:
query_ex += ';'
query_ex += "$select=" + select
if orderby is not None:
if is_first:
query_ex += '('
is_first = False
else:
query_ex += ';'
query_ex += "$orderby=" + orderby
if expand is not None:
if is_first:
query_ex += '('
is_first = False
else:
query_ex += ';'
query_ex += "$expand=" + expand
if not is_first:
query_ex += ')'
return query_ex
def get_query_expression(self, path, key=None, filter=None, top=None, skip=None, select=None, orderby=None, expand=None):
query_ex = path
if key is not None:
query_ex += '(' + key + ')'
is_first = True
if filter is not None:
if is_first:
query_ex += '?'
is_first = False
else:
query_ex += '&'
query_ex += "$filter=" + filter
if top is not None:
if is_first:
query_ex += '?'
is_first = False
else:
query_ex += '&'
query_ex += "$top=" + str(top)
if skip is not None:
if is_first:
query_ex += '?'
is_first = False
else:
query_ex += '&'
query_ex += "$skip=" + str(skip)
if select is not None:
if is_first:
query_ex += '?'
is_first = False
else:
query_ex += '&'
query_ex += "$select=" + select
if orderby is not None:
if is_first:
query_ex += '?'
is_first = False
else:
query_ex += '&'
query_ex += "$orderby=" + orderby
if expand is not None:
if is_first:
query_ex += '?'
is_first = False
else:
query_ex += '&'
query_ex += "$expand=" + expand
return urllib.quote(query_ex, '?$&,;=()/')
def query(self, query_ex):
return self._client.get_data_from_server(query_ex).get()
def query_primitive(self, query_ex, ret_type):
if self._client is None:
return []
values = self._client.get_data_from_server(query_ex).get()
ret = []
for value in values:
if value is None:
continue
primitive_value = odata_client_python.to_primitive_value(value)
if primitive_value is None:
continue
try:
if ret_type is int:
val = int(primitive_value.to_string())
elif ret_type is float:
val = float(primitive_value.to_string())
elif ret_type is bool:
val = primitive_value.to_string() == "true"
elif ret_type is str or ret_type is unicode:
val = primitive_value.to_string()
else:
val = eval(primitive_value.to_string())
except:
val = primitive_value.to_string()
ret.append(val)
return ret
def query_enum(self, query_ex, ret_type):
if self._client is None:
return []
values = self._client.get_data_from_server(query_ex).get()
ret = []
for value in values:
if value is None:
continue
enum_value = odata_client_python.to_enum_value(value)
if enum_value is None:
continue
val = ret_type.get_enum_value_from_string(enum_value.to_string())
ret.append(val)
return ret
def query_complex(self, query_ex, ret_type):
if self._client is None:
return []
values = self._client.get_data_from_server(query_ex).get()
ret = []
for value in values:
if value is None:
continue
complex_value = odata_client_python.to_complex_value(value)
if complex_value is None:
continue
ret.append(ret_type.create_instance_from_complex(complex_value, self))
return ret
def query_entityset(self, query_ex, ret_type):
if self._client is None:
return []
values = self._client.get_data_from_server(query_ex).get()
ret = []
for value in values:
if value is None:
continue
entity_value = odata_client_python.to_entity_value(value)
if entity_value is None:
continue
ret.append(ret_type.create_instance_from_entity(entity_value, self))
return ret
def query_singleton(self, query_ex, ret_type):
if self._client is None:
return None
values = self._client.get_data_from_server(query_ex).get()
if len(values) != 1:
return None
entity_value = odata_client_python.to_entity_value(values[0])
return ret_type.create_instance_from_entity(entity_value, self)
def operation_query_primitive(self, query_ex, parameters, is_function, ret_type):
if self._client is None:
return None
ret_values = odata_client_python.vector_odata_value()
self._client.send_data_to_server(query_ex, parameters, ret_values, odata_client_python.HTTP_GET if is_function else odata_client_python.HTTP_POST).get()
ret = []
for value in ret_values:
if value is None:
continue
primitive_value = odata_client_python.to_primitive_value(value)
if primitive_value is None:
continue
try:
if ret_type in ("uint8_t", "int8_t", "int16_t", "int32_t", "int64_t") or ret_type is int:
val = int(primitive_value.to_string())
elif ret_type in ("float", "double", "long double") or ret_type is float:
val = float(primitive_value.to_string())
elif ret_type == "bool" or ret_type is bool:
val = primitive_value.to_string() == "true"
elif ret_type == "::utility::string_t" or ret_type is str or ret_type is unicode:
val = primitive_value.to_string()
else:
val = eval(primitive_value.to_string())
except:
val = primitive_value.to_string()
ret.append(val)
return ret
def operation_query_enum(self, query_ex, parameters, is_function, ret_type):
if self._client is None:
return None
ret_values = odata_client_python.vector_odata_value()
self._client.send_data_to_server(query_ex, parameters, ret_values, odata_client_python.HTTP_GET if is_function else odata_client_python.HTTP_POST).get()
ret = []
for value in ret_values:
if value is None:
continue
enum_value = odata_client_python.to_enum_value(value)
if enum_value is None:
continue
val = ret_type.get_enum_value_from_string(enum_value.to_string())
ret.append(val)
return ret
def operation_query_complex(self, query_ex, parameters, is_function, ret_type):
if self._client is None:
return None
ret_values = odata_client_python.vector_odata_value()
self._client.send_data_to_server(query_ex, parameters, ret_values, odata_client_python.HTTP_GET if is_function else odata_client_python.HTTP_POST).get()
ret = []
for value in ret_values:
if value is None:
continue
complex_value = odata_client_python.to_complex_value(value)
if complex_value is None:
continue
ret.append(ret_type.create_instance_from_complex(complex_value, self))
return ret
def operation_query_entityset(self, query_ex, parameters, is_function, ret_type):
if self._client is None:
return None
ret_values = odata_client_python.vector_odata_value()
self._client.send_data_to_server(query_ex, parameters, ret_values, odata_client_python.HTTP_GET if is_function else odata_client_python.HTTP_POST).get()
ret = []
for value in ret_values:
if value is None:
continue
entity_value = odata_client_python.to_entity_value(value)
if entity_value is None:
continue
ret.append(ret_type.create_instance_from_entity(entity_value, self))
return ret
def operation_query_singleton(self, query_ex, parameters, is_function, ret_type):
if self._client is None:
return None
ret_values = odata_client_python.vector_odata_value()
self._client.send_data_to_server(query_ex, parameters, ret_values, odata_client_python.HTTP_GET if is_function else odata_client_python.HTTP_POST).get()
ret = None
if len(ret_values) != 1:
return None
entity_value = odata_client_python.to_entity_value(ret_values[0])
return ret_type.create_instance_from_entity(entity_value, self)
def operation_query_void(self, query_ex, parameters, is_function):
if self._client is None:
return -1
ret_values = odata_client_python.vector_odata_value()
self._client.send_data_to_server(query_ex, parameters, ret_values, odata_client_python.HTTP_GET if is_function else odata_client_python.HTTP_POST).get()
return 0
def add_object(self, path, _object):
if self._client is None or _object is None:
return -1
model = self._client.get_model().get()
if model is None:
return -1
entity_value = odata_client_python.to_entity_value(_object.to_value())
if entity_value is None:
return -1
entity_value.set_value(odata_client_python.odata_json_constants.PAYLOAD_ANNOTATION_TYPE, _object.get_full_name())
ret_code = self._client.create_entity(path, entity_value).get()
edit_link = entity_value.get_edit_link()
if edit_link != "":
_object.set_edit_link(edit_link)
return ret_code
def update_object(self, _object):
if self._client is None or _object is None:
return -1
return self._client.send_data_to_server(self.get_relative_path(_object.get_edit_link()), _object.to_value(), odata_client_python.HTTP_PATCH).get()
def delete_object(self, _object):
if self._client is None or _object is None:
return -1
return self._client.send_data_to_server(self.get_relative_path(_object.get_edit_link()), odata_client_python.HTTP_DELETE).get()
def add_related_object(self, _parent, path, _object):
model = self.get_edm_model()
if _parent is None or model is None or _object is None:
return -1
return self.add_object(self.get_relative_path(_parent.get_edit_link() + '/' + path), _object)
def update_related_object(self, _parent, path, _object):
if _parent is None or _object is None or self._client is None:
return -1
full_navigation_path = _parent.get_edit_link() + "/" + path
navigation_path = self.get_relative_path(full_navigation_path)
ret_code = self._client.send_data_to_server(self.get_relative_path(navigation_path), _object.to_value(), odata_client_python.HTTP_PATCH).get()
if _object is not None:
_object.set_edit_link(full_navigation_path)
return ret_code
def add_reference(self, _parent, path, _object):
if self._client is None or _parent is None or _object is None:
return -1
return self._client.add_reference(self.get_relative_path(_parent.get_edit_link() + "/" + path), _object.get_edit_link()).get()
def update_reference(self, _parent, path, _object):
if self._client is None or _parent is None or _object is None:
return -1
return self._client.update_reference(self.get_relative_path(_parent.get_edit_link() + "/" + path), _object.get_edit_link()).get()
def delete_reference(self, _parent, path, _object):
if self._client is None or _parent is None or _object is None:
return -1
return self._client.remove_reference(self.get_relative_path(_parent.get_edit_link() + "/" + path), _object.get_edit_link()).get()