Spaces:
Running
Running
File size: 2,022 Bytes
e3a12d5 db83efb 7eea859 db83efb e3a12d5 db83efb e3a12d5 db83efb e3a12d5 a171580 f06d4d5 e3a12d5 a171580 e3a12d5 5a42f57 a171580 5a42f57 7eea859 5a42f57 7eea859 5a42f57 e3a12d5 db83efb e3a12d5 db83efb e3a12d5 db83efb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
"""Module for defining the main routes of the API."""
import os
import pickle
import threading
from venv import logger
from fastapi import APIRouter, Request
from fastapi.responses import JSONResponse
from controllers import mail
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
from schema import MailReqData
router = APIRouter(prefix="/mail", tags=["mail"])
@router.post("")
def collect(query: MailReqData, request: Request):
"""
Handles the chat POST request.
Args:
query (ReqData): The request data containing the query parameters.
Returns:
str: The generated response from the chat function.
"""
try:
if os.path.exists(f"cache/{query.email}.pickle"):
with open(f"cache/{query.email}.pickle", "rb") as token:
credentials = pickle.load(token)
else:
cred_dict = request.state.session.get("credential")
credentials = Credentials(
token=cred_dict["token"],
refresh_token=cred_dict["refresh_token"],
token_uri=cred_dict["token_uri"],
client_id=cred_dict["client_id"],
client_secret=cred_dict["client_secret"],
scopes=cred_dict["scopes"],
)
mailservice = build("gmail", "v1", credentials=credentials)
threading.Thread(target=mail.collect, args=(mailservice, query.query)).start()
return JSONResponse(content={"message": "Mail collection in progress."})
except Exception as e:
logger.error("Error collecting mail: %s", e)
return JSONResponse(content={"error": str(e)}, status_code=500)
# @router.get("")
# def get():
# """
# Handles the chat POST request.
# Args:
# query (ReqData): The request data containing the query parameters.
# Returns:
# str: The generated response from the chat function.
# """
# # result = mail.get()
# return JSONResponse(content= result)
|