gmail-agent / app /main.py
samvish's picture
Initial commit
bbfb383
raw
history blame
1.81 kB
# app/main.py
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from .config import Config
from .agent import GmailAgent
from app import __version__
app = FastAPI(
title="Gmail Agent API",
description="API for Gmail management using Gemini and Composio",
version=__version__
)
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=Config.ALLOWED_ORIGINS,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Initialize Gmail Agent
gmail_agent = GmailAgent(
google_api_key=Config.GOOGLE_API_KEY,
composio_api_key=Config.COMPOSIO_API_KEY
)
class EmailRequest(BaseModel):
thread_id: str
email_content: str
sender_email: str
@app.get("/")
async def root():
"""
Root endpoint that provides API information and health check
"""
return {
"status": "online",
"version": __version__,
"name": "Gmail Agent API",
"description": "Gmail Agent API using Gemini and Composio"
}
@app.get("/health")
async def health_check():
return {"status": "healthy"}
@app.get("/authenticate")
async def get_auth_url():
try:
auth_url = gmail_agent.authenticate_gmail()
return {"auth_url": auth_url}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/process-email")
async def process_email(request: EmailRequest):
try:
response = await gmail_agent.process_email(
thread_id=request.thread_id,
email_content=request.email_content,
sender_email=request.sender_email
)
return {"response": response}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))