Skip to content

Commit 8b3ab57

Browse files
committed
client product
1 parent b61220f commit 8b3ab57

File tree

2 files changed

+50
-15
lines changed
  • packages/service-library/src/servicelib/rabbitmq/rpc_interfaces/webserver
  • services/web/server/tests/unit/with_dbs/04/products

2 files changed

+50
-15
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import logging
2+
from decimal import Decimal
3+
4+
from models_library.api_schemas_webserver import WEBSERVER_RPC_NAMESPACE
5+
from models_library.api_schemas_webserver.products import CreditResultRpcGet
6+
from models_library.products import ProductName
7+
from models_library.rabbitmq_basic_types import RPCMethodName
8+
from pydantic import TypeAdapter
9+
from servicelib.logging_utils import log_decorator
10+
from servicelib.rabbitmq import RabbitMQRPCClient
11+
12+
_logger = logging.getLogger(__name__)
13+
14+
15+
@log_decorator(_logger, level=logging.DEBUG)
16+
async def get_credit_amount(
17+
rabbitmq_rpc_client: RabbitMQRPCClient,
18+
*,
19+
dollar_amount: Decimal,
20+
product_name: ProductName,
21+
) -> CreditResultRpcGet:
22+
"""
23+
Get credit amount for a specific dollar amount and product.
24+
25+
Args:
26+
rabbitmq_rpc_client: RPC client to communicate with the webserver
27+
dollar_amount: The amount in dollars to be converted to credits
28+
product_name: The product for which to calculate the credit amount
29+
30+
Returns:
31+
Credit result information containing the credit amount
32+
"""
33+
result: CreditResultRpcGet = await rabbitmq_rpc_client.request(
34+
WEBSERVER_RPC_NAMESPACE,
35+
TypeAdapter(RPCMethodName).validate_python("get_credit_amount"),
36+
dollar_amount=dollar_amount,
37+
product_name=product_name,
38+
)
39+
assert isinstance(result, CreditResultRpcGet) # nosec
40+
return result

services/web/server/tests/unit/with_dbs/04/products/test_products_rpc.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,13 @@
77
from decimal import Decimal
88

99
import pytest
10-
from models_library.api_schemas_webserver import WEBSERVER_RPC_NAMESPACE
11-
from models_library.api_schemas_webserver.products import CreditResultRpcGet
1210
from models_library.products import ProductName
13-
from models_library.rabbitmq_basic_types import RPCMethodName
14-
from pydantic import TypeAdapter
1511
from pytest_mock import MockerFixture
1612
from pytest_simcore.helpers.monkeypatch_envs import setenvs_from_dict
1713
from pytest_simcore.helpers.typing_env import EnvVarsDict
1814
from pytest_simcore.helpers.webserver_login import UserInfoDict
1915
from servicelib.rabbitmq import RabbitMQRPCClient, RPCServerError
16+
from servicelib.rabbitmq.rpc_interfaces.webserver.products import get_credit_amount
2017
from settings_library.rabbit import RabbitSettings
2118
from simcore_postgres_database.models.users import UserRole
2219
from simcore_service_webserver.application_settings import ApplicationSettings
@@ -69,28 +66,26 @@ async def test_get_credit_amount(
6966
osparc_product_name: ProductName,
7067
logged_user: UserInfoDict,
7168
):
72-
result = await rpc_client.request(
73-
WEBSERVER_RPC_NAMESPACE,
74-
TypeAdapter(RPCMethodName).validate_python("get_credit_amount"),
69+
# Using the new client function for s4l
70+
credit_result = await get_credit_amount(
71+
rpc_client,
7572
dollar_amount=Decimal(900),
7673
product_name="s4l",
7774
)
78-
credit_result = CreditResultRpcGet.model_validate(result)
7975
assert credit_result.credit_amount == 100
8076

81-
result = await rpc_client.request(
82-
WEBSERVER_RPC_NAMESPACE,
83-
TypeAdapter(RPCMethodName).validate_python("get_credit_amount"),
77+
# Using the new client function for tis
78+
credit_result = await get_credit_amount(
79+
rpc_client,
8480
dollar_amount=Decimal(900),
8581
product_name="tis",
8682
)
87-
credit_result = CreditResultRpcGet.model_validate(result)
8883
assert credit_result.credit_amount == 180
8984

85+
# Testing the error case
9086
with pytest.raises(RPCServerError) as exc_info:
91-
await rpc_client.request(
92-
WEBSERVER_RPC_NAMESPACE,
93-
TypeAdapter(RPCMethodName).validate_python("get_credit_amount"),
87+
await get_credit_amount(
88+
rpc_client,
9489
dollar_amount=Decimal(900),
9590
product_name="osparc",
9691
)

0 commit comments

Comments
 (0)