Skip to content

Commit 7762849

Browse files
committed
✨ Refactor RPC service methods to streamline request handling and improve readability
1 parent efd24c9 commit 7762849

File tree

1 file changed

+43
-120
lines changed
  • packages/service-library/src/servicelib/rabbitmq/rpc_interfaces/catalog

1 file changed

+43
-120
lines changed

packages/service-library/src/servicelib/rabbitmq/rpc_interfaces/catalog/services.py

Lines changed: 43 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""RPC client-side for the RPC server at the payments service"""
22

33
import logging
4-
from typing import Any, cast
4+
from typing import cast
55

66
from models_library.api_schemas_catalog import CATALOG_RPC_NAMESPACE
77
from models_library.api_schemas_catalog.services import (
@@ -32,7 +32,16 @@
3232

3333
_logger = logging.getLogger(__name__)
3434

35+
#
36+
# In this interface, the context of the caller is passed in the following arguments:
37+
# - `user_id` is intended for the caller's identifer. Do not add other user_id that is not the callers!.
38+
# - Ideally this could be injected by an authentication layer (as in the rest API)
39+
# but for now we are passing it as an argument.
40+
# - `product_name` is the name of the product at the caller's context as well
41+
#
3542

43+
44+
@validate_call(config={"arbitrary_types_allowed": True})
3645
async def list_services_paginated( # pylint: disable=too-many-arguments
3746
rpc_client: RabbitMQRPCClient,
3847
*,
@@ -48,38 +57,24 @@ async def list_services_paginated( # pylint: disable=too-many-arguments
4857
CatalogForbiddenError: no access-rights to list services
4958
"""
5059

