Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🎨 expose listing license checkouts for frontend #6987

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
57 changes: 57 additions & 0 deletions api/specs/web-server/_licensed_items_checkouts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
""" Helper script to generate OAS automatically
"""

# pylint: disable=redefined-outer-name
# pylint: disable=unused-argument
# pylint: disable=unused-variable
# pylint: disable=too-many-arguments

from typing import Annotated

from _common import as_query
from fastapi import APIRouter, Depends
from models_library.api_schemas_webserver.licensed_items_purchases import (
LicensedItemPurchaseGet,
)
from models_library.generics import Envelope
from models_library.rest_error import EnvelopedError
from models_library.rest_pagination import Page
from simcore_service_webserver._meta import API_VTAG
from simcore_service_webserver.licenses._exceptions_handlers import _TO_HTTP_ERROR_MAP
from simcore_service_webserver.licenses._licensed_items_checkouts_models import (
LicensedItemCheckoutPathParams,
LicensedItemsCheckoutsListQueryParams,
)
from simcore_service_webserver.wallets._handlers import WalletsPathParams

router = APIRouter(
prefix=f"/{API_VTAG}",
tags=[
"licenses",
],
responses={
i.status_code: {"model": EnvelopedError} for i in _TO_HTTP_ERROR_MAP.values()
},
)


@router.get(
"/wallets/{wallet_id}/licensed-items-checkouts",
response_model=Page[LicensedItemPurchaseGet],
tags=["wallets"],
)
async def list_licensed_item_checkouts_for_wallet(
_path: Annotated[WalletsPathParams, Depends()],
_query: Annotated[as_query(LicensedItemsCheckoutsListQueryParams), Depends()],
):
...


@router.get(
"/licensed-items-checkouts/{licensed_item_checkout_id}",
response_model=Envelope[LicensedItemPurchaseGet],
)
async def get_licensed_item_checkout(
_path: Annotated[LicensedItemCheckoutPathParams, Depends()],
):
...
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime
from typing import NamedTuple

from pydantic import PositiveInt
from pydantic import BaseModel, PositiveInt

from ..licensed_items import LicensedItemID
from ..products import ProductName
Expand All @@ -10,8 +10,29 @@
from ..wallets import WalletID
from ._base import OutputSchema

# RPC

class LicensedItemCheckoutGet(OutputSchema):

class LicensedItemCheckoutRpcGet(BaseModel):
licensed_item_checkout_id: LicensedItemCheckoutID
licensed_item_id: LicensedItemID
wallet_id: WalletID
user_id: UserID
product_name: ProductName
started_at: datetime
stopped_at: datetime | None
num_of_seats: int


class LicensedItemCheckoutRpcGetPage(NamedTuple):
items: list[LicensedItemCheckoutRpcGet]
total: PositiveInt


# Rest


class LicensedItemCheckoutRestGet(OutputSchema):
licensed_item_checkout_id: LicensedItemCheckoutID
licensed_item_id: LicensedItemID
wallet_id: WalletID
Expand All @@ -22,6 +43,6 @@ class LicensedItemCheckoutGet(OutputSchema):
num_of_seats: int


class LicensedItemUsageGetPage(NamedTuple):
items: list[LicensedItemCheckoutGet]
class LicensedItemCheckoutRestGetPage(NamedTuple):
items: list[LicensedItemCheckoutRestGet]
total: PositiveInt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
LicensedItemGetPage,
)
from models_library.api_schemas_webserver.licensed_items_checkouts import (
LicensedItemCheckoutGet,
LicensedItemCheckoutRpcGet,
)
from models_library.licensed_items import LicensedItemID
from models_library.products import ProductName
Expand Down Expand Up @@ -78,7 +78,7 @@ async def checkout_licensed_item_for_wallet(
licensed_item_id: LicensedItemID,
num_of_seats: int,
service_run_id: ServiceRunID,
) -> LicensedItemCheckoutGet:
) -> LicensedItemCheckoutRpcGet:
result = await rabbitmq_rpc_client.request(
WEBSERVER_RPC_NAMESPACE,
TypeAdapter(RPCMethodName).validate_python("checkout_licensed_item_for_wallet"),
Expand All @@ -89,7 +89,7 @@ async def checkout_licensed_item_for_wallet(
num_of_seats=num_of_seats,
service_run_id=service_run_id,
)
assert isinstance(result, LicensedItemCheckoutGet) # nosec
assert isinstance(result, LicensedItemCheckoutRpcGet) # nosec
return result


Expand All @@ -100,13 +100,13 @@ async def release_licensed_item_for_wallet(
product_name: ProductName,
user_id: UserID,
licensed_item_checkout_id: LicensedItemCheckoutID,
) -> LicensedItemCheckoutGet:
) -> LicensedItemCheckoutRpcGet:
result = await rabbitmq_rpc_client.request(
WEBSERVER_RPC_NAMESPACE,
TypeAdapter(RPCMethodName).validate_python("release_licensed_item_for_wallet"),
product_name=product_name,
user_id=user_id,
licensed_item_checkout_id=licensed_item_checkout_id,
)
assert isinstance(result, LicensedItemCheckoutGet) # nosec
assert isinstance(result, LicensedItemCheckoutRpcGet) # nosec
return result
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We started renaming these as _service instead of _api, rithg?

Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
from models_library.api_schemas_resource_usage_tracker import (
licensed_items_checkouts as rut_licensed_items_checkouts,
)
from models_library.api_schemas_webserver import (
licensed_items_checkouts as webserver_licensed_items_checkouts,
)
from models_library.licensed_items import LicensedItemID
from models_library.products import ProductName
from models_library.resource_tracker_licensed_items_checkouts import (
LicensedItemCheckoutID,
)
from models_library.rest_ordering import OrderBy
from models_library.services_types import ServiceRunID
from models_library.users import UserID
from models_library.wallets import WalletID
Expand All @@ -20,6 +18,94 @@
from ..rabbitmq import get_rabbitmq_rpc_client
from ..users.api import get_user
from ..wallets.api import get_wallet_by_user
from ._licensed_items_checkouts_models import (
LicensedItemCheckoutGet,
LicensedItemCheckoutGetPage,
)


