python-bcb 0.2.0__py3-none-any.whl → 0.3.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- bcb/__init__.py +15 -15
- bcb/odata/__init__.py +15 -15
- bcb/odata/api.py +494 -494
- bcb/odata/framework.py +525 -525
- bcb/{sgs.py → sgs/__init__.py} +132 -131
- bcb/sgs/regional_economy.py +196 -0
- bcb/utils.py +12 -0
- {python_bcb-0.2.0.dist-info → python_bcb-0.3.2.dist-info}/METADATA +8 -7
- python_bcb-0.3.2.dist-info/RECORD +12 -0
- {python_bcb-0.2.0.dist-info → python_bcb-0.3.2.dist-info}/WHEEL +1 -1
- python_bcb-0.2.0.dist-info/RECORD +0 -11
- {python_bcb-0.2.0.dist-info → python_bcb-0.3.2.dist-info}/LICENSE +0 -0
bcb/odata/framework.py
CHANGED
|
@@ -1,525 +1,525 @@
|
|
|
1
|
-
from io import BytesIO
|
|
2
|
-
import logging
|
|
3
|
-
from lxml import etree
|
|
4
|
-
import json
|
|
5
|
-
import httpx
|
|
6
|
-
from urllib.parse import quote
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
# Edm.Boolean
|
|
10
|
-
# Edm.Byte
|
|
11
|
-
# Edm.Date
|
|
12
|
-
# Edm.DateTimeOffset
|
|
13
|
-
# Edm.Decimal
|
|
14
|
-
# Edm.Duration
|
|
15
|
-
# Edm.Guid
|
|
16
|
-
# Edm.Int16
|
|
17
|
-
# Edm.Int32
|
|
18
|
-
# Edm.Int64
|
|
19
|
-
# Edm.SByte
|
|
20
|
-
# Edm.String
|
|
21
|
-
# Edm.TimeOfDay
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
def str_types(type):
|
|
25
|
-
if type == "Edm.Decimal":
|
|
26
|
-
return "float"
|
|
27
|
-
elif type in ("Edm.Int32", "Edm.Int64", "Edm.Int16"):
|
|
28
|
-
return "int"
|
|
29
|
-
elif type == "Edm.String":
|
|
30
|
-
return "str"
|
|
31
|
-
elif type == "Edm.Boolean":
|
|
32
|
-
return "bool"
|
|
33
|
-
elif type in ("Edm.Date", "Edm.TimeOfDay"):
|
|
34
|
-
return "datetime"
|
|
35
|
-
else:
|
|
36
|
-
return type
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
class ODataEndPoint:
|
|
40
|
-
def __init__(self, **kwargs):
|
|
41
|
-
self.data = kwargs
|
|
42
|
-
|
|
43
|
-
def __getitem__(self, item):
|
|
44
|
-
return self.data[item]
|
|
45
|
-
|
|
46
|
-
def __setitem__(self, key, value):
|
|
47
|
-
self.data[key] = value
|
|
48
|
-
|
|
49
|
-
def __repr__(self):
|
|
50
|
-
url = self.data["url"]
|
|
51
|
-
return f"<EndPoint {url}>"
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
class ODataEntitySetMeta(type):
|
|
55
|
-
def __init__(self, *args, **kwargs):
|
|
56
|
-
super().__init__(*args, **kwargs)
|
|
57
|
-
|
|
58
|
-
def __call__(self, *args):
|
|
59
|
-
obj = super().__call__(*args)
|
|
60
|
-
entity = args[2]
|
|
61
|
-
for name, prop in entity.properties.items():
|
|
62
|
-
setattr(obj, name, prop)
|
|
63
|
-
return obj
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
class ODataEntitySet(metaclass=ODataEntitySetMeta):
|
|
67
|
-
def __init__(self, name, entity_type, entity):
|
|
68
|
-
self.name = name
|
|
69
|
-
self.entity_type = entity_type
|
|
70
|
-
self.entity = entity
|
|
71
|
-
|
|
72
|
-
def describe(self):
|
|
73
|
-
props = ", ".join(
|
|
74
|
-
[f"{prop.name}<{prop.ftype}>" for prop in self.entity.properties.values()]
|
|
75
|
-
)
|
|
76
|
-
print(
|
|
77
|
-
f"""
|
|
78
|
-
EntitySet (Endpoint): {self.name}
|
|
79
|
-
EntityType: {self.entity_type}
|
|
80
|
-
Properties: {props}"""
|
|
81
|
-
)
|
|
82
|
-
|
|
83
|
-
def __repr__(self):
|
|
84
|
-
return f"<EntitySet {self.name}>"
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
class ODataFunctionImport:
|
|
88
|
-
def __init__(self, name, function, entity_set):
|
|
89
|
-
self.name = name
|
|
90
|
-
self.entity_set = entity_set
|
|
91
|
-
self.function = function
|
|
92
|
-
|
|
93
|
-
def describe(self):
|
|
94
|
-
props = ", ".join(
|
|
95
|
-
[
|
|
96
|
-
f"{prop.name} <{prop.ftype}>"
|
|
97
|
-
for prop in self.entity_set.entity.properties.values()
|
|
98
|
-
]
|
|
99
|
-
)
|
|
100
|
-
params = ", ".join(
|
|
101
|
-
[f"{param.name} <{param.ftype}>" for param in self.function.parameters]
|
|
102
|
-
)
|
|
103
|
-
s = f"""
|
|
104
|
-
Function: {self.function.name}
|
|
105
|
-
Parameters: {params}
|
|
106
|
-
EntitySet: {self.entity_set.name}
|
|
107
|
-
EntityType: {self.entity_set.entity_type}
|
|
108
|
-
Properties: {props}"""
|
|
109
|
-
print(s)
|
|
110
|
-
|
|
111
|
-
def __repr__(self):
|
|
112
|
-
return f"<FunctionImport {self.name}>"
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
class ODataParameter:
|
|
116
|
-
def __init__(self, **kwargs):
|
|
117
|
-
self.data = kwargs
|
|
118
|
-
|
|
119
|
-
@property
|
|
120
|
-
def name(self):
|
|
121
|
-
return self.data.get("Name")
|
|
122
|
-
|
|
123
|
-
@property
|
|
124
|
-
def type(self):
|
|
125
|
-
return self.data.get("Type")
|
|
126
|
-
|
|
127
|
-
@property
|
|
128
|
-
def ftype(self):
|
|
129
|
-
return str_types(self.type)
|
|
130
|
-
|
|
131
|
-
@property
|
|
132
|
-
def required(self):
|
|
133
|
-
nullable = self.data.get("Nullable")
|
|
134
|
-
if nullable:
|
|
135
|
-
return nullable == "false"
|
|
136
|
-
else:
|
|
137
|
-
return True
|
|
138
|
-
|
|
139
|
-
def format(self, value):
|
|
140
|
-
if self.type == "Edm.Decimal":
|
|
141
|
-
return f"{float(value)}"
|
|
142
|
-
elif self.type == "Edm.Int32":
|
|
143
|
-
return f"{int(value)}"
|
|
144
|
-
elif self.type == "Edm.String":
|
|
145
|
-
return f"'{str(value)}'"
|
|
146
|
-
else:
|
|
147
|
-
return f"'{str(value)}'"
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
class ODataPropertyOrderBy:
|
|
151
|
-
def __init__(self, obj, order):
|
|
152
|
-
self.obj = obj
|
|
153
|
-
self.order = order
|
|
154
|
-
|
|
155
|
-
def __str__(self):
|
|
156
|
-
return f"{self.obj.name} {self.order}"
|
|
157
|
-
|
|
158
|
-
def __repr__(self):
|
|
159
|
-
return f"<{str(self)}>"
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
class ODataPropertyFilter:
|
|
163
|
-
def __init__(self, obj, oth, operator):
|
|
164
|
-
self.obj = obj
|
|
165
|
-
self.other = oth
|
|
166
|
-
self.operator = operator
|
|
167
|
-
|
|
168
|
-
def statement(self):
|
|
169
|
-
if self.obj.type == "Edm.Decimal":
|
|
170
|
-
return f"{self.obj.name} {self.operator} {float(self.other)}"
|
|
171
|
-
elif self.obj.type == "Edm.Int32":
|
|
172
|
-
return f"{self.obj.name} {self.operator} {int(self.other)}"
|
|
173
|
-
elif self.obj.type == "Edm.String":
|
|
174
|
-
return f"{self.obj.name} {self.operator} '{str(self.other)}'"
|
|
175
|
-
elif self.obj.type == "Edm.Date":
|
|
176
|
-
return f"{self.obj.name} {self.operator} {self.other.strftime('%Y-%m-%d')}"
|
|
177
|
-
else:
|
|
178
|
-
return f"{self.obj.name} {self.operator} '{self.other}'"
|
|
179
|
-
|
|
180
|
-
def __str__(self):
|
|
181
|
-
return self.statement()
|
|
182
|
-
|
|
183
|
-
def __repr__(self):
|
|
184
|
-
return f"<filter: {str(self)}>"
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
class ODataProperty:
|
|
188
|
-
def __init__(self, **kwargs):
|
|
189
|
-
self.data = kwargs
|
|
190
|
-
|
|
191
|
-
@property
|
|
192
|
-
def name(self):
|
|
193
|
-
return self.data.get("Name")
|
|
194
|
-
|
|
195
|
-
@property
|
|
196
|
-
def type(self):
|
|
197
|
-
return self.data.get("Type")
|
|
198
|
-
|
|
199
|
-
@property
|
|
200
|
-
def ftype(self):
|
|
201
|
-
return str_types(self.type)
|
|
202
|
-
|
|
203
|
-
def asc(self):
|
|
204
|
-
return ODataPropertyOrderBy(self, "asc")
|
|
205
|
-
|
|
206
|
-
def desc(self):
|
|
207
|
-
return ODataPropertyOrderBy(self, "desc")
|
|
208
|
-
|
|
209
|
-
def __gt__(self, other):
|
|
210
|
-
return ODataPropertyFilter(self, other, "gt")
|
|
211
|
-
|
|
212
|
-
def __ge__(self, other):
|
|
213
|
-
return ODataPropertyFilter(self, other, "ge")
|
|
214
|
-
|
|
215
|
-
def __lt__(self, other):
|
|
216
|
-
return ODataPropertyFilter(self, other, "lt")
|
|
217
|
-
|
|
218
|
-
def __le__(self, other):
|
|
219
|
-
return ODataPropertyFilter(self, other, "le")
|
|
220
|
-
|
|
221
|
-
def __eq__(self, other):
|
|
222
|
-
return ODataPropertyFilter(self, other, "eq")
|
|
223
|
-
|
|
224
|
-
def __repr__(self):
|
|
225
|
-
return f"<Property {self.name}<{self.ftype}>>"
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
class ODataFunction:
|
|
229
|
-
def __init__(self, **kwargs):
|
|
230
|
-
self.name = kwargs["Name"]
|
|
231
|
-
self.parameters = kwargs["parameters"]
|
|
232
|
-
self.return_type = kwargs["return_type"]
|
|
233
|
-
self.fullname = f'{kwargs["namespace"]}.{self.name}'
|
|
234
|
-
|
|
235
|
-
def __repr__(self):
|
|
236
|
-
return f"<Function {self.name}>"
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
class ODataEntity:
|
|
240
|
-
def __init__(self, name, properties, namespace):
|
|
241
|
-
self.name = name
|
|
242
|
-
self.properties = properties
|
|
243
|
-
self.fullname = f"{namespace}.{name}"
|
|
244
|
-
|
|
245
|
-
def __repr__(self):
|
|
246
|
-
return f"<Entity {self.name}>"
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
class ODataMetadata:
|
|
250
|
-
namespaces = {
|
|
251
|
-
"edm": "http://docs.oasis-open.org/odata/ns/edm",
|
|
252
|
-
"edmx": "http://docs.oasis-open.org/odata/ns/edmx",
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
def __init__(self, url):
|
|
256
|
-
self.url = url
|
|
257
|
-
self._load_document()
|
|
258
|
-
_xpath = "edmx:DataServices/edm:Schema"
|
|
259
|
-
schema = self.doc.xpath(_xpath, namespaces=self.namespaces)[0]
|
|
260
|
-
self.namespace = schema.attrib["Namespace"]
|
|
261
|
-
self._used_elements = []
|
|
262
|
-
self._parse_entities(schema)
|
|
263
|
-
self._parse_entity_sets(schema)
|
|
264
|
-
self._parse_functions(schema)
|
|
265
|
-
self._parse_function_imports(schema)
|
|
266
|
-
|
|
267
|
-
def _load_document(self):
|
|
268
|
-
res = httpx.get(self.url, timeout=60.0)
|
|
269
|
-
self.doc = etree.parse(BytesIO(res.content))
|
|
270
|
-
|
|
271
|
-
def _parse_entity(self, entity_element, namespace):
|
|
272
|
-
name = entity_element.attrib["Name"]
|
|
273
|
-
props = {
|
|
274
|
-
prop.attrib["Name"]: ODataProperty(**prop.attrib) for prop in entity_element
|
|
275
|
-
}
|
|
276
|
-
return ODataEntity(name, props, namespace)
|
|
277
|
-
|
|
278
|
-
def _parse_entities(self, schema):
|
|
279
|
-
_xpath = "edm:EntityType"
|
|
280
|
-
self.entities = [
|
|
281
|
-
self._parse_entity(e, self.namespace)
|
|
282
|
-
for e in schema.xpath(_xpath, namespaces=self.namespaces)
|
|
283
|
-
]
|
|
284
|
-
|
|
285
|
-
self._entities_fullnames = {e.fullname: e for e in self.entities}
|
|
286
|
-
|
|
287
|
-
def _parse_entity_sets(self, schema):
|
|
288
|
-
_xpath = "edm:EntityContainer/edm:EntitySet"
|
|
289
|
-
|
|
290
|
-
def _parse_entity_set(e):
|
|
291
|
-
entity = self._entities_fullnames[e.attrib["EntityType"]]
|
|
292
|
-
return ODataEntitySet(e.attrib["Name"], e.attrib["EntityType"], entity)
|
|
293
|
-
|
|
294
|
-
self.entity_sets = {
|
|
295
|
-
e.attrib["Name"]: _parse_entity_set(e)
|
|
296
|
-
for e in schema.xpath(_xpath, namespaces=self.namespaces)
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
for n, es in self.entity_sets.items():
|
|
300
|
-
es.fullname = f"{self.namespace}.{n}"
|
|
301
|
-
|
|
302
|
-
self._entity_sets_fullnames = {
|
|
303
|
-
es.fullname: es for es in self.entity_sets.values()
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
def _parse_functions(self, schema):
|
|
307
|
-
_xpath = "edm:Function"
|
|
308
|
-
|
|
309
|
-
def _parse_function(e):
|
|
310
|
-
parameters = []
|
|
311
|
-
return_type = None
|
|
312
|
-
for element in e:
|
|
313
|
-
if element.tag.endswith("Parameter"):
|
|
314
|
-
parameters.append(ODataParameter(**element.attrib))
|
|
315
|
-
elif element.tag.endswith("ReturnType"):
|
|
316
|
-
return_type = element.attrib["Type"]
|
|
317
|
-
kwargs = dict(**e.attrib)
|
|
318
|
-
kwargs["parameters"] = parameters
|
|
319
|
-
kwargs["return_type"] = return_type
|
|
320
|
-
kwargs["namespace"] = self.namespace
|
|
321
|
-
return ODataFunction(**kwargs)
|
|
322
|
-
|
|
323
|
-
self.functions = {
|
|
324
|
-
e.attrib["Name"]: _parse_function(e)
|
|
325
|
-
for e in schema.xpath(_xpath, namespaces=self.namespaces)
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
self._functions_fullnames = {f.fullname: f for f in self.functions.values()}
|
|
329
|
-
|
|
330
|
-
def _parse_function_imports(self, schema):
|
|
331
|
-
_xpath = "edm:EntityContainer/edm:FunctionImport"
|
|
332
|
-
|
|
333
|
-
def _parse_function_import(e):
|
|
334
|
-
function = self._functions_fullnames[e.attrib["Function"]]
|
|
335
|
-
entity_set = self._entity_sets_fullnames[e.attrib["EntitySet"]]
|
|
336
|
-
self._used_elements.append(e.attrib["EntitySet"])
|
|
337
|
-
return ODataFunctionImport(e.attrib["Name"], function, entity_set)
|
|
338
|
-
|
|
339
|
-
self.function_imports = {
|
|
340
|
-
e.attrib["Name"]: _parse_function_import(e)
|
|
341
|
-
for e in schema.xpath(_xpath, namespaces=self.namespaces)
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
class ODataService:
|
|
346
|
-
def __init__(self, url):
|
|
347
|
-
self.url = url
|
|
348
|
-
res = httpx.get(self.url, timeout=60.0)
|
|
349
|
-
self.api_data = json.loads(res.text)
|
|
350
|
-
self.endpoints = [ODataEndPoint(**x) for x in self.api_data["value"]]
|
|
351
|
-
self._odata_context_url = self.api_data["@odata.context"]
|
|
352
|
-
self.metadata = ODataMetadata(self._odata_context_url)
|
|
353
|
-
|
|
354
|
-
def __getitem__(self, item):
|
|
355
|
-
es = self.entity_sets.get(item)
|
|
356
|
-
if es is None:
|
|
357
|
-
fi = self.function_imports.get(item)
|
|
358
|
-
if fi is not None:
|
|
359
|
-
return fi
|
|
360
|
-
else:
|
|
361
|
-
raise ValueError("Invalid name: " + item)
|
|
362
|
-
else:
|
|
363
|
-
return es
|
|
364
|
-
|
|
365
|
-
@property
|
|
366
|
-
def entities(self):
|
|
367
|
-
return self.metadata.entities
|
|
368
|
-
|
|
369
|
-
@property
|
|
370
|
-
def function_imports(self):
|
|
371
|
-
return self.metadata.function_imports
|
|
372
|
-
|
|
373
|
-
@property
|
|
374
|
-
def entity_sets(self):
|
|
375
|
-
return self.metadata.entity_sets
|
|
376
|
-
|
|
377
|
-
def describe(self):
|
|
378
|
-
es_names = []
|
|
379
|
-
for es in self.entity_sets.keys():
|
|
380
|
-
k = f"{self.metadata.namespace}.{es}"
|
|
381
|
-
if k not in self.metadata._used_elements:
|
|
382
|
-
es_names.append(es)
|
|
383
|
-
if len(es_names):
|
|
384
|
-
print("EntitySets:")
|
|
385
|
-
for es in es_names:
|
|
386
|
-
print(" ", es)
|
|
387
|
-
if len(self.function_imports):
|
|
388
|
-
print("FunctionImports:")
|
|
389
|
-
for es in self.function_imports.keys():
|
|
390
|
-
print(" ", es)
|
|
391
|
-
|
|
392
|
-
def query(self, entity_set):
|
|
393
|
-
return ODataQuery(entity_set, self.url)
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
class ODataQuery:
|
|
397
|
-
def __init__(self, entity, url):
|
|
398
|
-
self.entity = entity
|
|
399
|
-
self.base_url = url
|
|
400
|
-
self._params = {}
|
|
401
|
-
self.function_parameters = {}
|
|
402
|
-
self._filter = []
|
|
403
|
-
self._select = []
|
|
404
|
-
self._orderby = []
|
|
405
|
-
self._raw = False
|
|
406
|
-
self.is_function = isinstance(entity, ODataFunctionImport)
|
|
407
|
-
if self.is_function:
|
|
408
|
-
self.function_parameters = {
|
|
409
|
-
p.name: None for p in self.entity.function.parameters
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
def odata_url(self):
|
|
413
|
-
self._url = (
|
|
414
|
-
f"{self.base_url}{self.entity.name}"
|
|
415
|
-
if self.base_url.endswith("/")
|
|
416
|
-
else f"{self.base_url}/{self.entity.name}"
|
|
417
|
-
)
|
|
418
|
-
if self.is_function:
|
|
419
|
-
args = [f"{p.name}=@{p.name}" for p in self.entity.function.parameters]
|
|
420
|
-
args = ",".join(args)
|
|
421
|
-
return f"{self._url}({args})"
|
|
422
|
-
else:
|
|
423
|
-
return self._url
|
|
424
|
-
|
|
425
|
-
def parameters(self, **kwargs):
|
|
426
|
-
for arg in kwargs:
|
|
427
|
-
if arg in self.function_parameters:
|
|
428
|
-
self.function_parameters[arg] = kwargs[arg]
|
|
429
|
-
else:
|
|
430
|
-
raise ValueError(f"Unknown parameter: {arg}")
|
|
431
|
-
return self
|
|
432
|
-
|
|
433
|
-
def filter(self, *args):
|
|
434
|
-
if len(args):
|
|
435
|
-
self._filter.extend(args)
|
|
436
|
-
return self
|
|
437
|
-
|
|
438
|
-
def limit(self, limit):
|
|
439
|
-
self._params["$top"] = limit
|
|
440
|
-
return self
|
|
441
|
-
|
|
442
|
-
def skip(self, skip):
|
|
443
|
-
self._params["$skip"] = skip
|
|
444
|
-
return self
|
|
445
|
-
|
|
446
|
-
def format(self, fmt):
|
|
447
|
-
self._params["$format"] = fmt
|
|
448
|
-
return self
|
|
449
|
-
|
|
450
|
-
def orderby(self, *args):
|
|
451
|
-
if len(args):
|
|
452
|
-
self._orderby.extend(args)
|
|
453
|
-
return self
|
|
454
|
-
|
|
455
|
-
def select(self, *args):
|
|
456
|
-
if len(args):
|
|
457
|
-
self._select.extend(args)
|
|
458
|
-
return self
|
|
459
|
-
|
|
460
|
-
def raw(self):
|
|
461
|
-
self._raw = True
|
|
462
|
-
return self
|
|
463
|
-
|
|
464
|
-
def _build_parameters(self):
|
|
465
|
-
params = {"$format": self._params.get("$format", "json")}
|
|
466
|
-
if len(self._filter):
|
|
467
|
-
_filter = " and ".join(str(f) for f in self._filter)
|
|
468
|
-
params["$filter"] = _filter
|
|
469
|
-
if len(self._orderby):
|
|
470
|
-
_orderby = ",".join(str(f) for f in self._orderby)
|
|
471
|
-
params["$orderby"] = _orderby
|
|
472
|
-
if len(self._select):
|
|
473
|
-
_select = ",".join(str(f.name) for f in self._select)
|
|
474
|
-
params["$select"] = _select
|
|
475
|
-
params.update(self._params)
|
|
476
|
-
return params
|
|
477
|
-
|
|
478
|
-
def reset(self):
|
|
479
|
-
self._filter = []
|
|
480
|
-
self._orderby = []
|
|
481
|
-
self._params = {}
|
|
482
|
-
|
|
483
|
-
def collect(self):
|
|
484
|
-
return json.loads(self.text())
|
|
485
|
-
|
|
486
|
-
def text(self):
|
|
487
|
-
params = self._build_parameters()
|
|
488
|
-
if self.is_function and len(self.function_parameters):
|
|
489
|
-
for p in self.entity.function.parameters:
|
|
490
|
-
val = self.function_parameters[p.name]
|
|
491
|
-
if p.required and val is None:
|
|
492
|
-
raise ValueError("Parameter not set: " + p.name)
|
|
493
|
-
params["@" + p.name] = p.format(val)
|
|
494
|
-
qs = "&".join([f"{quote(k)}={quote(str(v))}" for k, v in params.items()])
|
|
495
|
-
headers = {"OData-Version": "4.0", "OData-MaxVersion": "4.0"}
|
|
496
|
-
res = httpx.get(self.odata_url() + "?" + qs, headers=headers, timeout=60.0)
|
|
497
|
-
return res.text
|
|
498
|
-
|
|
499
|
-
def show(self):
|
|
500
|
-
print(f"URL:")
|
|
501
|
-
print(f" {self.odata_url()}")
|
|
502
|
-
if self.is_function and len(self.function_parameters):
|
|
503
|
-
print("Function Parameters:")
|
|
504
|
-
for p in self.entity.function.parameters:
|
|
505
|
-
v = self.function_parameters[p.name]
|
|
506
|
-
req = " (required) " if p.required else " "
|
|
507
|
-
print(f" {p.name}<{p.ftype}>{req}= {v}")
|
|
508
|
-
params = self._build_parameters()
|
|
509
|
-
if len(params):
|
|
510
|
-
print("Query Parameters:")
|
|
511
|
-
for k, v in params.items():
|
|
512
|
-
print(" ", k, "=", v)
|
|
513
|
-
if self.is_function:
|
|
514
|
-
names = [
|
|
515
|
-
f"{p.name}<{p.ftype}>"
|
|
516
|
-
for p in self.entity.entity_set.entity.properties.values()
|
|
517
|
-
]
|
|
518
|
-
names = ", ".join(names)
|
|
519
|
-
print("Return:", names)
|
|
520
|
-
else:
|
|
521
|
-
names = [
|
|
522
|
-
f"{p.name}<{p.ftype}>" for p in self.entity.entity.properties.values()
|
|
523
|
-
]
|
|
524
|
-
names = ", ".join(names)
|
|
525
|
-
print("Return:", names)
|
|
1
|
+
from io import BytesIO
|
|
2
|
+
import logging
|
|
3
|
+
from lxml import etree
|
|
4
|
+
import json
|
|
5
|
+
import httpx
|
|
6
|
+
from urllib.parse import quote
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
# Edm.Boolean
|
|
10
|
+
# Edm.Byte
|
|
11
|
+
# Edm.Date
|
|
12
|
+
# Edm.DateTimeOffset
|
|
13
|
+
# Edm.Decimal
|
|
14
|
+
# Edm.Duration
|
|
15
|
+
# Edm.Guid
|
|
16
|
+
# Edm.Int16
|
|
17
|
+
# Edm.Int32
|
|
18
|
+
# Edm.Int64
|
|
19
|
+
# Edm.SByte
|
|
20
|
+
# Edm.String
|
|
21
|
+
# Edm.TimeOfDay
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def str_types(type):
|
|
25
|
+
if type == "Edm.Decimal":
|
|
26
|
+
return "float"
|
|
27
|
+
elif type in ("Edm.Int32", "Edm.Int64", "Edm.Int16"):
|
|
28
|
+
return "int"
|
|
29
|
+
elif type == "Edm.String":
|
|
30
|
+
return "str"
|
|
31
|
+
elif type == "Edm.Boolean":
|
|
32
|
+
return "bool"
|
|
33
|
+
elif type in ("Edm.Date", "Edm.TimeOfDay"):
|
|
34
|
+
return "datetime"
|
|
35
|
+
else:
|
|
36
|
+
return type
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class ODataEndPoint:
|
|
40
|
+
def __init__(self, **kwargs):
|
|
41
|
+
self.data = kwargs
|
|
42
|
+
|
|
43
|
+
def __getitem__(self, item):
|
|
44
|
+
return self.data[item]
|
|
45
|
+
|
|
46
|
+
def __setitem__(self, key, value):
|
|
47
|
+
self.data[key] = value
|
|
48
|
+
|
|
49
|
+
def __repr__(self):
|
|
50
|
+
url = self.data["url"]
|
|
51
|
+
return f"<EndPoint {url}>"
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class ODataEntitySetMeta(type):
|
|
55
|
+
def __init__(self, *args, **kwargs):
|
|
56
|
+
super().__init__(*args, **kwargs)
|
|
57
|
+
|
|
58
|
+
def __call__(self, *args):
|
|
59
|
+
obj = super().__call__(*args)
|
|
60
|
+
entity = args[2]
|
|
61
|
+
for name, prop in entity.properties.items():
|
|
62
|
+
setattr(obj, name, prop)
|
|
63
|
+
return obj
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class ODataEntitySet(metaclass=ODataEntitySetMeta):
|
|
67
|
+
def __init__(self, name, entity_type, entity):
|
|
68
|
+
self.name = name
|
|
69
|
+
self.entity_type = entity_type
|
|
70
|
+
self.entity = entity
|
|
71
|
+
|
|
72
|
+
def describe(self):
|
|
73
|
+
props = ", ".join(
|
|
74
|
+
[f"{prop.name}<{prop.ftype}>" for prop in self.entity.properties.values()]
|
|
75
|
+
)
|
|
76
|
+
print(
|
|
77
|
+
f"""
|
|
78
|
+
EntitySet (Endpoint): {self.name}
|
|
79
|
+
EntityType: {self.entity_type}
|
|
80
|
+
Properties: {props}"""
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
def __repr__(self):
|
|
84
|
+
return f"<EntitySet {self.name}>"
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
class ODataFunctionImport:
|
|
88
|
+
def __init__(self, name, function, entity_set):
|
|
89
|
+
self.name = name
|
|
90
|
+
self.entity_set = entity_set
|
|
91
|
+
self.function = function
|
|
92
|
+
|
|
93
|
+
def describe(self):
|
|
94
|
+
props = ", ".join(
|
|
95
|
+
[
|
|
96
|
+
f"{prop.name} <{prop.ftype}>"
|
|
97
|
+
for prop in self.entity_set.entity.properties.values()
|
|
98
|
+
]
|
|
99
|
+
)
|
|
100
|
+
params = ", ".join(
|
|
101
|
+
[f"{param.name} <{param.ftype}>" for param in self.function.parameters]
|
|
102
|
+
)
|
|
103
|
+
s = f"""
|
|
104
|
+
Function: {self.function.name}
|
|
105
|
+
Parameters: {params}
|
|
106
|
+
EntitySet: {self.entity_set.name}
|
|
107
|
+
EntityType: {self.entity_set.entity_type}
|
|
108
|
+
Properties: {props}"""
|
|
109
|
+
print(s)
|
|
110
|
+
|
|
111
|
+
def __repr__(self):
|
|
112
|
+
return f"<FunctionImport {self.name}>"
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
class ODataParameter:
|
|
116
|
+
def __init__(self, **kwargs):
|
|
117
|
+
self.data = kwargs
|
|
118
|
+
|
|
119
|
+
@property
|
|
120
|
+
def name(self):
|
|
121
|
+
return self.data.get("Name")
|
|
122
|
+
|
|
123
|
+
@property
|
|
124
|
+
def type(self):
|
|
125
|
+
return self.data.get("Type")
|
|
126
|
+
|
|
127
|
+
@property
|
|
128
|
+
def ftype(self):
|
|
129
|
+
return str_types(self.type)
|
|
130
|
+
|
|
131
|
+
@property
|
|
132
|
+
def required(self):
|
|
133
|
+
nullable = self.data.get("Nullable")
|
|
134
|
+
if nullable:
|
|
135
|
+
return nullable == "false"
|
|
136
|
+
else:
|
|
137
|
+
return True
|
|
138
|
+
|
|
139
|
+
def format(self, value):
|
|
140
|
+
if self.type == "Edm.Decimal":
|
|
141
|
+
return f"{float(value)}"
|
|
142
|
+
elif self.type == "Edm.Int32":
|
|
143
|
+
return f"{int(value)}"
|
|
144
|
+
elif self.type == "Edm.String":
|
|
145
|
+
return f"'{str(value)}'"
|
|
146
|
+
else:
|
|
147
|
+
return f"'{str(value)}'"
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
class ODataPropertyOrderBy:
|
|
151
|
+
def __init__(self, obj, order):
|
|
152
|
+
self.obj = obj
|
|
153
|
+
self.order = order
|
|
154
|
+
|
|
155
|
+
def __str__(self):
|
|
156
|
+
return f"{self.obj.name} {self.order}"
|
|
157
|
+
|
|
158
|
+
def __repr__(self):
|
|
159
|
+
return f"<{str(self)}>"
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
class ODataPropertyFilter:
|
|
163
|
+
def __init__(self, obj, oth, operator):
|
|
164
|
+
self.obj = obj
|
|
165
|
+
self.other = oth
|
|
166
|
+
self.operator = operator
|
|
167
|
+
|
|
168
|
+
def statement(self):
|
|
169
|
+
if self.obj.type == "Edm.Decimal":
|
|
170
|
+
return f"{self.obj.name} {self.operator} {float(self.other)}"
|
|
171
|
+
elif self.obj.type == "Edm.Int32":
|
|
172
|
+
return f"{self.obj.name} {self.operator} {int(self.other)}"
|
|
173
|
+
elif self.obj.type == "Edm.String":
|
|
174
|
+
return f"{self.obj.name} {self.operator} '{str(self.other)}'"
|
|
175
|
+
elif self.obj.type == "Edm.Date":
|
|
176
|
+
return f"{self.obj.name} {self.operator} {self.other.strftime('%Y-%m-%d')}"
|
|
177
|
+
else:
|
|
178
|
+
return f"{self.obj.name} {self.operator} '{self.other}'"
|
|
179
|
+
|
|
180
|
+
def __str__(self):
|
|
181
|
+
return self.statement()
|
|
182
|
+
|
|
183
|
+
def __repr__(self):
|
|
184
|
+
return f"<filter: {str(self)}>"
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
class ODataProperty:
|
|
188
|
+
def __init__(self, **kwargs):
|
|
189
|
+
self.data = kwargs
|
|
190
|
+
|
|
191
|
+
@property
|
|
192
|
+
def name(self):
|
|
193
|
+
return self.data.get("Name")
|
|
194
|
+
|
|
195
|
+
@property
|
|
196
|
+
def type(self):
|
|
197
|
+
return self.data.get("Type")
|
|
198
|
+
|
|
199
|
+
@property
|
|
200
|
+
def ftype(self):
|
|
201
|
+
return str_types(self.type)
|
|
202
|
+
|
|
203
|
+
def asc(self):
|
|
204
|
+
return ODataPropertyOrderBy(self, "asc")
|
|
205
|
+
|
|
206
|
+
def desc(self):
|
|
207
|
+
return ODataPropertyOrderBy(self, "desc")
|
|
208
|
+
|
|
209
|
+
def __gt__(self, other):
|
|
210
|
+
return ODataPropertyFilter(self, other, "gt")
|
|
211
|
+
|
|
212
|
+
def __ge__(self, other):
|
|
213
|
+
return ODataPropertyFilter(self, other, "ge")
|
|
214
|
+
|
|
215
|
+
def __lt__(self, other):
|
|
216
|
+
return ODataPropertyFilter(self, other, "lt")
|
|
217
|
+
|
|
218
|
+
def __le__(self, other):
|
|
219
|
+
return ODataPropertyFilter(self, other, "le")
|
|
220
|
+
|
|
221
|
+
def __eq__(self, other):
|
|
222
|
+
return ODataPropertyFilter(self, other, "eq")
|
|
223
|
+
|
|
224
|
+
def __repr__(self):
|
|
225
|
+
return f"<Property {self.name}<{self.ftype}>>"
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
class ODataFunction:
|
|
229
|
+
def __init__(self, **kwargs):
|
|
230
|
+
self.name = kwargs["Name"]
|
|
231
|
+
self.parameters = kwargs["parameters"]
|
|
232
|
+
self.return_type = kwargs["return_type"]
|
|
233
|
+
self.fullname = f'{kwargs["namespace"]}.{self.name}'
|
|
234
|
+
|
|
235
|
+
def __repr__(self):
|
|
236
|
+
return f"<Function {self.name}>"
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
class ODataEntity:
|
|
240
|
+
def __init__(self, name, properties, namespace):
|
|
241
|
+
self.name = name
|
|
242
|
+
self.properties = properties
|
|
243
|
+
self.fullname = f"{namespace}.{name}"
|
|
244
|
+
|
|
245
|
+
def __repr__(self):
|
|
246
|
+
return f"<Entity {self.name}>"
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
class ODataMetadata:
|
|
250
|
+
namespaces = {
|
|
251
|
+
"edm": "http://docs.oasis-open.org/odata/ns/edm",
|
|
252
|
+
"edmx": "http://docs.oasis-open.org/odata/ns/edmx",
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
def __init__(self, url):
|
|
256
|
+
self.url = url
|
|
257
|
+
self._load_document()
|
|
258
|
+
_xpath = "edmx:DataServices/edm:Schema"
|
|
259
|
+
schema = self.doc.xpath(_xpath, namespaces=self.namespaces)[0]
|
|
260
|
+
self.namespace = schema.attrib["Namespace"]
|
|
261
|
+
self._used_elements = []
|
|
262
|
+
self._parse_entities(schema)
|
|
263
|
+
self._parse_entity_sets(schema)
|
|
264
|
+
self._parse_functions(schema)
|
|
265
|
+
self._parse_function_imports(schema)
|
|
266
|
+
|
|
267
|
+
def _load_document(self):
|
|
268
|
+
res = httpx.get(self.url, timeout=60.0)
|
|
269
|
+
self.doc = etree.parse(BytesIO(res.content))
|
|
270
|
+
|
|
271
|
+
def _parse_entity(self, entity_element, namespace):
|
|
272
|
+
name = entity_element.attrib["Name"]
|
|
273
|
+
props = {
|
|
274
|
+
prop.attrib["Name"]: ODataProperty(**prop.attrib) for prop in entity_element
|
|
275
|
+
}
|
|
276
|
+
return ODataEntity(name, props, namespace)
|
|
277
|
+
|
|
278
|
+
def _parse_entities(self, schema):
|
|
279
|
+
_xpath = "edm:EntityType"
|
|
280
|
+
self.entities = [
|
|
281
|
+
self._parse_entity(e, self.namespace)
|
|
282
|
+
for e in schema.xpath(_xpath, namespaces=self.namespaces)
|
|
283
|
+
]
|
|
284
|
+
|
|
285
|
+
self._entities_fullnames = {e.fullname: e for e in self.entities}
|
|
286
|
+
|
|
287
|
+
def _parse_entity_sets(self, schema):
|
|
288
|
+
_xpath = "edm:EntityContainer/edm:EntitySet"
|
|
289
|
+
|
|
290
|
+
def _parse_entity_set(e):
|
|
291
|
+
entity = self._entities_fullnames[e.attrib["EntityType"]]
|
|
292
|
+
return ODataEntitySet(e.attrib["Name"], e.attrib["EntityType"], entity)
|
|
293
|
+
|
|
294
|
+
self.entity_sets = {
|
|
295
|
+
e.attrib["Name"]: _parse_entity_set(e)
|
|
296
|
+
for e in schema.xpath(_xpath, namespaces=self.namespaces)
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
for n, es in self.entity_sets.items():
|
|
300
|
+
es.fullname = f"{self.namespace}.{n}"
|
|
301
|
+
|
|
302
|
+
self._entity_sets_fullnames = {
|
|
303
|
+
es.fullname: es for es in self.entity_sets.values()
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
def _parse_functions(self, schema):
|
|
307
|
+
_xpath = "edm:Function"
|
|
308
|
+
|
|
309
|
+
def _parse_function(e):
|
|
310
|
+
parameters = []
|
|
311
|
+
return_type = None
|
|
312
|
+
for element in e:
|
|
313
|
+
if element.tag.endswith("Parameter"):
|
|
314
|
+
parameters.append(ODataParameter(**element.attrib))
|
|
315
|
+
elif element.tag.endswith("ReturnType"):
|
|
316
|
+
return_type = element.attrib["Type"]
|
|
317
|
+
kwargs = dict(**e.attrib)
|
|
318
|
+
kwargs["parameters"] = parameters
|
|
319
|
+
kwargs["return_type"] = return_type
|
|
320
|
+
kwargs["namespace"] = self.namespace
|
|
321
|
+
return ODataFunction(**kwargs)
|
|
322
|
+
|
|
323
|
+
self.functions = {
|
|
324
|
+
e.attrib["Name"]: _parse_function(e)
|
|
325
|
+
for e in schema.xpath(_xpath, namespaces=self.namespaces)
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
self._functions_fullnames = {f.fullname: f for f in self.functions.values()}
|
|
329
|
+
|
|
330
|
+
def _parse_function_imports(self, schema):
|
|
331
|
+
_xpath = "edm:EntityContainer/edm:FunctionImport"
|
|
332
|
+
|
|
333
|
+
def _parse_function_import(e):
|
|
334
|
+
function = self._functions_fullnames[e.attrib["Function"]]
|
|
335
|
+
entity_set = self._entity_sets_fullnames[e.attrib["EntitySet"]]
|
|
336
|
+
self._used_elements.append(e.attrib["EntitySet"])
|
|
337
|
+
return ODataFunctionImport(e.attrib["Name"], function, entity_set)
|
|
338
|
+
|
|
339
|
+
self.function_imports = {
|
|
340
|
+
e.attrib["Name"]: _parse_function_import(e)
|
|
341
|
+
for e in schema.xpath(_xpath, namespaces=self.namespaces)
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
class ODataService:
|
|
346
|
+
def __init__(self, url):
|
|
347
|
+
self.url = url
|
|
348
|
+
res = httpx.get(self.url, timeout=60.0)
|
|
349
|
+
self.api_data = json.loads(res.text)
|
|
350
|
+
self.endpoints = [ODataEndPoint(**x) for x in self.api_data["value"]]
|
|
351
|
+
self._odata_context_url = self.api_data["@odata.context"]
|
|
352
|
+
self.metadata = ODataMetadata(self._odata_context_url)
|
|
353
|
+
|
|
354
|
+
def __getitem__(self, item):
|
|
355
|
+
es = self.entity_sets.get(item)
|
|
356
|
+
if es is None:
|
|
357
|
+
fi = self.function_imports.get(item)
|
|
358
|
+
if fi is not None:
|
|
359
|
+
return fi
|
|
360
|
+
else:
|
|
361
|
+
raise ValueError("Invalid name: " + item)
|
|
362
|
+
else:
|
|
363
|
+
return es
|
|
364
|
+
|
|
365
|
+
@property
|
|
366
|
+
def entities(self):
|
|
367
|
+
return self.metadata.entities
|
|
368
|
+
|
|
369
|
+
@property
|
|
370
|
+
def function_imports(self):
|
|
371
|
+
return self.metadata.function_imports
|
|
372
|
+
|
|
373
|
+
@property
|
|
374
|
+
def entity_sets(self):
|
|
375
|
+
return self.metadata.entity_sets
|
|
376
|
+
|
|
377
|
+
def describe(self):
|
|
378
|
+
es_names = []
|
|
379
|
+
for es in self.entity_sets.keys():
|
|
380
|
+
k = f"{self.metadata.namespace}.{es}"
|
|
381
|
+
if k not in self.metadata._used_elements:
|
|
382
|
+
es_names.append(es)
|
|
383
|
+
if len(es_names):
|
|
384
|
+
print("EntitySets:")
|
|
385
|
+
for es in es_names:
|
|
386
|
+
print(" ", es)
|
|
387
|
+
if len(self.function_imports):
|
|
388
|
+
print("FunctionImports:")
|
|
389
|
+
for es in self.function_imports.keys():
|
|
390
|
+
print(" ", es)
|
|
391
|
+
|
|
392
|
+
def query(self, entity_set):
|
|
393
|
+
return ODataQuery(entity_set, self.url)
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
class ODataQuery:
|
|
397
|
+
def __init__(self, entity, url):
|
|
398
|
+
self.entity = entity
|
|
399
|
+
self.base_url = url
|
|
400
|
+
self._params = {}
|
|
401
|
+
self.function_parameters = {}
|
|
402
|
+
self._filter = []
|
|
403
|
+
self._select = []
|
|
404
|
+
self._orderby = []
|
|
405
|
+
self._raw = False
|
|
406
|
+
self.is_function = isinstance(entity, ODataFunctionImport)
|
|
407
|
+
if self.is_function:
|
|
408
|
+
self.function_parameters = {
|
|
409
|
+
p.name: None for p in self.entity.function.parameters
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
def odata_url(self):
|
|
413
|
+
self._url = (
|
|
414
|
+
f"{self.base_url}{self.entity.name}"
|
|
415
|
+
if self.base_url.endswith("/")
|
|
416
|
+
else f"{self.base_url}/{self.entity.name}"
|
|
417
|
+
)
|
|
418
|
+
if self.is_function:
|
|
419
|
+
args = [f"{p.name}=@{p.name}" for p in self.entity.function.parameters]
|
|
420
|
+
args = ",".join(args)
|
|
421
|
+
return f"{self._url}({args})"
|
|
422
|
+
else:
|
|
423
|
+
return self._url
|
|
424
|
+
|
|
425
|
+
def parameters(self, **kwargs):
|
|
426
|
+
for arg in kwargs:
|
|
427
|
+
if arg in self.function_parameters:
|
|
428
|
+
self.function_parameters[arg] = kwargs[arg]
|
|
429
|
+
else:
|
|
430
|
+
raise ValueError(f"Unknown parameter: {arg}")
|
|
431
|
+
return self
|
|
432
|
+
|
|
433
|
+
def filter(self, *args):
|
|
434
|
+
if len(args):
|
|
435
|
+
self._filter.extend(args)
|
|
436
|
+
return self
|
|
437
|
+
|
|
438
|
+
def limit(self, limit):
|
|
439
|
+
self._params["$top"] = limit
|
|
440
|
+
return self
|
|
441
|
+
|
|
442
|
+
def skip(self, skip):
|
|
443
|
+
self._params["$skip"] = skip
|
|
444
|
+
return self
|
|
445
|
+
|
|
446
|
+
def format(self, fmt):
|
|
447
|
+
self._params["$format"] = fmt
|
|
448
|
+
return self
|
|
449
|
+
|
|
450
|
+
def orderby(self, *args):
|
|
451
|
+
if len(args):
|
|
452
|
+
self._orderby.extend(args)
|
|
453
|
+
return self
|
|
454
|
+
|
|
455
|
+
def select(self, *args):
|
|
456
|
+
if len(args):
|
|
457
|
+
self._select.extend(args)
|
|
458
|
+
return self
|
|
459
|
+
|
|
460
|
+
def raw(self):
|
|
461
|
+
self._raw = True
|
|
462
|
+
return self
|
|
463
|
+
|
|
464
|
+
def _build_parameters(self):
|
|
465
|
+
params = {"$format": self._params.get("$format", "json")}
|
|
466
|
+
if len(self._filter):
|
|
467
|
+
_filter = " and ".join(str(f) for f in self._filter)
|
|
468
|
+
params["$filter"] = _filter
|
|
469
|
+
if len(self._orderby):
|
|
470
|
+
_orderby = ",".join(str(f) for f in self._orderby)
|
|
471
|
+
params["$orderby"] = _orderby
|
|
472
|
+
if len(self._select):
|
|
473
|
+
_select = ",".join(str(f.name) for f in self._select)
|
|
474
|
+
params["$select"] = _select
|
|
475
|
+
params.update(self._params)
|
|
476
|
+
return params
|
|
477
|
+
|
|
478
|
+
def reset(self):
|
|
479
|
+
self._filter = []
|
|
480
|
+
self._orderby = []
|
|
481
|
+
self._params = {}
|
|
482
|
+
|
|
483
|
+
def collect(self):
|
|
484
|
+
return json.loads(self.text())
|
|
485
|
+
|
|
486
|
+
def text(self):
|
|
487
|
+
params = self._build_parameters()
|
|
488
|
+
if self.is_function and len(self.function_parameters):
|
|
489
|
+
for p in self.entity.function.parameters:
|
|
490
|
+
val = self.function_parameters[p.name]
|
|
491
|
+
if p.required and val is None:
|
|
492
|
+
raise ValueError("Parameter not set: " + p.name)
|
|
493
|
+
params["@" + p.name] = p.format(val)
|
|
494
|
+
qs = "&".join([f"{quote(k)}={quote(str(v))}" for k, v in params.items()])
|
|
495
|
+
headers = {"OData-Version": "4.0", "OData-MaxVersion": "4.0"}
|
|
496
|
+
res = httpx.get(self.odata_url() + "?" + qs, headers=headers, timeout=60.0)
|
|
497
|
+
return res.text
|
|
498
|
+
|
|
499
|
+
def show(self):
|
|
500
|
+
print(f"URL:")
|
|
501
|
+
print(f" {self.odata_url()}")
|
|
502
|
+
if self.is_function and len(self.function_parameters):
|
|
503
|
+
print("Function Parameters:")
|
|
504
|
+
for p in self.entity.function.parameters:
|
|
505
|
+
v = self.function_parameters[p.name]
|
|
506
|
+
req = " (required) " if p.required else " "
|
|
507
|
+
print(f" {p.name}<{p.ftype}>{req}= {v}")
|
|
508
|
+
params = self._build_parameters()
|
|
509
|
+
if len(params):
|
|
510
|
+
print("Query Parameters:")
|
|
511
|
+
for k, v in params.items():
|
|
512
|
+
print(" ", k, "=", v)
|
|
513
|
+
if self.is_function:
|
|
514
|
+
names = [
|
|
515
|
+
f"{p.name}<{p.ftype}>"
|
|
516
|
+
for p in self.entity.entity_set.entity.properties.values()
|
|
517
|
+
]
|
|
518
|
+
names = ", ".join(names)
|
|
519
|
+
print("Return:", names)
|
|
520
|
+
else:
|
|
521
|
+
names = [
|
|
522
|
+
f"{p.name}<{p.ftype}>" for p in self.entity.entity.properties.values()
|
|
523
|
+
]
|
|
524
|
+
names = ", ".join(names)
|
|
525
|
+
print("Return:", names)
|