Skip to content

Commit

Permalink
✨ Connects webserver and payments service (#4886)
Browse files Browse the repository at this point in the history
  • Loading branch information
pcrespov authored Oct 19, 2023
1 parent b02f9f8 commit 4e833ff
Show file tree
Hide file tree
Showing 21 changed files with 303 additions and 328 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from typing import Final

from pydantic import parse_obj_as

from ..rabbitmq_basic_types import RPCNamespace

PAYMENTS_RPC_NAMESPACE: Final[RPCNamespace] = parse_obj_as(RPCNamespace, "payments")
21 changes: 21 additions & 0 deletions packages/models-library/src/models_library/rabbitmq_basic_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import re
from typing import Final

from pydantic import ConstrainedStr, parse_obj_as

REGEX_RABBIT_QUEUE_ALLOWED_SYMBOLS: Final[str] = r"^[\w\-\.]*$"


class RPCNamespace(ConstrainedStr):
min_length: int = 1
max_length: int = 252
regex: re.Pattern[str] | None = re.compile(REGEX_RABBIT_QUEUE_ALLOWED_SYMBOLS)

@classmethod
def from_entries(cls, entries: dict[str, str]) -> "RPCNamespace":
"""
Given a list of entries creates a namespace to be used in declaring the rabbitmq queue.
Keeping this to a predefined length
"""
composed_string = "-".join(f"{k}_{v}" for k, v in sorted(entries.items()))
return parse_obj_as(cls, composed_string)
4 changes: 3 additions & 1 deletion packages/service-library/src/servicelib/rabbitmq/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from models_library.rabbitmq_basic_types import RPCNamespace

from ._client import RabbitMQClient
from ._client_rpc import RabbitMQRPCClient
from ._constants import BIND_TO_ALL_TOPICS
Expand All @@ -6,7 +8,7 @@
RPCNotInitializedError,
RPCServerError,
)
from ._models import RPCMethodName, RPCNamespace
from ._models import RPCMethodName
from ._rpc_router import RPCRouter
from ._utils import wait_till_rabbitmq_responsive

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
from typing import Any

import aio_pika
from models_library.rabbitmq_basic_types import RPCNamespace
from pydantic import PositiveInt
from settings_library.rabbit import RabbitSettings

from ..logging_utils import log_context
from ._client_base import RabbitMQClientBase
from ._errors import RemoteMethodNotRegisteredError, RPCNotInitializedError
from ._models import RPCMethodName, RPCNamespace, RPCNamespacedMethodName
from ._models import RPCMethodName, RPCNamespacedMethodName
from ._rpc_router import RPCRouter
from ._utils import get_rabbitmq_client_unique_name

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from typing import Final

BIND_TO_ALL_TOPICS: Final[str] = "#"
REGEX_RABBIT_QUEUE_ALLOWED_SYMBOLS: Final[str] = r"^[\w\-\.]*$"
21 changes: 4 additions & 17 deletions packages/service-library/src/servicelib/rabbitmq/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
from collections.abc import Awaitable, Callable
from typing import Any, Protocol

from models_library.rabbitmq_basic_types import (
REGEX_RABBIT_QUEUE_ALLOWED_SYMBOLS,
RPCNamespace,
)
from pydantic import ConstrainedStr, parse_obj_as

from ._constants import REGEX_RABBIT_QUEUE_ALLOWED_SYMBOLS

MessageHandler = Callable[[Any], Awaitable[bool]]


Expand All @@ -23,21 +25,6 @@ class RPCMethodName(ConstrainedStr):
regex: re.Pattern[str] | None = re.compile(REGEX_RABBIT_QUEUE_ALLOWED_SYMBOLS)


class RPCNamespace(ConstrainedStr):
min_length: int = 1
max_length: int = 252
regex: re.Pattern[str] | None = re.compile(REGEX_RABBIT_QUEUE_ALLOWED_SYMBOLS)

@classmethod
def from_entries(cls, entries: dict[str, str]) -> "RPCNamespace":
"""
Given a list of entries creates a namespace to be used in declaring the rabbitmq queue.
Keeping this to a predefined length
"""
composed_string = "-".join(f"{k}_{v}" for k, v in sorted(entries.items()))
return parse_obj_as(cls, composed_string)


