Added the option to pull less than 10 messages
Build & Publish to GHCR / select-runner (push) Successful in 2s
Build & Publish to GHCR / build (push) Failing after 5s
Generate openapi.json / generate-docs (push) Failing after 16s
pip-audit / audit (push) Failing after 23s
pip-audit / notify (push) Successful in 0s
Build & Publish to GHCR / select-runner (push) Successful in 2s
Build & Publish to GHCR / build (push) Failing after 5s
Generate openapi.json / generate-docs (push) Failing after 16s
pip-audit / audit (push) Failing after 23s
pip-audit / notify (push) Successful in 0s
This commit is contained in:
1 parent
a814949ced
commit
c2f7026ba5
2 files changed
+27
-8
No files matched your search
@@ -10,6 +10,7 @@ h11==0.16.0
|
|||||||
idna==3.20
|
idna==3.20
|
||||||
lib-secret-manager==0.3.1
|
lib-secret-manager==0.3.1
|
||||||
lib-service-auth==0.1.0
|
lib-service-auth==0.1.0
|
||||||
|
lib-shared-models[pydantic]=1.0
|
||||||
multidict==7.1.0
|
multidict==7.1.0
|
||||||
opentelemetry-api==1.45.2
|
opentelemetry-api==1.45.2
|
||||||
pamqp==4.0.1
|
pamqp==4.0.1
|
||||||
|
|||||||
+26
-8
@@ -1,6 +1,8 @@
|
|||||||
|
|
||||||
|
from typing import Annotated
|
||||||
|
|
||||||
from aio_pika.exceptions import AMQPException
|
from aio_pika.exceptions import AMQPException
|
||||||
from fastapi import APIRouter, Depends, HTTPException, Request
|
from fastapi import APIRouter, Depends, HTTPException, Path, Request
|
||||||
from service_auth import UserId
|
from service_auth import UserId
|
||||||
|
|
||||||
from auth_config import Caller, auth
|
from auth_config import Caller, auth
|
||||||
@@ -11,17 +13,18 @@ logger = setup_logger(__name__)
|
|||||||
_MAX_MESSAGES = 10
|
_MAX_MESSAGES = 10
|
||||||
|
|
||||||
user_router = APIRouter(dependencies=[Depends(auth.require(Caller.GATEWAY))])
|
user_router = APIRouter(dependencies=[Depends(auth.require(Caller.GATEWAY))])
|
||||||
|
CountParam = Annotated[int, Path(ge=1, le=_MAX_MESSAGES, description="Number of messages to pull")]
|
||||||
|
|
||||||
@user_router.post("/notifications/pull", response_model=PullResponse)
|
async def _pull(
|
||||||
async def pull_notifications(
|
|
||||||
user_id: UserId,
|
user_id: UserId,
|
||||||
request: Request
|
request: Request,
|
||||||
):
|
max_messages: int,
|
||||||
logger.debug("[FastAPI] User id %s is requesting messages", user_id)
|
)-> PullResponse:
|
||||||
|
logger.debug("[FastAPI] User id %s is requesting up to %d messages", user_id, max_messages)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
messages = await request.app.state.rmq_consumer.fetch_messages(
|
messages = await request.app.state.rmq_consumer.fetch_messages(
|
||||||
user_id=user_id, max_messages=_MAX_MESSAGES
|
user_id=user_id, max_messages=max_messages
|
||||||
)
|
)
|
||||||
except (AMQPException, OSError) as e:
|
except (AMQPException, OSError) as e:
|
||||||
logger.error("[FastAPI] RabbitMQ unavailable for user_id=%s: %s", user_id, e)
|
logger.error("[FastAPI] RabbitMQ unavailable for user_id=%s: %s", user_id, e)
|
||||||
@@ -31,4 +34,19 @@ async def pull_notifications(
|
|||||||
raise HTTPException(status_code=503, detail="Service not ready") from e
|
raise HTTPException(status_code=503, detail="Service not ready") from e
|
||||||
|
|
||||||
logger.info("[FastAPI] Retrieved %s message(s) for user_id=%s",len(messages), user_id)
|
logger.info("[FastAPI] Retrieved %s message(s) for user_id=%s",len(messages), user_id)
|
||||||
return PullResponse(messages=messages, count=len(messages), has_more=len(messages) >= _MAX_MESSAGES)
|
return PullResponse(messages=messages, count=len(messages), has_more=len(messages) >= max_messages)
|
||||||
|
|
||||||
|
@user_router.post("/notifications/pull", response_model=PullResponse)
|
||||||
|
async def pull_notifications(
|
||||||
|
user_id: UserId,
|
||||||
|
request: Request
|
||||||
|
):
|
||||||
|
return await _pull(user_id, request, _MAX_MESSAGES)
|
||||||
|
|
||||||
|
@user_router.post("/notifications/pull/{count}", response_model=PullResponse)
|
||||||
|
async def pull_notifications_limited(
|
||||||
|
user_id: UserId,
|
||||||
|
request: Request,
|
||||||
|
count: CountParam,
|
||||||
|
):
|
||||||
|
return await _pull(user_id, request, count)
|
||||||
Reference in new issue
Block a user