51-
@validate_call()
52-
async def _call(
53-
product_name: ProductName,
54-
user_id: UserID,
55-
limit: PageLimitInt,
56-
offset: PageOffsetInt,
57-
filters: ServiceListFilters | None = None,
58-
):
59-
return await rpc_client.request(
60-
CATALOG_RPC_NAMESPACE,
61-
TypeAdapter(RPCMethodName).validate_python("list_services_paginated"),
62-
product_name=product_name,
63-
user_id=user_id,
64-
limit=limit,
65-
offset=offset,
66-
filters=filters,
67-
timeout_s=40 * RPC_REQUEST_DEFAULT_TIMEOUT_S,
68-
)
69-
70-
result = await _call(
60+
result = await rpc_client.request(
61+
CATALOG_RPC_NAMESPACE,
62+
TypeAdapter(RPCMethodName).validate_python("list_services_paginated"),
7163
product_name=product_name,
7264
user_id=user_id,
7365
limit=limit,
7466
offset=offset,
7567
filters=filters,
68+
timeout_s=40 * RPC_REQUEST_DEFAULT_TIMEOUT_S,
7669
)
70+
7771
assert ( # nosec
7872
TypeAdapter(PageRpc[LatestServiceGet]).validate_python(result) is not None
7973
)
8074
return cast(PageRpc[LatestServiceGet], result)
8175

8276

77+
@validate_call(config={"arbitrary_types_allowed": True})
8378
@log_decorator(_logger, level=logging.DEBUG)
8479
async def get_service(
8580
rpc_client: RabbitMQRPCClient,
@@ -95,34 +90,20 @@ async def get_service(
9590
CatalogItemNotFoundError: service not found in catalog
9691
CatalogForbiddenError: not access rights to read this service
9792
"""
98-
99-
@validate_call()
100-
async def _call(
101-
product_name: ProductName,
102-
user_id: UserID,
103-
service_key: ServiceKey,
104-
service_version: ServiceVersion,
105-
) -> Any:
106-
return await rpc_client.request(
107-
CATALOG_RPC_NAMESPACE,
108-
TypeAdapter(RPCMethodName).validate_python("get_service"),
109-
product_name=product_name,
110-
user_id=user_id,
111-
service_key=service_key,
112-
service_version=service_version,
113-
timeout_s=4 * RPC_REQUEST_DEFAULT_TIMEOUT_S,
114-
)
115-
116-
result = await _call(
93+
result = await rpc_client.request(
94+
CATALOG_RPC_NAMESPACE,
95+
TypeAdapter(RPCMethodName).validate_python("get_service"),
11796
product_name=product_name,
11897
user_id=user_id,
11998
service_key=service_key,
12099
service_version=service_version,
100+
timeout_s=4 * RPC_REQUEST_DEFAULT_TIMEOUT_S,
121101
)
122102
assert TypeAdapter(ServiceGetV2).validate_python(result) is not None # nosec
123103
return cast(ServiceGetV2, result)
124104

125105

106+
@validate_call(config={"arbitrary_types_allowed": True})
126107
@log_decorator(_logger, level=logging.DEBUG)
127108
async def update_service(
128109
rpc_client: RabbitMQRPCClient,
@@ -140,26 +121,9 @@ async def update_service(
140121
CatalogItemNotFoundError: service not found in catalog
141122
CatalogForbiddenError: not access rights to read this service
142123
"""
143-
144-
@validate_call()
145-
async def _call(
146-
product_name: ProductName,
147-
user_id: UserID,
148-
service_key: ServiceKey,
149-
service_version: ServiceVersion,
150-
update: ServiceUpdateV2,
151-
):
152-
return await rpc_client.request(
153-
CATALOG_RPC_NAMESPACE,
154-
TypeAdapter(RPCMethodName).validate_python("update_service"),
155-
product_name=product_name,
156-
user_id=user_id,
157-
service_key=service_key,
158-
service_version=service_version,
159-
update=update,
160-
)
161-
162-
result = await _call(
124+
result = await rpc_client.request(
125+
CATALOG_RPC_NAMESPACE,
126+
TypeAdapter(RPCMethodName).validate_python("update_service"),
163127
product_name=product_name,
164128
user_id=user_id,
165129
service_key=service_key,
@@ -170,6 +134,7 @@ async def _call(
170134
return cast(ServiceGetV2, result)
171135

172136

137+
@validate_call(config={"arbitrary_types_allowed": True})
173138
@log_decorator(_logger, level=logging.DEBUG)
174139
async def check_for_service(
175140
rpc_client: RabbitMQRPCClient,
@@ -180,36 +145,23 @@ async def check_for_service(
180145
service_version: ServiceVersion,
181146
) -> None:
182147
"""
148+
183149
Raises:
184150
ValidationError: on invalid arguments
185151
CatalogItemNotFoundError: service not found in catalog
186152
CatalogForbiddenError: not access rights to read this service
187153
"""
188-
189-
@validate_call()
190-
async def _call(
191-
product_name: ProductName,
192-
user_id: UserID,
193-
service_key: ServiceKey,
194-
service_version: ServiceVersion,
195-
):
196-
return await rpc_client.request(
197-
CATALOG_RPC_NAMESPACE,
198-
TypeAdapter(RPCMethodName).validate_python("check_for_service"),
199-
product_name=product_name,
200-
user_id=user_id,
201-
service_key=service_key,
202-
service_version=service_version,
203-
)
204-
205-
await _call(
154+
await rpc_client.request(
155+
CATALOG_RPC_NAMESPACE,
156+
TypeAdapter(RPCMethodName).validate_python("check_for_service"),
206157
product_name=product_name,
207158
user_id=user_id,
208159
service_key=service_key,
209160
service_version=service_version,
210161
)
211162

212163

164+
@validate_call(config={"arbitrary_types_allowed": True})
213165
@log_decorator(_logger, level=logging.DEBUG)
214166
async def batch_get_my_services(
215167
rpc_client: RabbitMQRPCClient,
@@ -228,27 +180,19 @@ async def batch_get_my_services(
228180
ValidationError: on invalid arguments
229181
CatalogForbiddenError: no access-rights to list services
230182
"""
231-
232-
@validate_call()
233-
async def _call(
234-
product_name: ProductName,
235-
user_id: UserID,
236-
ids: list[tuple[ServiceKey, ServiceVersion]],
237-
):
238-
return await rpc_client.request(
239-
CATALOG_RPC_NAMESPACE,
240-
TypeAdapter(RPCMethodName).validate_python("batch_get_my_services"),
241-
product_name=product_name,
242-
user_id=user_id,
243-
ids=ids,
244-
timeout_s=40 * RPC_REQUEST_DEFAULT_TIMEOUT_S,
245-
)
246-
247-
result = await _call(product_name=product_name, user_id=user_id, ids=ids)
183+
result = await rpc_client.request(
184+
CATALOG_RPC_NAMESPACE,
185+
TypeAdapter(RPCMethodName).validate_python("batch_get_my_services"),
186+
product_name=product_name,
187+
user_id=user_id,
188+
ids=ids,
189+
timeout_s=40 * RPC_REQUEST_DEFAULT_TIMEOUT_S,
190+
)
248191
assert TypeAdapter(list[MyServiceGet]).validate_python(result) is not None # nosec
249192
return cast(list[MyServiceGet], result)
250193

251194

195+
@validate_call(config={"arbitrary_types_allowed": True})
252196
async def list_my_service_history_paginated( # pylint: disable=too-many-arguments
253197
rpc_client: RabbitMQRPCClient,
254198
*,
@@ -260,41 +204,20 @@ async def list_my_service_history_paginated( # pylint: disable=too-many-argumen
260204
filters: ServiceListFilters | None = None,
261205
) -> PageRpcServiceRelease:
262206
"""
207+
263208
Raises:
264209
ValidationError: on invalid arguments
265210
"""
266-
267-
@validate_call()
268-
async def _call(
269-
product_name: ProductName,
270-
user_id: UserID,
271-
service_key: ServiceKey,
272-
limit: PageLimitInt,
273-
offset: PageOffsetInt,
274-
filters: ServiceListFilters | None,
275-
):
276-
return await rpc_client.request(
277-
CATALOG_RPC_NAMESPACE,
278-
TypeAdapter(RPCMethodName).validate_python(
279-
"list_my_service_history_paginated"
280-
),
281-
product_name=product_name,
282-
user_id=user_id,
283-
service_key=service_key,
284-
limit=limit,
285-
offset=offset,
286-
filters=filters,
287-
)
288-
289-
result = await _call(
211+
result = await rpc_client.request(
212+
CATALOG_RPC_NAMESPACE,
213+
TypeAdapter(RPCMethodName).validate_python("list_my_service_history_paginated"),
290214
product_name=product_name,
291215
user_id=user_id,
292216
service_key=service_key,
293217
limit=limit,
294218
offset=offset,
295219
filters=filters,
296220
)
297-
298221
assert ( # nosec
299222
TypeAdapter(PageRpcServiceRelease).validate_python(result) is not None
300223
)

0 commit comments

Comments
 (0)