class RPCNamespacedMethodName(ConstrainedStr):
min_length: int = 1
max_length: int = 255
Expand Down
15 changes: 4 additions & 11 deletions services/payments/src/simcore_service_payments/api/rpc/routes.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
import logging
from typing import Final

from fastapi import FastAPI
from pydantic import parse_obj_as
from servicelib.rabbitmq import RPCNamespace
from models_library.api_schemas_payments import PAYMENTS_RPC_NAMESPACE

from ...services.rabbitmq import get_rabbitmq_rpc_client, is_rabbitmq_enabled
from ...services.rabbitmq import get_rabbitmq_rpc_server
from . import _payments

_logger = logging.getLogger(__name__)

PAYMENTS_RPC_NAMESPACE: Final[RPCNamespace] = parse_obj_as(RPCNamespace, "payments")


def setup_rpc_api_routes(app: FastAPI) -> None:
async def _on_startup() -> None:
if is_rabbitmq_enabled(app):
rpc_client = get_rabbitmq_rpc_client(app)
await rpc_client.register_router(
_payments.router, PAYMENTS_RPC_NAMESPACE, app
)
rpc_server = get_rabbitmq_rpc_server(app)
await rpc_server.register_router(_payments.router, PAYMENTS_RPC_NAMESPACE, app)

app.add_event_handler("startup", _on_startup)
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ def get_rabbitmq_client(app: FastAPI) -> RabbitMQClient:
return cast(RabbitMQClient, app.state.rabbitmq_client)


def is_rabbitmq_enabled(app: FastAPI) -> bool:
return app.state.rabbitmq_client is not None


def get_rabbitmq_rpc_client(app: FastAPI) -> RabbitMQRPCClient:
def get_rabbitmq_rpc_server(app: FastAPI) -> RabbitMQRPCClient:
assert app.state.rabbitmq_rpc_server # nosec
return cast(RabbitMQRPCClient, app.state.rabbitmq_rpc_server)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
from models_library.users import UserID
from models_library.wallets import WalletID

from ._api import check_wallet_permissions
from ._autorecharge_db import (
PaymentsAutorechargeDB,
get_wallet_autorecharge,
replace_wallet_autorecharge,
)
from ._methods_db import list_successful_payment_methods
from ._onetime_api import check_wallet_permissions
from .settings import get_plugin_settings

_logger = logging.getLogger(__name__)
Expand Down
135 changes: 0 additions & 135 deletions services/web/server/src/simcore_service_webserver/payments/_client.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from simcore_postgres_database.models.payments_methods import InitPromptAckFlowState
from yarl import URL

from ._api import check_wallet_permissions
from ._autorecharge_db import get_wallet_autorecharge
from ._methods_db import (
PaymentsMethodsDB,
Expand All @@ -29,6 +28,7 @@
list_successful_payment_methods,
udpate_payment_method,
)
from ._onetime_api import check_wallet_permissions
from ._socketio import notify_payment_method_acked
from .settings import PaymentsSettings, get_plugin_settings

Expand Down Expand Up @@ -64,11 +64,6 @@ def _to_api_model(
)


#
# Payment-methods
#


async def init_creation_of_wallet_payment_method(
app: web.Application,
*,
Expand Down Expand Up @@ -119,7 +114,7 @@ async def init_creation_of_wallet_payment_method(
)


async def _complete_create_of_wallet_payment_method(
async def _ack_creation_of_wallet_payment_method(
app: web.Application,
*,
payment_method_id: PaymentMethodID,
Expand Down Expand Up @@ -164,7 +159,7 @@ async def cancel_creation_of_wallet_payment_method(
app, user_id=user_id, wallet_id=wallet_id, product_name=product_name
)

await _complete_create_of_wallet_payment_method(
await _ack_creation_of_wallet_payment_method(
app,
payment_method_id=payment_method_id,
completion_state=InitPromptAckFlowState.CANCELED,
Expand Down
Loading

0 comments on commit 4e833ff

Please sign in to comment.