From cf8e95a9c175175b9632fbc4e14c1fc6bd6f0cd4 Mon Sep 17 00:00:00 2001 From: Antonio Silva Date: Wed, 9 Aug 2023 11:04:10 +0200 Subject: [PATCH 1/2] Add query param to include important people --- main.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 183e683..a39bb10 100644 --- a/main.py +++ b/main.py @@ -3,15 +3,32 @@ app = FastAPI() +IMPORTANT_PEOPLE = ["Boss", "Ned Stark", "Robert Baratheon", "Jaime Lannister"] + + +def add_to_destination_list(values: list[str], default=["Boss"]): + to = default + for value in values: + if value not in default: + to.append(value) + return to + + @app.post("/generate_message", status_code=status.HTTP_202_ACCEPTED) async def generate_message( destination: list[str] = Body(), message: str = Body("Hello World"), + include_important_people: bool = False, ) -> str: """ Generate a message that will be sent to all the people in the destination list. - **destination**: A list of people that should receive the message. - **message**: The content of the message that will be sent. + - **include_important_people**: Whether to include important people in the destination list, default `False`. """ - return {"message": message, "destination": ["Boss"] + destination} - + if include_important_people: + destination = add_to_destination_list(destination, default=IMPORTANT_PEOPLE) + else: + destination = add_to_destination_list(destination) + + return {"message": message, "destination": destination} From 3603c5c78a03ea35f5ee682ac7fc31ad4fc507ce Mon Sep 17 00:00:00 2001 From: Antonio Silva Date: Wed, 9 Aug 2023 11:28:25 +0200 Subject: [PATCH 2/2] Add filter for message with less than 10 chars --- main.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/main.py b/main.py index a39bb10..a9dd1ba 100644 --- a/main.py +++ b/main.py @@ -26,6 +26,12 @@ async def generate_message( - **message**: The content of the message that will be sent. - **include_important_people**: Whether to include important people in the destination list, default `False`. """ + if len(message) <= 10: + raise HTTPException( + status_code=status.HTTP_409_CONFLICT, + detail="Message should be at least 10 characters long.", + ) + if include_important_people: destination = add_to_destination_list(destination, default=IMPORTANT_PEOPLE) else: