Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat (export): Adding endpoints to list JSON Object IDs and Download JSON Resources #199

Merged
merged 2 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion gdrive/drive_client.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import io
import logging
import json
import mimetypes
import pprint
from typing import List

from google.oauth2 import service_account
from googleapiclient.discovery import build
from googleapiclient.discovery import build, mimetypes
from googleapiclient.http import MediaIoBaseUpload

from gdrive import settings, error
Expand All @@ -17,6 +19,8 @@

service = build("drive", "v3", credentials=creds)

DIRECTORY_MIME_TYPE = "application/vnd.google-apps.folder"


def init():
drive = drives_list()
Expand Down Expand Up @@ -174,9 +178,57 @@ def get_files(filename: str) -> List:
return results["files"]


def get_files_by_drive_id(filename: str, drive_id: str):
"""
Get list of files by filename
"""

results = (
service.files()
.list(
q=f"name = '{filename}'",
corpora="drive",
driveId=drive_id,
includeTeamDriveItems=True,
supportsTeamDrives=True,
)
.execute()
)

return results["files"]


def get_files_in_folder(id: str) -> List:
"""
Get list of files within a folder by folder ID
"""
files = []
page_token = None
while True:
results = (
service.files()
.list(
q=f"'{id}' in parents and trashed=false",
supportsAllDrives=True,
includeItemsFromAllDrives=True,
pageToken=page_token,
)
.execute()
)
files.extend(results.get("files", []))
page_token = results.get("nextPageToken")
if not page_token:
break
return files


def delete_file(id: str) -> None:
"""
Delete file by id
"""

service.files().delete(fileId=id, supportsAllDrives=True).execute()


def export_to_json(id: str) -> str:
return json.loads(service.files().get_media(fileId=id).execute())
44 changes: 44 additions & 0 deletions gdrive/export_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,47 @@ async def find(find: FindModel):
)
export_data = export_client.find(responseId, find.field, find.values, result)
return export_data


# ------------------------------- Archive API --------------------------------------


class InteractionModel(BaseModel):
interactionId: str
driveId: str


@router.post("/export/vendor-response-list")
async def get_vendor_responses(request: InteractionModel):
"""
Returns a list of Google Drive object IDs that contain the
vendor responses for this particular interaction
"""
interaction_folders = drive_client.get_files_by_drive_id(
filename=request.interactionId, drive_id=request.driveId
)
vendor_file_ids = []
for dir in interaction_folders:
files = drive_client.get_files_in_folder(id=dir["id"])
for file in files:
if file["mimeType"] == "application/json" and (
"analytics" not in file["name"]
):
vendor_file_ids.append(file["id"])

return responses.JSONResponse(status_code=202, content=vendor_file_ids)


class JsonResourceModel(BaseModel):
resourceId: str


@router.post("/export/resource")
async def export_resource(request: JsonResourceModel):
try:
result = drive_client.export_to_json(request.resourceId)
except UnicodeDecodeError as e:
return responses.JSONResponse(
status_code=400, content="Resource could not be JSON encoded"
)
return responses.JSONResponse(status_code=202, content=result)