async def list_licensed_items_checkouts_for_wallet(
app: web.Application,
*,
# access context
product_name: ProductName,
user_id: UserID,
wallet_id: WalletID,
offset: int,
limit: int,
order_by: OrderBy,
) -> LicensedItemCheckoutGetPage:
# Check whether user has access to the wallet
await get_wallet_by_user(
app,
user_id=user_id,
wallet_id=wallet_id,
product_name=product_name,
)

rpc_client = get_rabbitmq_rpc_client(app)

result = await licensed_items_checkouts.get_licensed_items_checkouts_page(
rpc_client,
product_name=product_name,
filter_wallet_id=wallet_id,
offset=offset,
limit=limit,
order_by=order_by,
)

return LicensedItemCheckoutGetPage(
total=result.total,
items=[
LicensedItemCheckoutGet.model_construct(
licensed_item_checkout_id=checkout_item.licensed_item_checkout_id,
licensed_item_id=checkout_item.licensed_item_id,
wallet_id=checkout_item.wallet_id,
user_id=checkout_item.user_id,
product_name=checkout_item.product_name,
started_at=checkout_item.started_at,
stopped_at=checkout_item.stopped_at,
num_of_seats=checkout_item.num_of_seats,
)
for checkout_item in result.items
],
)


async def get_licensed_item_checkout(
app: web.Application,
*,
# access context
product_name: ProductName,
user_id: UserID,
licensed_item_checkout_id: LicensedItemCheckoutID,
) -> LicensedItemCheckoutGet:
rpc_client = get_rabbitmq_rpc_client(app)

checkout_item = await licensed_items_checkouts.get_licensed_item_checkout(
rpc_client,
product_name=product_name,
licensed_item_checkout_id=licensed_item_checkout_id,
)

# Check whether user has access to the wallet
await get_wallet_by_user(
app,
user_id=user_id,
wallet_id=checkout_item.wallet_id,
product_name=product_name,
)

return LicensedItemCheckoutGet.model_construct(
licensed_item_checkout_id=checkout_item.licensed_item_checkout_id,
licensed_item_id=checkout_item.licensed_item_id,
wallet_id=checkout_item.wallet_id,
user_id=checkout_item.user_id,
product_name=checkout_item.product_name,
started_at=checkout_item.started_at,
stopped_at=checkout_item.stopped_at,
num_of_seats=checkout_item.num_of_seats,
)


async def checkout_licensed_item_for_wallet(
Expand All @@ -33,7 +119,7 @@ async def checkout_licensed_item_for_wallet(
licensed_item_id: LicensedItemID,
num_of_seats: int,
service_run_id: ServiceRunID,
) -> webserver_licensed_items_checkouts.LicensedItemCheckoutGet:
) -> LicensedItemCheckoutGet:
# Check whether user has access to the wallet
await get_wallet_by_user(
app,
Expand All @@ -58,7 +144,7 @@ async def checkout_licensed_item_for_wallet(
)
)

return webserver_licensed_items_checkouts.LicensedItemCheckoutGet(
return LicensedItemCheckoutGet.model_construct(
licensed_item_checkout_id=licensed_item_get.licensed_item_checkout_id,
licensed_item_id=licensed_item_get.licensed_item_id,
wallet_id=licensed_item_get.wallet_id,
Expand All @@ -78,7 +164,7 @@ async def release_licensed_item_for_wallet(
user_id: UserID,
# release args
licensed_item_checkout_id: LicensedItemCheckoutID,
) -> webserver_licensed_items_checkouts.LicensedItemCheckoutGet:
) -> LicensedItemCheckoutGet:
rpc_client = get_rabbitmq_rpc_client(app)

checkout_item = await licensed_items_checkouts.get_licensed_item_checkout(
Expand All @@ -103,7 +189,7 @@ async def release_licensed_item_for_wallet(
)
)

return webserver_licensed_items_checkouts.LicensedItemCheckoutGet(
return LicensedItemCheckoutGet.model_construct(
licensed_item_checkout_id=licensed_item_get.licensed_item_checkout_id,
licensed_item_id=licensed_item_get.licensed_item_id,
wallet_id=licensed_item_get.wallet_id,
Expand Down
Loading
Loading