FastAPI - Run a function after POST method

Prerequisites

Steps

  1. End result. Receive an email from server.

  2. Create a virtual environment and activate it.

    $ python3 -m venv venv
    $ source venv/bin/activate
    
  3. Install dependencies.

    (venv) $ pip install fastapi uvicorn[standard] python-dotenv
    (venv) clone https://github.com/taeheechoi/python-send-email-with-attachments.git .
    
  4. Rename .env_sample to .env and update environment variables with your credentials.

    EMAIL_ID=youremail@gmail.com
    EMAIL_PASSWORD=app password 16 characters
    TO_EMAIL=test1@gmail.com;test2@gmail.com
    
  5. Create a new python file and paste below code. eg) app.py

    import os
    from datetime import datetime
    from glob import glob
    from typing import Optional
    
    from fastapi import FastAPI
    from pydantic import BaseModel, validator
    import uvicorn
    
    from gmail import email_with_attachment
    
    app = FastAPI()
    
    class DateTimeModeMixin(BaseModel):
        # To add timestamp script executed, https://pydantic-docs.helpmanual.io/usage/validators/
        created_at: datetime = None
    
        @validator("created_at", pre=True, always=True)
        def default_datetime(cls, value:datetime) -> datetime:
            return value or datetime.now()
    
    class Job(DateTimeModeMixin):
        name: str
        description: Optional[str]
    
    jobs = []
    
    def job_email_with_attachment():
        to_email = os.getenv("TO_EMAIL").split(';')
        attachments = glob('data/*.csv')
        email_with_attachment(header='header...', recipient=to_email, body='body...', attachments=attachments)
    
    @app.get('/jobs')
    async def get_jobs():
        return jobs
    
    @app.post('/jobs', status_code=201)
    async def add_job(job: Job):
        jobs.append(job)
        job_email_with_attachment()
        return jobs
    
    if __name__ == '__main__':
        uvicorn.run(app)
    
  6. Run app.py. Ctrl + F5 or Right click >> "Run Python File in Terminal" within VS Code.

  7. Install VS extension "REST Client."

  8. Click "Send Request" on test.http file.

Github

References

  • https://realpython.com/api-integration-in-python/
  • https://stackoverflow.com/questions/62934384/how-to-add-timestamp-to-each-request-in-uvicorn-logs
  • https://www.jeffastor.com/blog/designing-a-robust-user-model-in-a-fastapi-app
  • https://pydantic-docs.helpmanual.io/usage/validators/
taeheechoi © 2023