From 97f8d7d5838459a6071b488e03bee7c3609101d8 Mon Sep 17 00:00:00 2001 From: Mads Bisgaard Date: Tue, 7 Jan 2025 15:51:41 +0100 Subject: [PATCH] add rpc client for resource usage tracker --- .../services_rpc/resource_usage_tracket.py | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 services/api-server/src/simcore_service_api_server/services_rpc/resource_usage_tracket.py diff --git a/services/api-server/src/simcore_service_api_server/services_rpc/resource_usage_tracket.py b/services/api-server/src/simcore_service_api_server/services_rpc/resource_usage_tracket.py new file mode 100644 index 00000000000..c58b4071f56 --- /dev/null +++ b/services/api-server/src/simcore_service_api_server/services_rpc/resource_usage_tracket.py @@ -0,0 +1,49 @@ +from dataclasses import dataclass +from functools import partial + +from fastapi import FastAPI +from models_library.resource_tracker_licensed_items_checkouts import ( + LicensedItemCheckoutID, +) +from servicelib.rabbitmq._client_rpc import RabbitMQRPCClient +from servicelib.rabbitmq.rpc_interfaces.resource_usage_tracker.licensed_items_checkouts import ( + get_licensed_item_checkout as _get_licensed_item_checkout, +) + +from ..exceptions.service_errors_utils import service_exception_mapper +from ..models.schemas.model_adapter import LicensedItemCheckoutGet + +_exception_mapper = partial( + service_exception_mapper, service_name="ResourceUsageTracker" +) + + +@dataclass +class ResourceUsageTrackerClient: + _client: RabbitMQRPCClient + + @_exception_mapper(rpc_exception_map={}) + async def get_licensed_item_checkout( + self, *, product_name: str, licensed_item_checkout_id: LicensedItemCheckoutID + ) -> LicensedItemCheckoutGet: + _licensed_item_checkout = await _get_licensed_item_checkout( + rabbitmq_rpc_client=self._client, + product_name=product_name, + licensed_item_checkout_id=licensed_item_checkout_id, + ) + return LicensedItemCheckoutGet( + licensed_item_checkout_id=_licensed_item_checkout.licensed_item_checkout_id, + licensed_item_id=_licensed_item_checkout.licensed_item_id, + wallet_id=_licensed_item_checkout.wallet_id, + user_id=_licensed_item_checkout.user_id, + product_name=_licensed_item_checkout.product_name, + started_at=_licensed_item_checkout.started_at, + stopped_at=_licensed_item_checkout.stopped_at, + num_of_seats=_licensed_item_checkout.num_of_seats, + ) + + +def setup(app: FastAPI, rabbitmq_rmp_client: RabbitMQRPCClient): + app.state.wb_api_rpc_client = ResourceUsageTrackerClient( + _client=rabbitmq_rmp_client + )