From f2b1a9508a8f185da6d0dbf69dac0b3d4d09db6a Mon Sep 17 00:00:00 2001 From: Tyler Perkins Date: Wed, 26 Jul 2023 19:55:48 -0400 Subject: [PATCH] Add optional date param --- src/main.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main.py b/src/main.py index 5b83ba1..d35f0ed 100644 --- a/src/main.py +++ b/src/main.py @@ -1,5 +1,6 @@ -from fastapi import FastAPI -import datetime +from fastapi import FastAPI, HTTPException +from datetime import datetime, date +import dateutil.parser app = FastAPI() @@ -21,11 +22,20 @@ ben_franklin_virtues = { @app.get("/") -async def virtue(): +async def virtue(day: str = None): """ Get the virtue of the week """ - current_week = datetime.date.today().isocalendar()[1] + if day is None: + current_date = date.today() + else: + try: + current_date = dateutil.parser.parse(day).date() + except: + raise HTTPException(status_code=400, + detail="Invalid date format, please provide a date in the month/day/year format.") + + current_week = current_date.isocalendar()[1] virtues = list(ben_franklin_virtues.keys())