File size: 1,823 Bytes
e3a12d5
db83efb
 
 
e3a12d5
db83efb
e3a12d5
db83efb
 
e3a12d5
f06d4d5
e3a12d5
 
4fe2f95
e3a12d5
 
 
 
 
 
 
 
 
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
"""Module for defining the main routes of the API."""
import os
import pickle
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

router = APIRouter(prefix="/mail", tags=["mail"])

@router.post("")
def collect(email: str, 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/{email}.pickle"):
            with open(f"cache/{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)
        mail.collect(mailservice)
        return JSONResponse(content={"message": "Mail collected successfully."})
    except Exception as 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)