maleo-metadata-client 0.0.3__py3-none-any.whl → 0.0.5__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.

Potentially problematic release.


This version of maleo-metadata-client might be problematic. Click here for more details.

@@ -1,374 +0,0 @@
1
- import json
2
- from copy import deepcopy
3
- from Crypto.PublicKey.RSA import RsaKey
4
- from datetime import datetime, timezone
5
- from redis.asyncio.client import Redis
6
- from typing import Dict, List, Literal, Optional, Union, overload
7
- from uuid import UUID
8
- from maleo.soma.authorization import BearerAuth
9
- from maleo.soma.dtos.configurations.cache.redis import RedisCacheNamespaces
10
- from maleo.soma.enums.cardinality import Cardinality
11
- from maleo.soma.enums.environment import Environment
12
- from maleo.soma.enums.expiration import Expiration
13
- from maleo.soma.enums.logging import LogLevel
14
- from maleo.soma.enums.operation import OperationTarget
15
- from maleo.soma.managers.client.maleo import MaleoClientService
16
- from maleo.soma.managers.client.http import HTTPClientManager
17
- from maleo.soma.managers.credential import CredentialManager
18
- from maleo.soma.schemas.authentication import PrivilegedAuthentication
19
- from maleo.soma.schemas.data import DataPair
20
- from maleo.soma.schemas.operation.context import (
21
- OperationContextSchema,
22
- OperationOriginSchema,
23
- OperationLayerSchema,
24
- OperationTargetSchema,
25
- )
26
- from maleo.soma.schemas.operation.resource import (
27
- ReadSingleResourceOperationSchema,
28
- ReadMultipleResourceOperationSchema,
29
- )
30
- from maleo.soma.schemas.operation.resource.action import ReadResourceOperationAction
31
- from maleo.soma.schemas.operation.resource.result import (
32
- ReadSingleResourceOperationResult,
33
- ReadMultipleResourceOperationResult,
34
- )
35
- from maleo.soma.schemas.operation.timestamp import OperationTimestamp
36
- from maleo.soma.schemas.pagination import StrictPagination
37
- from maleo.soma.schemas.parameter.general import ReadSingleQueryParameterSchema
38
- from maleo.soma.schemas.request import RequestContext
39
- from maleo.soma.schemas.response import (
40
- SingleDataResponseSchema,
41
- MultipleDataResponseSchema,
42
- )
43
- from maleo.soma.schemas.service import ServiceContext
44
- from maleo.soma.utils.cache import build_key
45
- from maleo.soma.utils.logging import ClientLogger
46
- from maleo.soma.utils.merger import merge_dicts
47
- from maleo.soma.utils.token import reencode
48
- from maleo.metadata.constants.service import RESOURCE
49
- from maleo.metadata.schemas.data.service import ServiceDataSchema
50
- from maleo.metadata.schemas.parameter.client.service import (
51
- ReadMultipleParameter,
52
- ReadMultipleQueryParameter,
53
- )
54
- from maleo.metadata.schemas.parameter.general.service import ReadSingleParameter
55
-
56
-
57
- class ServiceClientService(MaleoClientService):
58
- def __init__(
59
- self,
60
- environment: Environment,
61
- key: str,
62
- url: str,
63
- operation_origin: OperationOriginSchema,
64
- logger: ClientLogger,
65
- credential_manager: CredentialManager,
66
- http_client_manager: HTTPClientManager,
67
- private_key: RsaKey,
68
- redis: Redis,
69
- redis_namespaces: RedisCacheNamespaces,
70
- service_context: ServiceContext,
71
- ):
72
- super().__init__(
73
- environment,
74
- key,
75
- url,
76
- operation_origin,
77
- logger,
78
- credential_manager,
79
- http_client_manager,
80
- private_key,
81
- redis,
82
- redis_namespaces,
83
- service_context,
84
- )
85
- self._namespace = self._redis_namespaces.create(
86
- self._key,
87
- RESOURCE.aggregate(),
88
- origin=self._CACHE_ORIGIN,
89
- layer=self._CACHE_LAYER,
90
- )
91
- self._default_operation_context = OperationContextSchema(
92
- origin=self._operation_origin,
93
- layer=OperationLayerSchema(type=self._OPERATION_LAYER_TYPE, details=None),
94
- target=OperationTargetSchema(
95
- type=self._OPERATION_TARGET_TYPE, details=None
96
- ),
97
- )
98
-
99
- @overload
100
- async def read(
101
- self,
102
- cardinality: Literal[Cardinality.MULTIPLE],
103
- *,
104
- operation_id: UUID,
105
- request_context: RequestContext,
106
- authentication: PrivilegedAuthentication,
107
- parameters: ReadMultipleParameter,
108
- headers: Optional[Dict[str, str]] = None,
109
- ) -> ReadMultipleResourceOperationResult[
110
- ServiceDataSchema, StrictPagination, None
111
- ]: ...
112
- @overload
113
- async def read(
114
- self,
115
- cardinality: Literal[Cardinality.SINGLE],
116
- *,
117
- operation_id: UUID,
118
- request_context: RequestContext,
119
- authentication: PrivilegedAuthentication,
120
- parameters: ReadSingleParameter,
121
- headers: Optional[Dict[str, str]] = None,
122
- ) -> ReadSingleResourceOperationResult[ServiceDataSchema, None]: ...
123
- async def read(
124
- self,
125
- cardinality: Cardinality,
126
- *,
127
- operation_id: UUID,
128
- request_context: RequestContext,
129
- authentication: PrivilegedAuthentication,
130
- parameters: Union[ReadMultipleParameter, ReadSingleParameter],
131
- headers: Optional[Dict[str, str]] = None,
132
- ) -> Union[
133
- ReadMultipleResourceOperationResult[ServiceDataSchema, StrictPagination, None],
134
- ReadSingleResourceOperationResult[ServiceDataSchema, None],
135
- ]:
136
- operation_action = ReadResourceOperationAction()
137
- executed_at = datetime.now(tz=timezone.utc)
138
-
139
- # Get function identifier
140
- func = self.__class__
141
- module, qualname = func.__module__, func.__qualname__
142
-
143
- # Define arguments being used in this function
144
- positional_arguments = [cardinality]
145
- keyword_arguments = {
146
- "authentication": authentication.model_dump(
147
- mode="json",
148
- exclude={
149
- "credentials": {
150
- "token": {
151
- "payload": {
152
- "iat_dt",
153
- "iat",
154
- "exp_dt",
155
- "exp",
156
- }
157
- }
158
- }
159
- },
160
- ),
161
- "parameters": parameters.model_dump(mode="json"),
162
- }
163
-
164
- # Define full function string
165
- function = f"{qualname}({json.dumps(positional_arguments)}|{json.dumps(keyword_arguments)})"
166
-
167
- # Define full cache key
168
- cache_key = build_key(module, function, namespace=self._namespace)
169
-
170
- if parameters.use_cache:
171
- operation_context = deepcopy(self._default_operation_context)
172
- operation_context.target.type = OperationTarget.CACHE
173
-
174
- # Check redis for data
175
- result_str = await self._redis.get(cache_key)
176
-
177
- if result_str is not None:
178
- completed_at = datetime.now(tz=timezone.utc)
179
-
180
- if isinstance(parameters, ReadMultipleParameter):
181
- result = ReadMultipleResourceOperationResult[
182
- ServiceDataSchema, StrictPagination, None
183
- ].model_validate(json.loads(result_str))
184
- ReadMultipleResourceOperationSchema[
185
- PrivilegedAuthentication,
186
- ServiceDataSchema,
187
- StrictPagination,
188
- None,
189
- ](
190
- service_context=self._service_context,
191
- id=operation_id,
192
- context=operation_context,
193
- timestamp=OperationTimestamp(
194
- executed_at=executed_at,
195
- completed_at=completed_at,
196
- duration=(completed_at - executed_at).total_seconds(),
197
- ),
198
- summary="Successfully retrieved multiple services from cache",
199
- request_context=request_context,
200
- authentication=authentication,
201
- action=operation_action,
202
- resource=RESOURCE,
203
- result=result,
204
- ).log(
205
- self._logger, LogLevel.INFO
206
- )
207
- elif isinstance(parameters, ReadSingleParameter):
208
- result = ReadSingleResourceOperationResult[
209
- ServiceDataSchema, None
210
- ].model_validate(json.loads(result_str))
211
- ReadSingleResourceOperationSchema[
212
- PrivilegedAuthentication, ServiceDataSchema, None
213
- ](
214
- service_context=self._service_context,
215
- id=operation_id,
216
- context=operation_context,
217
- timestamp=OperationTimestamp(
218
- executed_at=executed_at,
219
- completed_at=completed_at,
220
- duration=(completed_at - executed_at).total_seconds(),
221
- ),
222
- summary="Successfully retrieved single service from cache",
223
- request_context=request_context,
224
- authentication=authentication,
225
- action=operation_action,
226
- resource=RESOURCE,
227
- result=result,
228
- ).log(
229
- self._logger, LogLevel.INFO
230
- )
231
- return result
232
-
233
- operation_context = deepcopy(self._default_operation_context)
234
- async with self._http_client_manager.get() as http_client:
235
- # Create headers
236
- base_headers = {
237
- "content-type": "application/json",
238
- "x-operation-id": str(operation_id),
239
- }
240
- if headers is not None:
241
- headers = merge_dicts(base_headers, headers)
242
- else:
243
- headers = base_headers
244
-
245
- # Create auth
246
- token = None
247
- try:
248
- token = reencode(
249
- payload=authentication.credentials.token.payload,
250
- key=self._private_key,
251
- )
252
- except Exception:
253
- pass
254
-
255
- auth = BearerAuth(token) if token is not None else None
256
-
257
- if isinstance(parameters, ReadMultipleParameter):
258
- # Define URL
259
- url = f"{self._url}/v1/{RESOURCE.identifiers[0].url_slug}/"
260
-
261
- # Parse parameters to query params
262
- params = ReadMultipleQueryParameter.model_validate(
263
- parameters.model_dump()
264
- ).model_dump(
265
- exclude={"sort_columns", "date_filters"}, exclude_none=True
266
- )
267
- elif isinstance(parameters, ReadSingleParameter):
268
- # Define URL
269
- url = f"{self._url}/v1/{RESOURCE.identifiers[0].url_slug}/{parameters.identifier}/{parameters.value}"
270
-
271
- # Parse parameters to query params
272
- params = ReadSingleQueryParameterSchema.model_validate(
273
- parameters.model_dump()
274
- ).model_dump(exclude_none=True)
275
-
276
- # Send request and wait for response
277
- response = await http_client.get(
278
- url=url, params=params, headers=headers, auth=auth
279
- )
280
-
281
- if response.is_success:
282
- completed_at = datetime.now(tz=timezone.utc)
283
-
284
- if isinstance(parameters, ReadMultipleParameter):
285
- validated_response = MultipleDataResponseSchema[
286
- ServiceDataSchema, StrictPagination, None
287
- ].model_validate(response.json())
288
- data = DataPair[List[ServiceDataSchema], None](
289
- old=validated_response.data,
290
- new=None,
291
- )
292
- result = ReadMultipleResourceOperationResult[
293
- ServiceDataSchema, StrictPagination, None
294
- ](
295
- data=data,
296
- pagination=validated_response.pagination,
297
- metadata=None,
298
- other=None,
299
- )
300
- ReadMultipleResourceOperationSchema[
301
- PrivilegedAuthentication,
302
- ServiceDataSchema,
303
- StrictPagination,
304
- None,
305
- ](
306
- service_context=self._service_context,
307
- id=operation_id,
308
- context=operation_context,
309
- timestamp=OperationTimestamp(
310
- executed_at=executed_at,
311
- completed_at=completed_at,
312
- duration=(completed_at - executed_at).total_seconds(),
313
- ),
314
- summary="Successfully retrieved multiple services from http request",
315
- request_context=request_context,
316
- authentication=authentication,
317
- action=operation_action,
318
- resource=RESOURCE,
319
- result=result,
320
- ).log(
321
- self._logger, level=LogLevel.INFO
322
- )
323
- elif isinstance(parameters, ReadSingleParameter):
324
- validated_response = SingleDataResponseSchema[
325
- ServiceDataSchema, None
326
- ].model_validate(response.json())
327
- data = DataPair[ServiceDataSchema, None](
328
- old=validated_response.data,
329
- new=None,
330
- )
331
- result = ReadSingleResourceOperationResult[ServiceDataSchema, None](
332
- data=data,
333
- pagination=validated_response.pagination,
334
- metadata=None,
335
- other=None,
336
- )
337
- ReadSingleResourceOperationSchema[
338
- PrivilegedAuthentication, ServiceDataSchema, None
339
- ](
340
- service_context=self._service_context,
341
- id=operation_id,
342
- context=operation_context,
343
- timestamp=OperationTimestamp(
344
- executed_at=executed_at,
345
- completed_at=completed_at,
346
- duration=(completed_at - executed_at).total_seconds(),
347
- ),
348
- summary="Successfully retrieved single service from http request",
349
- request_context=request_context,
350
- authentication=authentication,
351
- action=operation_action,
352
- resource=RESOURCE,
353
- result=result,
354
- ).log(
355
- self._logger, level=LogLevel.INFO
356
- )
357
-
358
- if parameters.use_cache:
359
- await self._redis.set(
360
- cache_key, result.model_dump_json(), Expiration.EXP_1MO.value
361
- )
362
-
363
- return result
364
-
365
- self._raise_resource_http_request_error(
366
- response=response,
367
- operation_id=operation_id,
368
- operation_context=operation_context,
369
- executed_at=executed_at,
370
- operation_action=operation_action,
371
- request_context=request_context,
372
- authentication=authentication,
373
- resource=RESOURCE,
374
- )