maleo-metadata-client 0.0.7__py3-none-any.whl → 0.0.10__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.
- maleo/metadata/client/manager.py +65 -2
- maleo/metadata/client/services/blood_type.py +10 -8
- maleo/metadata/client/services/gender.py +304 -0
- maleo/metadata/client/services/medical_role.py +354 -0
- maleo/metadata/client/services/medical_service.py +306 -0
- maleo/metadata/client/services/organization_type.py +310 -0
- maleo/metadata/client/services/service.py +304 -0
- maleo/metadata/client/services/system_role.py +304 -0
- maleo/metadata/client/services/user_type.py +302 -0
- {maleo_metadata_client-0.0.7.dist-info → maleo_metadata_client-0.0.10.dist-info}/METADATA +12 -11
- maleo_metadata_client-0.0.10.dist-info/RECORD +16 -0
- maleo_metadata_client-0.0.7.dist-info/RECORD +0 -9
- {maleo_metadata_client-0.0.7.dist-info → maleo_metadata_client-0.0.10.dist-info}/WHEEL +0 -0
- {maleo_metadata_client-0.0.7.dist-info → maleo_metadata_client-0.0.10.dist-info}/licenses/LICENSE +0 -0
- {maleo_metadata_client-0.0.7.dist-info → maleo_metadata_client-0.0.10.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from copy import deepcopy
|
|
3
|
+
from datetime import datetime, timezone
|
|
4
|
+
from typing import Literal, Union, overload
|
|
5
|
+
from uuid import UUID
|
|
6
|
+
from maleo.client.maleo.config import MaleoMetadataClientConfig
|
|
7
|
+
from maleo.client.maleo.service import MaleoClientService
|
|
8
|
+
from maleo.database.enums import Connection
|
|
9
|
+
from maleo.database.utils import build_cache_key
|
|
10
|
+
from maleo.enums.cardinality import Cardinality
|
|
11
|
+
from maleo.enums.connection import Header
|
|
12
|
+
from maleo.logging.enums import Level
|
|
13
|
+
from maleo.metadata.constants.organization_type import ORGANIZATION_TYPE_RESOURCE
|
|
14
|
+
from maleo.metadata.schemas.organization_type import (
|
|
15
|
+
ReadMultipleParameter,
|
|
16
|
+
ReadSingleParameter,
|
|
17
|
+
StandardOrganizationTypeSchema,
|
|
18
|
+
FullOrganizationTypeSchema,
|
|
19
|
+
)
|
|
20
|
+
from maleo.metadata.enums.organization_type import Granularity, IdentifierType
|
|
21
|
+
from maleo.metadata.utils.organization_type import get_schema_model
|
|
22
|
+
from maleo.schemas.connection import ConnectionContext
|
|
23
|
+
from maleo.schemas.exception.factory import Factory as MaleoExceptionFactory
|
|
24
|
+
from maleo.schemas.operation.action.resource import ReadResourceOperationAction
|
|
25
|
+
from maleo.schemas.operation.enums import OperationType, Target
|
|
26
|
+
from maleo.schemas.operation.mixins import Timestamp
|
|
27
|
+
from maleo.schemas.operation.resource import (
|
|
28
|
+
ReadMultipleResourceOperation,
|
|
29
|
+
ReadSingleResourceOperation,
|
|
30
|
+
)
|
|
31
|
+
from maleo.schemas.pagination import StrictPagination
|
|
32
|
+
from maleo.schemas.response import (
|
|
33
|
+
MultipleDataResponse,
|
|
34
|
+
ReadMultipleDataResponse,
|
|
35
|
+
SingleDataResponse,
|
|
36
|
+
ReadSingleDataResponse,
|
|
37
|
+
)
|
|
38
|
+
from maleo.schemas.security.authorization import (
|
|
39
|
+
OptionalAnyAuthorization,
|
|
40
|
+
AnyAuthorization,
|
|
41
|
+
Factory as AuthorizationFactory,
|
|
42
|
+
)
|
|
43
|
+
from maleo.schemas.security.impersonation import OptionalImpersonation
|
|
44
|
+
from maleo.types.dict import OptionalStringToStringDict
|
|
45
|
+
from maleo.utils.merger import merge_dicts
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class OrganizationTypeClientService(MaleoClientService[MaleoMetadataClientConfig]):
|
|
49
|
+
resource = ORGANIZATION_TYPE_RESOURCE
|
|
50
|
+
|
|
51
|
+
@overload
|
|
52
|
+
async def read(
|
|
53
|
+
self,
|
|
54
|
+
cardinality: Literal[Cardinality.MULTIPLE],
|
|
55
|
+
granularity: Literal[Granularity.STANDARD],
|
|
56
|
+
*,
|
|
57
|
+
operation_id: UUID,
|
|
58
|
+
resource_operation_action: ReadResourceOperationAction,
|
|
59
|
+
connection_context: ConnectionContext,
|
|
60
|
+
authorization: AnyAuthorization,
|
|
61
|
+
impersonation: OptionalImpersonation = None,
|
|
62
|
+
parameters: ReadMultipleParameter,
|
|
63
|
+
headers: OptionalStringToStringDict = None,
|
|
64
|
+
) -> ReadMultipleDataResponse[
|
|
65
|
+
StandardOrganizationTypeSchema, StrictPagination, None
|
|
66
|
+
]: ...
|
|
67
|
+
@overload
|
|
68
|
+
async def read(
|
|
69
|
+
self,
|
|
70
|
+
cardinality: Literal[Cardinality.MULTIPLE],
|
|
71
|
+
granularity: Literal[Granularity.FULL],
|
|
72
|
+
*,
|
|
73
|
+
operation_id: UUID,
|
|
74
|
+
resource_operation_action: ReadResourceOperationAction,
|
|
75
|
+
connection_context: ConnectionContext,
|
|
76
|
+
authorization: AnyAuthorization,
|
|
77
|
+
impersonation: OptionalImpersonation = None,
|
|
78
|
+
parameters: ReadMultipleParameter,
|
|
79
|
+
headers: OptionalStringToStringDict = None,
|
|
80
|
+
) -> ReadMultipleDataResponse[
|
|
81
|
+
FullOrganizationTypeSchema, StrictPagination, None
|
|
82
|
+
]: ...
|
|
83
|
+
@overload
|
|
84
|
+
async def read(
|
|
85
|
+
self,
|
|
86
|
+
cardinality: Literal[Cardinality.SINGLE],
|
|
87
|
+
granularity: Literal[Granularity.STANDARD],
|
|
88
|
+
*,
|
|
89
|
+
operation_id: UUID,
|
|
90
|
+
resource_operation_action: ReadResourceOperationAction,
|
|
91
|
+
connection_context: ConnectionContext,
|
|
92
|
+
authorization: AnyAuthorization,
|
|
93
|
+
impersonation: OptionalImpersonation = None,
|
|
94
|
+
parameters: ReadSingleParameter,
|
|
95
|
+
headers: OptionalStringToStringDict = None,
|
|
96
|
+
) -> ReadSingleDataResponse[StandardOrganizationTypeSchema, None]: ...
|
|
97
|
+
@overload
|
|
98
|
+
async def read(
|
|
99
|
+
self,
|
|
100
|
+
cardinality: Literal[Cardinality.SINGLE],
|
|
101
|
+
granularity: Literal[Granularity.FULL],
|
|
102
|
+
*,
|
|
103
|
+
operation_id: UUID,
|
|
104
|
+
resource_operation_action: ReadResourceOperationAction,
|
|
105
|
+
connection_context: ConnectionContext,
|
|
106
|
+
authorization: AnyAuthorization,
|
|
107
|
+
impersonation: OptionalImpersonation = None,
|
|
108
|
+
parameters: ReadSingleParameter,
|
|
109
|
+
headers: OptionalStringToStringDict = None,
|
|
110
|
+
) -> ReadSingleDataResponse[FullOrganizationTypeSchema, None]: ...
|
|
111
|
+
async def read(
|
|
112
|
+
self,
|
|
113
|
+
cardinality: Cardinality,
|
|
114
|
+
granularity: Granularity,
|
|
115
|
+
*,
|
|
116
|
+
operation_id: UUID,
|
|
117
|
+
resource_operation_action: ReadResourceOperationAction,
|
|
118
|
+
connection_context: ConnectionContext,
|
|
119
|
+
authorization: OptionalAnyAuthorization = None,
|
|
120
|
+
impersonation: OptionalImpersonation = None,
|
|
121
|
+
parameters: Union[ReadMultipleParameter, ReadSingleParameter],
|
|
122
|
+
headers: OptionalStringToStringDict = None,
|
|
123
|
+
) -> Union[
|
|
124
|
+
ReadMultipleDataResponse[
|
|
125
|
+
StandardOrganizationTypeSchema, StrictPagination, None
|
|
126
|
+
],
|
|
127
|
+
ReadMultipleDataResponse[FullOrganizationTypeSchema, StrictPagination, None],
|
|
128
|
+
ReadSingleDataResponse[StandardOrganizationTypeSchema, None],
|
|
129
|
+
ReadSingleDataResponse[FullOrganizationTypeSchema, None],
|
|
130
|
+
]:
|
|
131
|
+
redis_client = self._redis.manager.client.get(Connection.ASYNC)
|
|
132
|
+
data_model_cls = get_schema_model(granularity)
|
|
133
|
+
|
|
134
|
+
executed_at = datetime.now(tz=timezone.utc)
|
|
135
|
+
|
|
136
|
+
# Define arguments being used in this function
|
|
137
|
+
positional_arguments = [cardinality, granularity]
|
|
138
|
+
keyword_arguments = {
|
|
139
|
+
"authorization": (
|
|
140
|
+
authorization.model_dump(mode="json")
|
|
141
|
+
if authorization is not None
|
|
142
|
+
else None
|
|
143
|
+
),
|
|
144
|
+
"parameters": parameters.model_dump(mode="json"),
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
# Define full function string
|
|
148
|
+
ext = f"({json.dumps(positional_arguments)}|{json.dumps(keyword_arguments)})"
|
|
149
|
+
|
|
150
|
+
# Define full cache_key
|
|
151
|
+
cache_key = build_cache_key(ext, namespace=self._namespace)
|
|
152
|
+
|
|
153
|
+
if parameters.use_cache:
|
|
154
|
+
# Initialize cache operation context
|
|
155
|
+
operation_context = deepcopy(self._operation_context)
|
|
156
|
+
operation_context.target.type = Target.CACHE
|
|
157
|
+
|
|
158
|
+
redis_response_str = await redis_client.get(cache_key)
|
|
159
|
+
|
|
160
|
+
if redis_response_str is not None:
|
|
161
|
+
operation_timestamp = Timestamp.completed_now(executed_at)
|
|
162
|
+
if cardinality is Cardinality.MULTIPLE:
|
|
163
|
+
response = ReadMultipleDataResponse[
|
|
164
|
+
data_model_cls, StrictPagination, None
|
|
165
|
+
].model_validate_json(redis_response_str)
|
|
166
|
+
ReadMultipleResourceOperation[
|
|
167
|
+
data_model_cls, StrictPagination, None
|
|
168
|
+
](
|
|
169
|
+
application_context=self._application_context,
|
|
170
|
+
id=operation_id,
|
|
171
|
+
context=operation_context,
|
|
172
|
+
action=resource_operation_action,
|
|
173
|
+
resource=self.resource,
|
|
174
|
+
timestamp=operation_timestamp,
|
|
175
|
+
summary=f"Successfully retrieved {cardinality} {granularity} organization types from cache",
|
|
176
|
+
connection_context=connection_context,
|
|
177
|
+
authentication=None,
|
|
178
|
+
authorization=authorization,
|
|
179
|
+
impersonation=impersonation,
|
|
180
|
+
response=response,
|
|
181
|
+
).log(
|
|
182
|
+
self._logger, Level.INFO
|
|
183
|
+
)
|
|
184
|
+
elif cardinality is Cardinality.SINGLE:
|
|
185
|
+
response = ReadSingleDataResponse[
|
|
186
|
+
data_model_cls, None
|
|
187
|
+
].model_validate_json(redis_response_str)
|
|
188
|
+
ReadSingleResourceOperation[data_model_cls, None](
|
|
189
|
+
application_context=self._application_context,
|
|
190
|
+
id=operation_id,
|
|
191
|
+
context=operation_context,
|
|
192
|
+
action=resource_operation_action,
|
|
193
|
+
resource=self.resource,
|
|
194
|
+
timestamp=operation_timestamp,
|
|
195
|
+
summary=f"Successfully retrieved {cardinality} {granularity} organization type from cache",
|
|
196
|
+
connection_context=connection_context,
|
|
197
|
+
authentication=None,
|
|
198
|
+
authorization=authorization,
|
|
199
|
+
impersonation=impersonation,
|
|
200
|
+
response=response,
|
|
201
|
+
).log(self._logger, Level.INFO)
|
|
202
|
+
|
|
203
|
+
return response # type: ignore
|
|
204
|
+
|
|
205
|
+
operation_context = deepcopy(self._operation_context)
|
|
206
|
+
operation_context.target.type = Target.MICROSERVICE
|
|
207
|
+
|
|
208
|
+
async with self._http_client_manager.get() as http_client:
|
|
209
|
+
base_headers = {
|
|
210
|
+
Header.CONTENT_TYPE.value: "application/json",
|
|
211
|
+
Header.X_OPERATION_ID.value: str(operation_id),
|
|
212
|
+
}
|
|
213
|
+
if impersonation is not None:
|
|
214
|
+
base_headers[Header.X_USER_ID.value] = str(impersonation.user_id)
|
|
215
|
+
if impersonation.organization_id is not None:
|
|
216
|
+
base_headers[Header.X_ORGANIZATION_ID.value] = str(
|
|
217
|
+
impersonation.organization_id
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
if headers is not None:
|
|
221
|
+
headers = merge_dicts(base_headers, headers)
|
|
222
|
+
else:
|
|
223
|
+
headers = base_headers
|
|
224
|
+
|
|
225
|
+
if authorization is not None:
|
|
226
|
+
auth = AuthorizationFactory.httpx_auth(
|
|
227
|
+
scheme=authorization.scheme, authorization=authorization.credentials
|
|
228
|
+
)
|
|
229
|
+
else:
|
|
230
|
+
auth = None
|
|
231
|
+
|
|
232
|
+
if isinstance(parameters, ReadMultipleParameter):
|
|
233
|
+
url = f"{self._config.url}/v1/{self.resource.identifiers[-1].slug}/"
|
|
234
|
+
elif isinstance(parameters, ReadSingleParameter):
|
|
235
|
+
if parameters.identifier is IdentifierType.ID:
|
|
236
|
+
url = f"{self._config.url}/v1/{self.resource.identifiers[-1].slug}/{parameters.value}"
|
|
237
|
+
else:
|
|
238
|
+
url = f"{self._config.url}/v1/{self.resource.identifiers[-1].slug}/{parameters.identifier}/{parameters.value}"
|
|
239
|
+
|
|
240
|
+
params = parameters.to_query_params()
|
|
241
|
+
|
|
242
|
+
response = await http_client.get(
|
|
243
|
+
url, params=params, headers=headers, auth=auth
|
|
244
|
+
)
|
|
245
|
+
|
|
246
|
+
operation_timestamp = Timestamp.completed_now(executed_at)
|
|
247
|
+
|
|
248
|
+
if response.is_error:
|
|
249
|
+
raise MaleoExceptionFactory.from_httpx(
|
|
250
|
+
response,
|
|
251
|
+
operation_type=OperationType.REQUEST,
|
|
252
|
+
application_context=self._application_context,
|
|
253
|
+
operation_id=operation_id,
|
|
254
|
+
operation_context=operation_context,
|
|
255
|
+
operation_action=resource_operation_action,
|
|
256
|
+
operation_timestamp=operation_timestamp,
|
|
257
|
+
connection_context=connection_context,
|
|
258
|
+
authentication=None,
|
|
259
|
+
authorization=authorization,
|
|
260
|
+
impersonation=impersonation,
|
|
261
|
+
logger=self._logger,
|
|
262
|
+
)
|
|
263
|
+
|
|
264
|
+
if isinstance(parameters, ReadMultipleParameter):
|
|
265
|
+
validated_response = MultipleDataResponse[
|
|
266
|
+
data_model_cls, StrictPagination, None
|
|
267
|
+
].model_validate(response.json())
|
|
268
|
+
service_response = ReadMultipleDataResponse[
|
|
269
|
+
data_model_cls, StrictPagination, None
|
|
270
|
+
].new(
|
|
271
|
+
data=validated_response.data,
|
|
272
|
+
pagination=validated_response.pagination,
|
|
273
|
+
)
|
|
274
|
+
ReadMultipleResourceOperation[data_model_cls, StrictPagination, None](
|
|
275
|
+
application_context=self._application_context,
|
|
276
|
+
id=operation_id,
|
|
277
|
+
context=operation_context,
|
|
278
|
+
action=resource_operation_action,
|
|
279
|
+
resource=ORGANIZATION_TYPE_RESOURCE,
|
|
280
|
+
timestamp=operation_timestamp,
|
|
281
|
+
summary=f"Successfully retrieved multiple {granularity} organization types from microservice",
|
|
282
|
+
connection_context=connection_context,
|
|
283
|
+
authentication=None,
|
|
284
|
+
authorization=authorization,
|
|
285
|
+
impersonation=impersonation,
|
|
286
|
+
response=service_response,
|
|
287
|
+
).log(self._logger, Level.INFO)
|
|
288
|
+
elif isinstance(parameters, ReadSingleParameter):
|
|
289
|
+
validated_response = SingleDataResponse[
|
|
290
|
+
data_model_cls, None
|
|
291
|
+
].model_validate(response.json())
|
|
292
|
+
service_response = ReadSingleDataResponse[data_model_cls, None].new(
|
|
293
|
+
data=validated_response.data,
|
|
294
|
+
)
|
|
295
|
+
ReadSingleResourceOperation[data_model_cls, None](
|
|
296
|
+
application_context=self._application_context,
|
|
297
|
+
id=operation_id,
|
|
298
|
+
context=operation_context,
|
|
299
|
+
action=resource_operation_action,
|
|
300
|
+
resource=ORGANIZATION_TYPE_RESOURCE,
|
|
301
|
+
timestamp=operation_timestamp,
|
|
302
|
+
summary=f"Successfully retrieved single {granularity} organization type from microservice",
|
|
303
|
+
connection_context=connection_context,
|
|
304
|
+
authentication=None,
|
|
305
|
+
authorization=authorization,
|
|
306
|
+
impersonation=impersonation,
|
|
307
|
+
response=service_response,
|
|
308
|
+
).log(self._logger, Level.INFO)
|
|
309
|
+
|
|
310
|
+
return service_response # type: ignore
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from copy import deepcopy
|
|
3
|
+
from datetime import datetime, timezone
|
|
4
|
+
from typing import Literal, Union, overload
|
|
5
|
+
from uuid import UUID
|
|
6
|
+
from maleo.client.maleo.config import MaleoMetadataClientConfig
|
|
7
|
+
from maleo.client.maleo.service import MaleoClientService
|
|
8
|
+
from maleo.database.enums import Connection
|
|
9
|
+
from maleo.database.utils import build_cache_key
|
|
10
|
+
from maleo.enums.cardinality import Cardinality
|
|
11
|
+
from maleo.enums.connection import Header
|
|
12
|
+
from maleo.logging.enums import Level
|
|
13
|
+
from maleo.metadata.constants.service import SERVICE_RESOURCE
|
|
14
|
+
from maleo.metadata.schemas.service import (
|
|
15
|
+
ReadMultipleParameter,
|
|
16
|
+
ReadSingleParameter,
|
|
17
|
+
StandardServiceSchema,
|
|
18
|
+
FullServiceSchema,
|
|
19
|
+
)
|
|
20
|
+
from maleo.metadata.enums.service import Granularity, IdentifierType
|
|
21
|
+
from maleo.metadata.utils.service import get_schema_model
|
|
22
|
+
from maleo.schemas.connection import ConnectionContext
|
|
23
|
+
from maleo.schemas.exception.factory import Factory as MaleoExceptionFactory
|
|
24
|
+
from maleo.schemas.operation.action.resource import ReadResourceOperationAction
|
|
25
|
+
from maleo.schemas.operation.enums import OperationType, Target
|
|
26
|
+
from maleo.schemas.operation.mixins import Timestamp
|
|
27
|
+
from maleo.schemas.operation.resource import (
|
|
28
|
+
ReadMultipleResourceOperation,
|
|
29
|
+
ReadSingleResourceOperation,
|
|
30
|
+
)
|
|
31
|
+
from maleo.schemas.pagination import StrictPagination
|
|
32
|
+
from maleo.schemas.response import (
|
|
33
|
+
MultipleDataResponse,
|
|
34
|
+
ReadMultipleDataResponse,
|
|
35
|
+
SingleDataResponse,
|
|
36
|
+
ReadSingleDataResponse,
|
|
37
|
+
)
|
|
38
|
+
from maleo.schemas.security.authorization import (
|
|
39
|
+
OptionalAnyAuthorization,
|
|
40
|
+
AnyAuthorization,
|
|
41
|
+
Factory as AuthorizationFactory,
|
|
42
|
+
)
|
|
43
|
+
from maleo.schemas.security.impersonation import OptionalImpersonation
|
|
44
|
+
from maleo.types.dict import OptionalStringToStringDict
|
|
45
|
+
from maleo.utils.merger import merge_dicts
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class ServiceClientService(MaleoClientService[MaleoMetadataClientConfig]):
|
|
49
|
+
resource = SERVICE_RESOURCE
|
|
50
|
+
|
|
51
|
+
@overload
|
|
52
|
+
async def read(
|
|
53
|
+
self,
|
|
54
|
+
cardinality: Literal[Cardinality.MULTIPLE],
|
|
55
|
+
granularity: Literal[Granularity.STANDARD],
|
|
56
|
+
*,
|
|
57
|
+
operation_id: UUID,
|
|
58
|
+
resource_operation_action: ReadResourceOperationAction,
|
|
59
|
+
connection_context: ConnectionContext,
|
|
60
|
+
authorization: AnyAuthorization,
|
|
61
|
+
impersonation: OptionalImpersonation = None,
|
|
62
|
+
parameters: ReadMultipleParameter,
|
|
63
|
+
headers: OptionalStringToStringDict = None,
|
|
64
|
+
) -> ReadMultipleDataResponse[StandardServiceSchema, StrictPagination, None]: ...
|
|
65
|
+
@overload
|
|
66
|
+
async def read(
|
|
67
|
+
self,
|
|
68
|
+
cardinality: Literal[Cardinality.MULTIPLE],
|
|
69
|
+
granularity: Literal[Granularity.FULL],
|
|
70
|
+
*,
|
|
71
|
+
operation_id: UUID,
|
|
72
|
+
resource_operation_action: ReadResourceOperationAction,
|
|
73
|
+
connection_context: ConnectionContext,
|
|
74
|
+
authorization: AnyAuthorization,
|
|
75
|
+
impersonation: OptionalImpersonation = None,
|
|
76
|
+
parameters: ReadMultipleParameter,
|
|
77
|
+
headers: OptionalStringToStringDict = None,
|
|
78
|
+
) -> ReadMultipleDataResponse[FullServiceSchema, StrictPagination, None]: ...
|
|
79
|
+
@overload
|
|
80
|
+
async def read(
|
|
81
|
+
self,
|
|
82
|
+
cardinality: Literal[Cardinality.SINGLE],
|
|
83
|
+
granularity: Literal[Granularity.STANDARD],
|
|
84
|
+
*,
|
|
85
|
+
operation_id: UUID,
|
|
86
|
+
resource_operation_action: ReadResourceOperationAction,
|
|
87
|
+
connection_context: ConnectionContext,
|
|
88
|
+
authorization: AnyAuthorization,
|
|
89
|
+
impersonation: OptionalImpersonation = None,
|
|
90
|
+
parameters: ReadSingleParameter,
|
|
91
|
+
headers: OptionalStringToStringDict = None,
|
|
92
|
+
) -> ReadSingleDataResponse[StandardServiceSchema, None]: ...
|
|
93
|
+
@overload
|
|
94
|
+
async def read(
|
|
95
|
+
self,
|
|
96
|
+
cardinality: Literal[Cardinality.SINGLE],
|
|
97
|
+
granularity: Literal[Granularity.FULL],
|
|
98
|
+
*,
|
|
99
|
+
operation_id: UUID,
|
|
100
|
+
resource_operation_action: ReadResourceOperationAction,
|
|
101
|
+
connection_context: ConnectionContext,
|
|
102
|
+
authorization: AnyAuthorization,
|
|
103
|
+
impersonation: OptionalImpersonation = None,
|
|
104
|
+
parameters: ReadSingleParameter,
|
|
105
|
+
headers: OptionalStringToStringDict = None,
|
|
106
|
+
) -> ReadSingleDataResponse[FullServiceSchema, None]: ...
|
|
107
|
+
async def read(
|
|
108
|
+
self,
|
|
109
|
+
cardinality: Cardinality,
|
|
110
|
+
granularity: Granularity,
|
|
111
|
+
*,
|
|
112
|
+
operation_id: UUID,
|
|
113
|
+
resource_operation_action: ReadResourceOperationAction,
|
|
114
|
+
connection_context: ConnectionContext,
|
|
115
|
+
authorization: OptionalAnyAuthorization = None,
|
|
116
|
+
impersonation: OptionalImpersonation = None,
|
|
117
|
+
parameters: Union[ReadMultipleParameter, ReadSingleParameter],
|
|
118
|
+
headers: OptionalStringToStringDict = None,
|
|
119
|
+
) -> Union[
|
|
120
|
+
ReadMultipleDataResponse[StandardServiceSchema, StrictPagination, None],
|
|
121
|
+
ReadMultipleDataResponse[FullServiceSchema, StrictPagination, None],
|
|
122
|
+
ReadSingleDataResponse[StandardServiceSchema, None],
|
|
123
|
+
ReadSingleDataResponse[FullServiceSchema, None],
|
|
124
|
+
]:
|
|
125
|
+
redis_client = self._redis.manager.client.get(Connection.ASYNC)
|
|
126
|
+
data_model_cls = get_schema_model(granularity)
|
|
127
|
+
|
|
128
|
+
executed_at = datetime.now(tz=timezone.utc)
|
|
129
|
+
|
|
130
|
+
# Define arguments being used in this function
|
|
131
|
+
positional_arguments = [cardinality, granularity]
|
|
132
|
+
keyword_arguments = {
|
|
133
|
+
"authorization": (
|
|
134
|
+
authorization.model_dump(mode="json")
|
|
135
|
+
if authorization is not None
|
|
136
|
+
else None
|
|
137
|
+
),
|
|
138
|
+
"parameters": parameters.model_dump(mode="json"),
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
# Define full function string
|
|
142
|
+
ext = f"({json.dumps(positional_arguments)}|{json.dumps(keyword_arguments)})"
|
|
143
|
+
|
|
144
|
+
# Define full cache_key
|
|
145
|
+
cache_key = build_cache_key(ext, namespace=self._namespace)
|
|
146
|
+
|
|
147
|
+
if parameters.use_cache:
|
|
148
|
+
# Initialize cache operation context
|
|
149
|
+
operation_context = deepcopy(self._operation_context)
|
|
150
|
+
operation_context.target.type = Target.CACHE
|
|
151
|
+
|
|
152
|
+
redis_response_str = await redis_client.get(cache_key)
|
|
153
|
+
|
|
154
|
+
if redis_response_str is not None:
|
|
155
|
+
operation_timestamp = Timestamp.completed_now(executed_at)
|
|
156
|
+
if cardinality is Cardinality.MULTIPLE:
|
|
157
|
+
response = ReadMultipleDataResponse[
|
|
158
|
+
data_model_cls, StrictPagination, None
|
|
159
|
+
].model_validate_json(redis_response_str)
|
|
160
|
+
ReadMultipleResourceOperation[
|
|
161
|
+
data_model_cls, StrictPagination, None
|
|
162
|
+
](
|
|
163
|
+
application_context=self._application_context,
|
|
164
|
+
id=operation_id,
|
|
165
|
+
context=operation_context,
|
|
166
|
+
action=resource_operation_action,
|
|
167
|
+
resource=self.resource,
|
|
168
|
+
timestamp=operation_timestamp,
|
|
169
|
+
summary=f"Successfully retrieved {cardinality} {granularity} services from cache",
|
|
170
|
+
connection_context=connection_context,
|
|
171
|
+
authentication=None,
|
|
172
|
+
authorization=authorization,
|
|
173
|
+
impersonation=impersonation,
|
|
174
|
+
response=response,
|
|
175
|
+
).log(
|
|
176
|
+
self._logger, Level.INFO
|
|
177
|
+
)
|
|
178
|
+
elif cardinality is Cardinality.SINGLE:
|
|
179
|
+
response = ReadSingleDataResponse[
|
|
180
|
+
data_model_cls, None
|
|
181
|
+
].model_validate_json(redis_response_str)
|
|
182
|
+
ReadSingleResourceOperation[data_model_cls, None](
|
|
183
|
+
application_context=self._application_context,
|
|
184
|
+
id=operation_id,
|
|
185
|
+
context=operation_context,
|
|
186
|
+
action=resource_operation_action,
|
|
187
|
+
resource=self.resource,
|
|
188
|
+
timestamp=operation_timestamp,
|
|
189
|
+
summary=f"Successfully retrieved {cardinality} {granularity} service from cache",
|
|
190
|
+
connection_context=connection_context,
|
|
191
|
+
authentication=None,
|
|
192
|
+
authorization=authorization,
|
|
193
|
+
impersonation=impersonation,
|
|
194
|
+
response=response,
|
|
195
|
+
).log(self._logger, Level.INFO)
|
|
196
|
+
|
|
197
|
+
return response # type: ignore
|
|
198
|
+
|
|
199
|
+
operation_context = deepcopy(self._operation_context)
|
|
200
|
+
operation_context.target.type = Target.MICROSERVICE
|
|
201
|
+
|
|
202
|
+
async with self._http_client_manager.get() as http_client:
|
|
203
|
+
base_headers = {
|
|
204
|
+
Header.CONTENT_TYPE.value: "application/json",
|
|
205
|
+
Header.X_OPERATION_ID.value: str(operation_id),
|
|
206
|
+
}
|
|
207
|
+
if impersonation is not None:
|
|
208
|
+
base_headers[Header.X_USER_ID.value] = str(impersonation.user_id)
|
|
209
|
+
if impersonation.organization_id is not None:
|
|
210
|
+
base_headers[Header.X_ORGANIZATION_ID.value] = str(
|
|
211
|
+
impersonation.organization_id
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
if headers is not None:
|
|
215
|
+
headers = merge_dicts(base_headers, headers)
|
|
216
|
+
else:
|
|
217
|
+
headers = base_headers
|
|
218
|
+
|
|
219
|
+
if authorization is not None:
|
|
220
|
+
auth = AuthorizationFactory.httpx_auth(
|
|
221
|
+
scheme=authorization.scheme, authorization=authorization.credentials
|
|
222
|
+
)
|
|
223
|
+
else:
|
|
224
|
+
auth = None
|
|
225
|
+
|
|
226
|
+
if isinstance(parameters, ReadMultipleParameter):
|
|
227
|
+
url = f"{self._config.url}/v1/{self.resource.identifiers[-1].slug}/"
|
|
228
|
+
elif isinstance(parameters, ReadSingleParameter):
|
|
229
|
+
if parameters.identifier is IdentifierType.ID:
|
|
230
|
+
url = f"{self._config.url}/v1/{self.resource.identifiers[-1].slug}/{parameters.value}"
|
|
231
|
+
else:
|
|
232
|
+
url = f"{self._config.url}/v1/{self.resource.identifiers[-1].slug}/{parameters.identifier}/{parameters.value}"
|
|
233
|
+
|
|
234
|
+
params = parameters.to_query_params()
|
|
235
|
+
|
|
236
|
+
response = await http_client.get(
|
|
237
|
+
url, params=params, headers=headers, auth=auth
|
|
238
|
+
)
|
|
239
|
+
|
|
240
|
+
operation_timestamp = Timestamp.completed_now(executed_at)
|
|
241
|
+
|
|
242
|
+
if response.is_error:
|
|
243
|
+
raise MaleoExceptionFactory.from_httpx(
|
|
244
|
+
response,
|
|
245
|
+
operation_type=OperationType.REQUEST,
|
|
246
|
+
application_context=self._application_context,
|
|
247
|
+
operation_id=operation_id,
|
|
248
|
+
operation_context=operation_context,
|
|
249
|
+
operation_action=resource_operation_action,
|
|
250
|
+
operation_timestamp=operation_timestamp,
|
|
251
|
+
connection_context=connection_context,
|
|
252
|
+
authentication=None,
|
|
253
|
+
authorization=authorization,
|
|
254
|
+
impersonation=impersonation,
|
|
255
|
+
logger=self._logger,
|
|
256
|
+
)
|
|
257
|
+
|
|
258
|
+
if isinstance(parameters, ReadMultipleParameter):
|
|
259
|
+
validated_response = MultipleDataResponse[
|
|
260
|
+
data_model_cls, StrictPagination, None
|
|
261
|
+
].model_validate(response.json())
|
|
262
|
+
service_response = ReadMultipleDataResponse[
|
|
263
|
+
data_model_cls, StrictPagination, None
|
|
264
|
+
].new(
|
|
265
|
+
data=validated_response.data,
|
|
266
|
+
pagination=validated_response.pagination,
|
|
267
|
+
)
|
|
268
|
+
ReadMultipleResourceOperation[data_model_cls, StrictPagination, None](
|
|
269
|
+
application_context=self._application_context,
|
|
270
|
+
id=operation_id,
|
|
271
|
+
context=operation_context,
|
|
272
|
+
action=resource_operation_action,
|
|
273
|
+
resource=SERVICE_RESOURCE,
|
|
274
|
+
timestamp=operation_timestamp,
|
|
275
|
+
summary=f"Successfully retrieved multiple {granularity} services from microservice",
|
|
276
|
+
connection_context=connection_context,
|
|
277
|
+
authentication=None,
|
|
278
|
+
authorization=authorization,
|
|
279
|
+
impersonation=impersonation,
|
|
280
|
+
response=service_response,
|
|
281
|
+
).log(self._logger, Level.INFO)
|
|
282
|
+
elif isinstance(parameters, ReadSingleParameter):
|
|
283
|
+
validated_response = SingleDataResponse[
|
|
284
|
+
data_model_cls, None
|
|
285
|
+
].model_validate(response.json())
|
|
286
|
+
service_response = ReadSingleDataResponse[data_model_cls, None].new(
|
|
287
|
+
data=validated_response.data,
|
|
288
|
+
)
|
|
289
|
+
ReadSingleResourceOperation[data_model_cls, None](
|
|
290
|
+
application_context=self._application_context,
|
|
291
|
+
id=operation_id,
|
|
292
|
+
context=operation_context,
|
|
293
|
+
action=resource_operation_action,
|
|
294
|
+
resource=SERVICE_RESOURCE,
|
|
295
|
+
timestamp=operation_timestamp,
|
|
296
|
+
summary=f"Successfully retrieved single {granularity} service from microservice",
|
|
297
|
+
connection_context=connection_context,
|
|
298
|
+
authentication=None,
|
|
299
|
+
authorization=authorization,
|
|
300
|
+
impersonation=impersonation,
|
|
301
|
+
response=service_response,
|
|
302
|
+
).log(self._logger, Level.INFO)
|
|
303
|
+
|
|
304
|
+
return service_response # type: ignore
|