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