Skip to content

Commit eeeb62a

Browse files
committed
dataclass
1 parent 981fe5f commit eeeb62a

File tree

7 files changed

+29
-26
lines changed

7 files changed

+29
-26
lines changed

services/web/server/src/simcore_service_webserver/payments/_rpc_invoice.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from servicelib.rabbitmq import RPCRouter
1010

1111
from ..products import products_service
12-
from ..products.models import CreditResultDict
12+
from ..products.models import CreditResult
1313
from ..rabbitmq import get_rabbitmq_rpc_server
1414
from ..users.api import get_user_display_and_id_names, get_user_invoice_address
1515

@@ -24,7 +24,7 @@ async def get_invoice_data(
2424
dollar_amount: Decimal,
2525
product_name: ProductName,
2626
) -> InvoiceDataGet:
27-
credit_result: CreditResultDict = await products_service.get_credit_amount(
27+
credit_result: CreditResult = await products_service.get_credit_amount(
2828
app, dollar_amount=dollar_amount, product_name=product_name
2929
)
3030
product_stripe_info = await products_service.get_product_stripe_info(
@@ -36,7 +36,7 @@ async def get_invoice_data(
3636
user_info = await get_user_display_and_id_names(app, user_id=user_id)
3737

3838
return InvoiceDataGet(
39-
credit_amount=credit_result["credit_amount"],
39+
credit_amount=credit_result.credit_amount,
4040
stripe_price_id=product_stripe_info.stripe_price_id,
4141
stripe_tax_rate_id=product_stripe_info.stripe_tax_rate_id,
4242
user_invoice_address=user_invoice_address,

services/web/server/src/simcore_service_webserver/products/_controller/rpc.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from ...constants import APP_SETTINGS_KEY
1010
from ...rabbitmq import get_rabbitmq_rpc_server, setup_rabbitmq
1111
from .. import _service
12-
from .._models import CreditResultDict
12+
from .._models import CreditResult
1313

1414
router = RPCRouter()
1515

@@ -21,10 +21,10 @@ async def get_credit_amount(
2121
dollar_amount: Decimal,
2222
product_name: ProductName,
2323
) -> CreditResultRpcGet:
24-
credit_result: CreditResultDict = await _service.get_credit_amount(
24+
credit_result: CreditResult = await _service.get_credit_amount(
2525
app, dollar_amount=dollar_amount, product_name=product_name
2626
)
27-
return CreditResultRpcGet.model_validate(credit_result)
27+
return CreditResultRpcGet.model_validate(credit_result, from_attributes=True)
2828

2929

3030
async def _register_rpc_routes_on_startup(app: web.Application):

services/web/server/src/simcore_service_webserver/products/_models.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import logging
22
import re
33
import string
4+
from dataclasses import dataclass
45
from decimal import Decimal
5-
from typing import Annotated, Any, NamedTuple
6+
from typing import Annotated, Any
67

78
from models_library.basic_regex import (
89
PUBLIC_VARIABLE_NAME_RE,
@@ -32,24 +33,26 @@
3233
WebFeedback,
3334
products,
3435
)
35-
from typing_extensions import TypedDict
3636

3737
from ..constants import FRONTEND_APPS_AVAILABLE
3838

3939
_logger = logging.getLogger(__name__)
4040

4141

42-
class CreditResultDict(TypedDict):
42+
@dataclass(frozen=True)
43+
class CreditResult:
4344
product_name: ProductName
4445
credit_amount: Decimal
4546

4647

47-
class ProductStripeInfo(NamedTuple):
48+
@dataclass(frozen=True)
49+
class ProductStripeInfo:
4850
stripe_price_id: StripePriceID
4951
stripe_tax_rate_id: StripeTaxRateID
5052

5153

52-
class PaymentFieldsTuple(NamedTuple):
54+
@dataclass(frozen=True)
55+
class PaymentFields:
5356
enabled: bool
5457
credits_per_usd: Decimal | None
5558
min_payment_amount_usd: Decimal | None

services/web/server/src/simcore_service_webserver/products/_repository.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
from ..constants import FRONTEND_APPS_AVAILABLE
2929
from ..db.base_repository import BaseRepository
30-
from ._models import PaymentFieldsTuple, Product, ProductStripeInfo
30+
from ._models import PaymentFields, Product, ProductStripeInfo
3131

3232
_logger = logging.getLogger(__name__)
3333

@@ -60,7 +60,7 @@
6060
)
6161

6262

63-
def _to_domain(products_row: Row, payments: PaymentFieldsTuple) -> Product:
63+
def _to_domain(products_row: Row, payments: PaymentFields) -> Product:
6464
return Product(
6565
**products_row._asdict(),
6666
is_payment_enabled=payments.enabled,
@@ -70,12 +70,12 @@ def _to_domain(products_row: Row, payments: PaymentFieldsTuple) -> Product:
7070

7171
async def _get_product_payment_fields(
7272
conn: AsyncConnection, product_name: ProductName
73-
) -> PaymentFieldsTuple:
73+
) -> PaymentFields:
7474
price_info = await get_product_latest_price_info_or_none(
7575
conn, product_name=product_name
7676
)
7777
if price_info is None or price_info.usd_per_credit == 0:
78-
return PaymentFieldsTuple(
78+
return PaymentFields(
7979
enabled=False,
8080
credits_per_usd=None,
8181
min_payment_amount_usd=None,
@@ -84,7 +84,7 @@ async def _get_product_payment_fields(
8484
assert price_info.usd_per_credit > 0 # nosec
8585
assert price_info.min_payment_amount_usd > 0 # nosec
8686

87-
return PaymentFieldsTuple(
87+
return PaymentFields(
8888
enabled=True,
8989
credits_per_usd=Decimal(1 / price_info.usd_per_credit).quantize(
9090
QUANTIZE_EXP_ARG

services/web/server/src/simcore_service_webserver/products/_service.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from simcore_postgres_database.utils_products_prices import ProductPriceInfo
1010

1111
from ..constants import APP_PRODUCTS_KEY
12-
from ._models import CreditResultDict, ProductStripeInfo
12+
from ._models import CreditResult, ProductStripeInfo
1313
from ._repository import ProductRepository
1414
from .errors import (
1515
BelowMinimumPaymentError,
@@ -77,7 +77,7 @@ async def get_credit_amount(
7777
*,
7878
dollar_amount: Decimal,
7979
product_name: ProductName,
80-
) -> CreditResultDict:
80+
) -> CreditResult:
8181
"""For provided dollars and product gets credit amount.
8282
8383
NOTE: Contrary to other product api functions (e.g. get_current_product) this function
@@ -105,7 +105,7 @@ async def get_credit_amount(
105105
)
106106

107107
credit_amount = dollar_amount / price_info.usd_per_credit
108-
return CreditResultDict(product_name=product_name, credit_amount=credit_amount)
108+
return CreditResult(product_name=product_name, credit_amount=credit_amount)
109109

110110

111111
async def get_product_stripe_info(

services/web/server/src/simcore_service_webserver/products/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from models_library.products import ProductName
22

3-
from ._models import CreditResultDict, Product, ProductStripeInfo
3+
from ._models import CreditResult, Product, ProductStripeInfo
44

55
__all__: tuple[str, ...] = (
6-
"CreditResultDict",
6+
"CreditResult",
77
"Product",
88
"ProductName",
99
"ProductStripeInfo",

services/web/server/src/simcore_service_webserver/wallets/_payments_handlers.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
)
2424
from servicelib.logging_utils import get_log_record_extra, log_context
2525
from servicelib.utils import fire_and_forget_task
26-
from simcore_service_webserver.products._models import CreditResultDict
26+
from simcore_service_webserver.products._models import CreditResult
2727

2828
from .._meta import API_VTAG as VTAG
2929
from ..login.decorators import login_required
@@ -79,7 +79,7 @@ async def _create_payment(request: web.Request):
7979
log_duration=True,
8080
extra=get_log_record_extra(user_id=req_ctx.user_id),
8181
):
82-
credit_result: CreditResultDict = await products_service.get_credit_amount(
82+
credit_result: CreditResult = await products_service.get_credit_amount(
8383
request.app,
8484
dollar_amount=body_params.price_dollars,
8585
product_name=req_ctx.product_name,
@@ -90,7 +90,7 @@ async def _create_payment(request: web.Request):
9090
user_id=req_ctx.user_id,
9191
product_name=req_ctx.product_name,
9292
wallet_id=wallet_id,
93-
osparc_credits=credit_result["credit_amount"],
93+
osparc_credits=credit_result.credit_amount,
9494
comment=body_params.comment,
9595
price_dollars=body_params.price_dollars,
9696
)
@@ -351,7 +351,7 @@ async def _pay_with_payment_method(request: web.Request):
351351
log_duration=True,
352352
extra=get_log_record_extra(user_id=req_ctx.user_id),
353353
):
354-
credit_result: CreditResultDict = await products_service.get_credit_amount(
354+
credit_result: CreditResult = await products_service.get_credit_amount(
355355
request.app,
356356
dollar_amount=body_params.price_dollars,
357357
product_name=req_ctx.product_name,
@@ -363,7 +363,7 @@ async def _pay_with_payment_method(request: web.Request):
363363
product_name=req_ctx.product_name,
364364
wallet_id=wallet_id,
365365
payment_method_id=path_params.payment_method_id,
366-
osparc_credits=credit_result["credit_amount"],
366+
osparc_credits=credit_result.credit_amount,
367367
comment=body_params.comment,
368368
price_dollars=body_params.price_dollars,
369369
)

0 commit comments

Comments
 (0)