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