From 3074249703d928b2f25a8cd6b0adaac684567911 Mon Sep 17 00:00:00 2001 From: Julien Maupetit Date: Fri, 14 Jun 2024 16:52:44 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F(api)=20return=20the=20number?= =?UTF-8?q?=20of=20created=20items=20for=20dynamique=20bulk=20requests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is a relevant information when using the API with the client or the CLI. --- src/api/CHANGELOG.md | 4 ++++ src/api/qualicharge/api/v1/routers/dynamic.py | 16 +++++++++++++--- src/api/tests/api/v1/routers/test_dynamic.py | 8 ++++---- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/api/CHANGELOG.md b/src/api/CHANGELOG.md index dab26028..1679f867 100644 --- a/src/api/CHANGELOG.md +++ b/src/api/CHANGELOG.md @@ -8,6 +8,10 @@ and this project adheres to ## [Unreleased] +### Changed + +- API dynamique bulk requests now returns the number of created items + ## [0.9.0] - 2024-06-11 ### Added diff --git a/src/api/qualicharge/api/v1/routers/dynamic.py b/src/api/qualicharge/api/v1/routers/dynamic.py index d63f011e..5ae22367 100644 --- a/src/api/qualicharge/api/v1/routers/dynamic.py +++ b/src/api/qualicharge/api/v1/routers/dynamic.py @@ -6,7 +6,7 @@ from annotated_types import Len from fastapi import APIRouter, Depends, HTTPException, Path, Query, Security from fastapi import status as fa_status -from pydantic import PastDatetime, StringConstraints +from pydantic import BaseModel, PastDatetime, StringConstraints from sqlalchemy import func from sqlalchemy.schema import Column as SAColumn from sqlmodel import Session, join, select @@ -44,6 +44,12 @@ ] +class DynamiqueItemsCreatedResponse(BaseModel): + """API response model used when dynamic items are created.""" + + size: int + + @router.get("/status/", tags=["Status"]) async def list_statuses( user: Annotated[User, Security(get_user, scopes=[ScopesEnum.DYNAMIC_READ.value])], @@ -319,7 +325,7 @@ async def create_status_bulk( user: Annotated[User, Security(get_user, scopes=[ScopesEnum.DYNAMIC_CREATE.value])], statuses: BulkStatusCreateList, session: Session = Depends(get_session), -) -> None: +) -> DynamiqueItemsCreatedResponse: """Create a statuses batch.""" for status in statuses: if not is_pdc_allowed_for_user(status.id_pdc_itinerance, user): @@ -359,6 +365,8 @@ async def create_status_bulk( session.add_all(db_statuses) session.commit() + return DynamiqueItemsCreatedResponse(size=len(db_statuses)) + @router.post("/session/", status_code=fa_status.HTTP_201_CREATED, tags=["Session"]) async def create_session( @@ -395,7 +403,7 @@ async def create_session_bulk( user: Annotated[User, Security(get_user, scopes=[ScopesEnum.DYNAMIC_CREATE.value])], sessions: BulkSessionCreateList, db_session: Session = Depends(get_session), -) -> None: +) -> DynamiqueItemsCreatedResponse: """Create a sessions batch.""" for session in sessions: if not is_pdc_allowed_for_user(session.id_pdc_itinerance, user): @@ -433,3 +441,5 @@ async def create_session_bulk( db_qc_sessions.append(db_qc_session) db_session.add_all(db_qc_sessions) db_session.commit() + + return DynamiqueItemsCreatedResponse(size=len(db_qc_sessions)) diff --git a/src/api/tests/api/v1/routers/test_dynamic.py b/src/api/tests/api/v1/routers/test_dynamic.py index 395fa7a7..ea9b04bb 100644 --- a/src/api/tests/api/v1/routers/test_dynamic.py +++ b/src/api/tests/api/v1/routers/test_dynamic.py @@ -906,7 +906,7 @@ def test_create_status_bulk_for_superuser(db_session, client_auth): json=[json.loads(s.model_dump_json()) for s in qc_statuses], ) assert response.status_code == status.HTTP_201_CREATED - assert response.json() is None + assert response.json() == {"size": 3} # Check created statuses db_statuses = db_session.exec(select(Status)).all() @@ -1023,7 +1023,7 @@ def test_create_status_bulk_for_user(db_session, client_auth): json=[json.loads(s.model_dump_json()) for s in qc_statuses], ) assert response.status_code == status.HTTP_201_CREATED - assert response.json() is None + assert response.json() == {"size": 3} # Check created statuses db_statuses = db_session.exec(select(Status)).all() @@ -1280,7 +1280,7 @@ def test_create_session_bulk_for_superuser(db_session, client_auth): json=[json.loads(s.model_dump_json()) for s in qc_sessions], ) assert response.status_code == status.HTTP_201_CREATED - assert response.json() is None + assert response.json() == {"size": 3} # Check created statuses db_qc_sessions = db_session.exec(select(Session)).all() @@ -1391,7 +1391,7 @@ def test_create_session_bulk_for_user(db_session, client_auth): json=[json.loads(s.model_dump_json()) for s in qc_sessions], ) assert response.status_code == status.HTTP_201_CREATED - assert response.json() is None + assert response.json() == {"size": 3} # Check created statuses db_qc_sessions = db_session.exec(select(Session)).all()