Skip to content

Commit f863ee1

Browse files
add host field
1 parent 8178ae4 commit f863ee1

File tree

6 files changed

+62
-1
lines changed

6 files changed

+62
-1
lines changed

packages/models-library/src/models_library/api_schemas_webserver/products.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,28 @@ def _update_json_schema_extra(schema: JsonDict) -> None:
8989
)
9090

9191

92+
class ProductHostRpcGet(BaseModel):
93+
product_name: ProductName
94+
host: str
95+
96+
@staticmethod
97+
def _update_json_schema_extra(schema: JsonDict) -> None:
98+
schema.update(
99+
{
100+
"examples": [
101+
{
102+
"product_name": "osparc",
103+
"credit_amount": "osparc.io",
104+
},
105+
]
106+
}
107+
)
108+
109+
model_config = ConfigDict(
110+
json_schema_extra=_update_json_schema_extra,
111+
)
112+
113+
92114
class ProductTemplateGet(OutputSchema):
93115
id_: Annotated[IDStr, Field(alias="id")]
94116
content: str

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
from aiohttp import web
44
from models_library.api_schemas_webserver import WEBSERVER_RPC_NAMESPACE
5-
from models_library.api_schemas_webserver.products import CreditResultRpcGet
5+
from models_library.api_schemas_webserver.products import (
6+
CreditResultRpcGet,
7+
ProductHostRpcGet,
8+
)
69
from models_library.products import ProductName
710
from servicelib.rabbitmq import RPCRouter
811

@@ -27,6 +30,16 @@ async def get_credit_amount(
2730
return CreditResultRpcGet.model_validate(credit_result, from_attributes=True)
2831

2932

33+
@router.expose()
34+
async def get_product_host(
35+
app: web.Application,
36+
*,
37+
product_name: ProductName,
38+
) -> ProductHostRpcGet:
39+
host: str = await _service.get_product_host(app, product_name=product_name)
40+
return ProductHostRpcGet(product_name=product_name, host=host)
41+
42+
3043
async def _register_rpc_routes_on_startup(app: web.Application):
3144
rpc_server = get_rabbitmq_rpc_server(app)
3245
await rpc_server.register_router(router, WEBSERVER_RPC_NAMESPACE, app)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ class Product(BaseModel):
8787
re.Pattern, BeforeValidator(str.strip), Field(..., description="Host regex")
8888
]
8989

90+
host: Annotated[str, BeforeValidator(str.strip), Field(..., description="Host")]
91+
9092
support_email: Annotated[
9193
LowerCaseEmailStr,
9294
Field(

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
products.c.display_name,
4343
products.c.short_name,
4444
products.c.host_regex,
45+
products.c.host,
4546
products.c.support_email,
4647
products.c.product_owners_email,
4748
products.c.twilio_messaging_sid,
@@ -215,6 +216,16 @@ async def get_product_ui(
215216
row = result.one_or_none()
216217
return dict(**row.ui) if row else None
217218

219+
async def get_product_host(
220+
self, product_name: ProductName, connection: AsyncConnection | None = None
221+
) -> str | None:
222+
query = sa.select(products.c.host).where(products.c.name == product_name)
223+
224+
async with pass_or_acquire_connection(self.engine, connection) as conn:
225+
result = await conn.execute(query)
226+
row = result.one_or_none()
227+
return f"{row.host}" if row else None
228+
218229
async def auto_create_products_groups(
219230
self,
220231
connection: AsyncConnection | None = None,

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from .errors import (
1515
BelowMinimumPaymentError,
1616
MissingStripeConfigError,
17+
ProductHostNotFoundError,
1718
ProductNotFoundError,
1819
ProductPriceNotDefinedError,
1920
ProductTemplateNotFoundError,
@@ -136,6 +137,14 @@ async def get_template_content(app: web.Application, *, template_name: str):
136137
return content
137138

138139

140+
async def get_product_host(app: web.Application, *, product_name) -> str:
141+
repo = ProductRepository.create_from_app(app)
142+
host = await repo.get_product_host(product_name)
143+
if not host:
144+
raise ProductHostNotFoundError(product_name=product_name)
145+
return host
146+
147+
139148
async def auto_create_products_groups(
140149
app: web.Application,
141150
) -> dict[ProductName, GroupID]:

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ class ProductTemplateNotFoundError(ProductError):
2424
msg_template = "Missing template {template_name} for product"
2525

2626

27+
class ProductHostNotFoundError(ProductError):
28+
msg_template = "Missing host for product {product_name}"
29+
30+
2731
class MissingStripeConfigError(ProductError):
2832
msg_template = (
2933
"Missing product stripe for product {product_name}.\n"

0 commit comments

Comments
 (0)