maleo-metadata-client 0.0.1__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.

@@ -0,0 +1,378 @@
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 GeneralAuthentication
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 FlexiblePagination
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.system_role import RESOURCE
49
+ from maleo.metadata.schemas.data.system_role import SystemRoleDataSchema
50
+ from maleo.metadata.schemas.parameter.client.system_role import (
51
+ ReadMultipleParameter,
52
+ ReadMultipleQueryParameter,
53
+ )
54
+ from maleo.metadata.schemas.parameter.general.system_role import ReadSingleParameter
55
+
56
+
57
+ class SystemRoleClientService(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: GeneralAuthentication,
107
+ parameters: ReadMultipleParameter,
108
+ headers: Optional[Dict[str, str]] = None,
109
+ ) -> ReadMultipleResourceOperationResult[
110
+ SystemRoleDataSchema, FlexiblePagination, 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: GeneralAuthentication,
120
+ parameters: ReadSingleParameter,
121
+ headers: Optional[Dict[str, str]] = None,
122
+ ) -> ReadSingleResourceOperationResult[SystemRoleDataSchema, None]: ...
123
+ async def read(
124
+ self,
125
+ cardinality: Cardinality,
126
+ *,
127
+ operation_id: UUID,
128
+ request_context: RequestContext,
129
+ authentication: GeneralAuthentication,
130
+ parameters: Union[ReadMultipleParameter, ReadSingleParameter],
131
+ headers: Optional[Dict[str, str]] = None,
132
+ ) -> Union[
133
+ ReadMultipleResourceOperationResult[
134
+ SystemRoleDataSchema, FlexiblePagination, None
135
+ ],
136
+ ReadSingleResourceOperationResult[SystemRoleDataSchema, None],
137
+ ]:
138
+ operation_action = ReadResourceOperationAction()
139
+ executed_at = datetime.now(tz=timezone.utc)
140
+
141
+ # Get function identifier
142
+ func = self.__class__
143
+ module, qualname = func.__module__, func.__qualname__
144
+
145
+ # Define arguments being used in this function
146
+ positional_arguments = [cardinality]
147
+ keyword_arguments = {
148
+ "authentication": authentication.model_dump(
149
+ mode="json",
150
+ exclude={
151
+ "credentials": {
152
+ "token": {
153
+ "payload": {
154
+ "iat_dt",
155
+ "iat",
156
+ "exp_dt",
157
+ "exp",
158
+ }
159
+ }
160
+ }
161
+ },
162
+ ),
163
+ "parameters": parameters.model_dump(mode="json"),
164
+ }
165
+
166
+ # Define full function string
167
+ function = f"{qualname}({json.dumps(positional_arguments)}|{json.dumps(keyword_arguments)})"
168
+
169
+ # Define full cache key
170
+ cache_key = build_key(module, function, namespace=self._namespace)
171
+
172
+ if parameters.use_cache:
173
+ operation_context = deepcopy(self._default_operation_context)
174
+ operation_context.target.type = OperationTarget.CACHE
175
+
176
+ # Check redis for data
177
+ result_str = await self._redis.get(cache_key)
178
+
179
+ if result_str is not None:
180
+ completed_at = datetime.now(tz=timezone.utc)
181
+
182
+ if isinstance(parameters, ReadMultipleParameter):
183
+ result = ReadMultipleResourceOperationResult[
184
+ SystemRoleDataSchema, FlexiblePagination, None
185
+ ].model_validate(json.loads(result_str))
186
+ ReadMultipleResourceOperationSchema[
187
+ GeneralAuthentication,
188
+ SystemRoleDataSchema,
189
+ FlexiblePagination,
190
+ None,
191
+ ](
192
+ service_context=self._service_context,
193
+ id=operation_id,
194
+ context=operation_context,
195
+ timestamp=OperationTimestamp(
196
+ executed_at=executed_at,
197
+ completed_at=completed_at,
198
+ duration=(completed_at - executed_at).total_seconds(),
199
+ ),
200
+ summary="Successfully retrieved multiple system roles from cache",
201
+ request_context=request_context,
202
+ authentication=authentication,
203
+ action=operation_action,
204
+ resource=RESOURCE,
205
+ result=result,
206
+ ).log(
207
+ self._logger, LogLevel.INFO
208
+ )
209
+ elif isinstance(parameters, ReadSingleParameter):
210
+ result = ReadSingleResourceOperationResult[
211
+ SystemRoleDataSchema, None
212
+ ].model_validate(json.loads(result_str))
213
+ ReadSingleResourceOperationSchema[
214
+ GeneralAuthentication, SystemRoleDataSchema, None
215
+ ](
216
+ service_context=self._service_context,
217
+ id=operation_id,
218
+ context=operation_context,
219
+ timestamp=OperationTimestamp(
220
+ executed_at=executed_at,
221
+ completed_at=completed_at,
222
+ duration=(completed_at - executed_at).total_seconds(),
223
+ ),
224
+ summary="Successfully retrieved single system role from cache",
225
+ request_context=request_context,
226
+ authentication=authentication,
227
+ action=operation_action,
228
+ resource=RESOURCE,
229
+ result=result,
230
+ ).log(
231
+ self._logger, LogLevel.INFO
232
+ )
233
+ return result
234
+
235
+ operation_context = deepcopy(self._default_operation_context)
236
+ async with self._http_client_manager.get() as http_client:
237
+ # Create headers
238
+ base_headers = {
239
+ "content-type": "application/json",
240
+ "x-operation-id": str(operation_id),
241
+ }
242
+ if headers is not None:
243
+ headers = merge_dicts(base_headers, headers)
244
+ else:
245
+ headers = base_headers
246
+
247
+ # Create auth
248
+ token = None
249
+ try:
250
+ token = reencode(
251
+ payload=authentication.credentials.token.payload,
252
+ key=self._private_key,
253
+ )
254
+ except Exception:
255
+ pass
256
+
257
+ auth = BearerAuth(token) if token is not None else None
258
+
259
+ if isinstance(parameters, ReadMultipleParameter):
260
+ # Define URL
261
+ url = f"{self._url}/v1/{RESOURCE.identifiers[0].url_slug}/"
262
+
263
+ # Parse parameters to query params
264
+ params = ReadMultipleQueryParameter.model_validate(
265
+ parameters.model_dump()
266
+ ).model_dump(
267
+ exclude={"sort_columns", "date_filters"}, exclude_none=True
268
+ )
269
+ elif isinstance(parameters, ReadSingleParameter):
270
+ # Define URL
271
+ url = f"{self._url}/v1/{RESOURCE.identifiers[0].url_slug}/{parameters.identifier}/{parameters.value}"
272
+
273
+ # Parse parameters to query params
274
+ params = ReadSingleQueryParameterSchema.model_validate(
275
+ parameters.model_dump()
276
+ ).model_dump(exclude_none=True)
277
+
278
+ # Send request and wait for response
279
+ response = await http_client.get(
280
+ url=url, params=params, headers=headers, auth=auth
281
+ )
282
+
283
+ if response.is_success:
284
+ completed_at = datetime.now(tz=timezone.utc)
285
+
286
+ if isinstance(parameters, ReadMultipleParameter):
287
+ validated_response = MultipleDataResponseSchema[
288
+ SystemRoleDataSchema, FlexiblePagination, None
289
+ ].model_validate(response.json())
290
+ data = DataPair[List[SystemRoleDataSchema], None](
291
+ old=validated_response.data,
292
+ new=None,
293
+ )
294
+ result = ReadMultipleResourceOperationResult[
295
+ SystemRoleDataSchema, FlexiblePagination, None
296
+ ](
297
+ data=data,
298
+ pagination=validated_response.pagination,
299
+ metadata=None,
300
+ other=None,
301
+ )
302
+ ReadMultipleResourceOperationSchema[
303
+ GeneralAuthentication,
304
+ SystemRoleDataSchema,
305
+ FlexiblePagination,
306
+ None,
307
+ ](
308
+ service_context=self._service_context,
309
+ id=operation_id,
310
+ context=operation_context,
311
+ timestamp=OperationTimestamp(
312
+ executed_at=executed_at,
313
+ completed_at=completed_at,
314
+ duration=(completed_at - executed_at).total_seconds(),
315
+ ),
316
+ summary="Successfully retrieved multiple system roles from http request",
317
+ request_context=request_context,
318
+ authentication=authentication,
319
+ action=operation_action,
320
+ resource=RESOURCE,
321
+ result=result,
322
+ ).log(
323
+ self._logger, level=LogLevel.INFO
324
+ )
325
+ elif isinstance(parameters, ReadSingleParameter):
326
+ validated_response = SingleDataResponseSchema[
327
+ SystemRoleDataSchema, None
328
+ ].model_validate(response.json())
329
+ data = DataPair[SystemRoleDataSchema, None](
330
+ old=validated_response.data,
331
+ new=None,
332
+ )
333
+ result = ReadSingleResourceOperationResult[
334
+ SystemRoleDataSchema, None
335
+ ](
336
+ data=data,
337
+ pagination=validated_response.pagination,
338
+ metadata=None,
339
+ other=None,
340
+ )
341
+ ReadSingleResourceOperationSchema[
342
+ GeneralAuthentication, SystemRoleDataSchema, None
343
+ ](
344
+ service_context=self._service_context,
345
+ id=operation_id,
346
+ context=operation_context,
347
+ timestamp=OperationTimestamp(
348
+ executed_at=executed_at,
349
+ completed_at=completed_at,
350
+ duration=(completed_at - executed_at).total_seconds(),
351
+ ),
352
+ summary="Successfully retrieved single system role from http request",
353
+ request_context=request_context,
354
+ authentication=authentication,
355
+ action=operation_action,
356
+ resource=RESOURCE,
357
+ result=result,
358
+ ).log(
359
+ self._logger, level=LogLevel.INFO
360
+ )
361
+
362
+ if parameters.use_cache:
363
+ await self._redis.set(
364
+ cache_key, result.model_dump_json(), Expiration.EXP_1MO.value
365
+ )
366
+
367
+ return result
368
+
369
+ self._raise_resource_http_request_error(
370
+ response=response,
371
+ operation_id=operation_id,
372
+ operation_context=operation_context,
373
+ executed_at=executed_at,
374
+ operation_action=operation_action,
375
+ request_context=request_context,
376
+ authentication=authentication,
377
+ resource=RESOURCE,
378
+ )