-
Notifications
You must be signed in to change notification settings - Fork 0
/
routers.py
40 lines (30 loc) · 995 Bytes
/
routers.py
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
import json
import os
from fastapi import APIRouter, File, UploadFile
from pydrive2.drive import GoogleDrive
from pydrive2.auth import GoogleAuth
from utils import upload_to_google_drive, get_file_for_key
router = APIRouter()
FOLDER_ID = os.getenv("FOLDER_ID")
@router.post("/upload-file/")
def upload_file(data: UploadFile = File(...)) -> json:
try:
g_auth = GoogleAuth()
drive = GoogleDrive(g_auth)
g_auth.LocalWebserverAuth()
output = upload_to_google_drive(
drive=drive, g_auth=g_auth, data=data, folder_id=FOLDER_ID
)
return output
except Exception:
return {"Success": False}
@router.get("/get-file/")
def get_file(key: str) -> json:
try:
g_auth = GoogleAuth()
drive = GoogleDrive(g_auth)
output = get_file_for_key(drive=drive, key=key, folder_id=FOLDER_ID)
return output
except Exception as ex:
print("Error: ", str(ex))
return {"Success": False}