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