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