simplified the api naming and remove the user authentication
This commit is contained in:
parent
2615aff732
commit
a604597209
@ -4,22 +4,21 @@ from app.central_storage.backend.business.document_manager import (
|
|||||||
|
|
||||||
|
|
||||||
class DocumentHub:
|
class DocumentHub:
|
||||||
def __init__(self, user_id: str):
|
def __init__(self, ):
|
||||||
self.user_id = user_id
|
|
||||||
self.document_manager = DocumentManager(self.user_id)
|
self.document_manager = DocumentManager(self.user_id)
|
||||||
return
|
return
|
||||||
|
|
||||||
async def get_document_by_id(self, document_id: str):
|
async def retrieve_document_info(self, document_id: str):
|
||||||
return await self.document_manager.get_document_details_by_id(document_id)
|
return await self.document_manager.retrieve_document_info(document_id)
|
||||||
|
|
||||||
async def upload_document_for_object(
|
async def upload_document(
|
||||||
self, object_id: str, file_name: str, file_data: bytes
|
self, associated_with: str, file_name: str, file_data: bytes
|
||||||
) -> bool:
|
) -> bool:
|
||||||
"""Upload a file
|
"""Upload a file
|
||||||
Args:
|
Args:
|
||||||
file_name: the name of the file
|
file_name: the name of the file
|
||||||
file (bytes): the file to be uploaded
|
file (bytes): the file to be uploaded
|
||||||
"""
|
"""
|
||||||
return await self.document_manager.upload_file_for_object(
|
return await self.document_manager.upload_file(
|
||||||
object_id, file_name, file_data
|
associated_with, file_name, file_data
|
||||||
)
|
)
|
||||||
|
|||||||
@ -3,11 +3,10 @@ from app.central_storage.backend.models.models import MediaType, DataFormat
|
|||||||
|
|
||||||
|
|
||||||
class DocumentManager:
|
class DocumentManager:
|
||||||
def __init__(self, user_id) -> None:
|
def __init__(self) -> None:
|
||||||
self.user_id = user_id
|
|
||||||
self.document_service = DocumentService()
|
self.document_service = DocumentService()
|
||||||
|
|
||||||
async def get_document_details_by_id(self, document_id: str):
|
async def retrieve_document_info(self, document_id: str):
|
||||||
await self.document_service.load_document(document_id=document_id)
|
await self.document_service.load_document(document_id=document_id)
|
||||||
|
|
||||||
download_link = (
|
download_link = (
|
||||||
@ -19,15 +18,15 @@ class DocumentManager:
|
|||||||
"file_download_url": download_link,
|
"file_download_url": download_link,
|
||||||
}
|
}
|
||||||
|
|
||||||
async def upload_file_for_object(
|
async def upload_file(
|
||||||
self, object_id: str, file_name: str, file_data: bytes
|
self, associated_with: str, file_name: str, file_data: bytes
|
||||||
) -> bool:
|
) -> bool:
|
||||||
await self.document_service.new_document(
|
await self.document_service.new_document(
|
||||||
file_name=file_name,
|
file_name=file_name,
|
||||||
# This 'UNKNOWN' will make the document manager decide the media type from file name
|
# This 'UNKNOWN' will make the document manager decide the media type from file name
|
||||||
media_type=MediaType.UNKNOWN,
|
media_type=MediaType.UNKNOWN,
|
||||||
data_format=DataFormat.RAW,
|
data_format=DataFormat.RAW,
|
||||||
created_by=object_id,
|
created_by=associated_with,
|
||||||
)
|
)
|
||||||
return await self.document_service.save_document_file(file_data) or None
|
return await self.document_service.save_document_file(file_data) or None
|
||||||
# TODO: This should go to Freeleaps App
|
# TODO: This should go to Freeleaps App
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
from fastapi import APIRouter
|
from fastapi import APIRouter
|
||||||
from .get_document_by_id import router as doc_router
|
from .retrieve_document_info import router as doc_router
|
||||||
from .upload_document import router as ud_router
|
from .upload_document import router as ud_router
|
||||||
|
|
||||||
api_router = APIRouter()
|
api_router = APIRouter()
|
||||||
|
|||||||
@ -17,25 +17,18 @@ token_manager = TokenManager()
|
|||||||
# Web API
|
# Web API
|
||||||
# Fetch document by ID
|
# Fetch document by ID
|
||||||
@router.get(
|
@router.get(
|
||||||
"/get-document-by-id/{document_id}",
|
"/retrieve_document_info/{document_id}",
|
||||||
operation_id="get-document-by-id",
|
operation_id="retrieve_document_info",
|
||||||
summary="Fetch a document by its ID",
|
summary="Fetch a document by its ID",
|
||||||
description="Retrieve a specific document by its document ID and return file name and download URL",
|
description="Retrieve a specific document by its document ID and return file name and download URL",
|
||||||
response_description="The document details including file name and download URL",
|
response_description="The document details including file name and download URL",
|
||||||
)
|
)
|
||||||
async def get_document_by_id(
|
async def retrieve_document_info(
|
||||||
document_id: str,
|
document_id: str
|
||||||
current_user: dict = Depends(token_manager.get_current_user),
|
|
||||||
):
|
):
|
||||||
user_id = current_user.get("id")
|
|
||||||
|
|
||||||
if not user_id:
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=HTTP_401_UNAUTHORIZED, detail="Could not validate credentials"
|
|
||||||
)
|
|
||||||
|
|
||||||
# Fetch the document using DocumentHub
|
# Fetch the document using DocumentHub
|
||||||
document = await DocumentHub(user_id).get_document_by_id(document_id)
|
document = await DocumentHub().retrieve_document_info(document_id)
|
||||||
|
|
||||||
# If document is not found, raise 404 error
|
# If document is not found, raise 404 error
|
||||||
if not document:
|
if not document:
|
||||||
@ -13,28 +13,20 @@ token_manager = TokenManager()
|
|||||||
|
|
||||||
@router.post(
|
@router.post(
|
||||||
"/upload-document",
|
"/upload-document",
|
||||||
summary="upload a document for a given object.",
|
summary="upload a document with a given associated_with id.",
|
||||||
description="upload a document. If success, returning the document id",
|
description="upload a document. If success, returning the document id",
|
||||||
)
|
)
|
||||||
async def attach_document_for_request(
|
async def attach_document_for_request(
|
||||||
object_id: str = Form(...),
|
associated_with: str = Form(...),
|
||||||
file: UploadFile = File(None),
|
file: UploadFile = File(None)
|
||||||
current_user: dict = Depends(token_manager.get_current_user),
|
|
||||||
):
|
):
|
||||||
print("current user", current_user)
|
|
||||||
user_id = current_user.get("id")
|
|
||||||
print("current user id", user_id)
|
|
||||||
if not user_id:
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=HTTP_401_UNAUTHORIZED, detail="Could not validate credentials"
|
|
||||||
)
|
|
||||||
|
|
||||||
document_hub = DocumentHub(user_id)
|
document_hub = DocumentHub()
|
||||||
# File processing
|
# File processing
|
||||||
try:
|
try:
|
||||||
file_data = await file.read() # You can use async chunking for larger files
|
file_data = await file.read() # You can use async chunking for larger files
|
||||||
document_id = await document_hub.upload_document_for_object(
|
document_id = await document_hub.upload_document(
|
||||||
object_id, file.filename, file_data
|
associated_with, file.filename, file_data
|
||||||
)
|
)
|
||||||
|
|
||||||
if document_id:
|
if document_id:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user