From 8a793481b9a4a1451cd6cbbd8e8c206073d1eef9 Mon Sep 17 00:00:00 2001 From: Mads Bisgaard Date: Wed, 18 Dec 2024 21:45:09 +0100 Subject: [PATCH] expose get_available_licensed_items_for_wallet in api-server --- .../api/routes/wallets.py | 39 ++++++++++++++++++- .../services_rpc/wb_api_server.py | 27 ++++++++++++- 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/services/api-server/src/simcore_service_api_server/api/routes/wallets.py b/services/api-server/src/simcore_service_api_server/api/routes/wallets.py index ca34726eae3..5661b14057c 100644 --- a/services/api-server/src/simcore_service_api_server/api/routes/wallets.py +++ b/services/api-server/src/simcore_service_api_server/api/routes/wallets.py @@ -2,10 +2,24 @@ from typing import Annotated, Any from fastapi import APIRouter, Depends, status +from fastapi_pagination import Page +from pydantic import PositiveInt +from simcore_service_api_server.api.dependencies.authentication import ( + get_current_user_id, + get_product_name, +) +from simcore_service_api_server.api.dependencies.webserver_rpc import ( + get_wb_api_rpc_client, +) +from simcore_service_api_server.models.pagination import PaginationParams +from simcore_service_api_server.services_rpc.wb_api_server import WbApiRpcClient from ...exceptions.service_errors_utils import DEFAULT_BACKEND_SERVICE_STATUS_CODES from ...models.schemas.errors import ErrorGet -from ...models.schemas.model_adapter import WalletGetWithAvailableCreditsLegacy +from ...models.schemas.model_adapter import ( + LicensedItemGet, + WalletGetWithAvailableCreditsLegacy, +) from ..dependencies.webserver_http import AuthSession, get_webserver_session from ._constants import FMSG_CHANGELOG_NEW_IN_VERSION @@ -49,3 +63,26 @@ async def get_wallet( webserver_api: Annotated[AuthSession, Depends(get_webserver_session)], ): return await webserver_api.get_wallet(wallet_id=wallet_id) + + +@router.get( + "/{wallet_id}/licensed-items", + response_model=Page[LicensedItemGet], + status_code=status.HTTP_200_OK, + responses=WALLET_STATUS_CODES, + description="Get all available licensed items for a given wallet", + include_in_schema=False, +) +async def get_available_licensed_items_for_wallet( + wallet_id: int, + page_params: Annotated[PaginationParams, Depends()], + web_api_rpc: Annotated[WbApiRpcClient, Depends(get_wb_api_rpc_client)], + product_name: Annotated[str, Depends(get_product_name)], + user_id: Annotated[PositiveInt, Depends(get_current_user_id)], +): + return await web_api_rpc.get_available_licensed_items_for_wallet( + product_name=product_name, + wallet_id=wallet_id, + user_id=user_id, + page_params=page_params, + ) diff --git a/services/api-server/src/simcore_service_api_server/services_rpc/wb_api_server.py b/services/api-server/src/simcore_service_api_server/services_rpc/wb_api_server.py index fa5529fde68..701452302a6 100644 --- a/services/api-server/src/simcore_service_api_server/services_rpc/wb_api_server.py +++ b/services/api-server/src/simcore_service_api_server/services_rpc/wb_api_server.py @@ -5,7 +5,12 @@ from fastapi import FastAPI from fastapi_pagination import Page, create_page from models_library.api_schemas_webserver.licensed_items import LicensedItemGetPage +from models_library.users import UserID +from models_library.wallets import WalletID from servicelib.rabbitmq._client_rpc import RabbitMQRPCClient +from servicelib.rabbitmq.rpc_interfaces.webserver.licenses.licensed_items import ( + get_available_licensed_items_for_wallet as _get_available_licensed_items_for_wallet, +) from servicelib.rabbitmq.rpc_interfaces.webserver.licenses.licensed_items import ( get_licensed_items as _get_licensed_items, ) @@ -44,7 +49,7 @@ def _create_licensed_items_get_page( @_exception_mapper(rpc_exception_map={}) async def get_licensed_items( - self, product_name: str, page_params: PaginationParams + self, *, product_name: str, page_params: PaginationParams ) -> Page[LicensedItemGet]: licensed_items_page = await _get_licensed_items( rabbitmq_rpc_client=self._client, @@ -56,6 +61,26 @@ async def get_licensed_items( licensed_items_page=licensed_items_page, page_params=page_params ) + @_exception_mapper(rpc_exception_map={}) + async def get_available_licensed_items_for_wallet( + self, + product_name: str, + wallet_id: WalletID, + user_id: UserID, + page_params: PaginationParams, + ) -> Page[LicensedItemGet]: + licensed_items_page = await _get_available_licensed_items_for_wallet( + rabbitmq_rpc_client=self._client, + product_name=product_name, + wallet_id=wallet_id, + user_id=user_id, + offset=page_params.offset, + limit=page_params.limit, + ) + return self._create_licensed_items_get_page( + licensed_items_page=licensed_items_page, page_params=page_params + ) + def setup(app: FastAPI, rabbitmq_rmp_client: RabbitMQRPCClient): app.state.wb_api_rpc_client = WbApiRpcClient(_client=rabbitmq_rmp_client)