generated from PrivateAIM/python-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from PrivateAIM/41-refactor-endpoints
Reintroduce local endpoint and rename existing endpoints
- Loading branch information
Showing
8 changed files
with
185 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import logging | ||
import uuid | ||
from typing import Annotated | ||
|
||
from fastapi import Depends, UploadFile, APIRouter, HTTPException | ||
from minio import Minio, S3Error | ||
from pydantic import BaseModel, HttpUrl | ||
from starlette import status | ||
from starlette.requests import Request | ||
from starlette.responses import StreamingResponse | ||
|
||
from project.config import Settings | ||
from project.dependencies import get_client_id, get_settings, get_local_minio | ||
|
||
router = APIRouter() | ||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class LocalUploadResponse(BaseModel): | ||
url: HttpUrl | ||
|
||
|
||
@router.put( | ||
"/", | ||
response_model=LocalUploadResponse, | ||
summary="Upload file as intermediate result to local storage", | ||
operation_id="putLocalResult", | ||
) | ||
async def submit_intermediate_result_to_local( | ||
client_id: Annotated[str, Depends(get_client_id)], | ||
file: UploadFile, | ||
settings: Annotated[Settings, Depends(get_settings)], | ||
minio: Annotated[Minio, Depends(get_local_minio)], | ||
request: Request, | ||
): | ||
object_id = uuid.uuid4() | ||
object_name = f"local/{client_id}/{object_id}" | ||
|
||
minio.put_object( | ||
settings.minio.bucket, | ||
object_name, | ||
data=file.file, | ||
length=file.size, | ||
content_type=file.content_type or "application/octet-stream", | ||
) | ||
|
||
return LocalUploadResponse( | ||
url=str( | ||
request.url_for( | ||
"retrieve_intermediate_result_from_local", | ||
object_id=object_id, | ||
) | ||
) | ||
) | ||
|
||
|
||
@router.get( | ||
"/{object_id}", | ||
summary="Get intermediate result as file from local storage", | ||
operation_id="getLocalResult", | ||
) | ||
async def retrieve_intermediate_result_from_local( | ||
client_id: Annotated[str, Depends(get_client_id)], | ||
object_id: uuid.UUID, | ||
settings: Annotated[Settings, Depends(get_settings)], | ||
minio: Annotated[Minio, Depends(get_local_minio)], | ||
): | ||
try: | ||
response = minio.get_object( | ||
settings.minio.bucket, | ||
f"local/{client_id}/{object_id}", | ||
) | ||
except S3Error as e: | ||
logger.exception(f"Could not get object `{object_id}` for client `{client_id}`") | ||
|
||
if e.code == "NoSuchKey": | ||
raise HTTPException( | ||
status_code=status.HTTP_404_NOT_FOUND, | ||
detail=f"Object with ID {object_id} does not exist", | ||
) | ||
|
||
raise HTTPException( | ||
status_code=status.HTTP_502_BAD_GATEWAY, | ||
detail="Unexpected error from object store", | ||
) | ||
|
||
return StreamingResponse( | ||
response, | ||
media_type=response.headers.get("Content-Type", "application/octet-stream"), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import uuid | ||
|
||
from starlette import status | ||
|
||
from project.routers.local import LocalUploadResponse | ||
from tests.common.auth import BearerAuth, issue_client_access_token | ||
from tests.common.helpers import next_random_bytes | ||
from tests.common.rest import wrap_bytes_for_request, detail_of | ||
|
||
|
||
def test_200_submit_receive_from_local(test_client, rng): | ||
blob = next_random_bytes(rng) | ||
r = test_client.put( | ||
"/local", | ||
auth=BearerAuth(issue_client_access_token()), | ||
files=wrap_bytes_for_request(blob), | ||
) | ||
|
||
assert r.status_code == status.HTTP_200_OK | ||
model = LocalUploadResponse(**r.json()) | ||
|
||
r = test_client.get( | ||
model.url.path, | ||
auth=BearerAuth(issue_client_access_token()), | ||
) | ||
|
||
assert r.status_code == status.HTTP_200_OK | ||
assert r.read() == blob | ||
|
||
|
||
def test_404_unknown_oid(test_client): | ||
oid = uuid.uuid4() | ||
r = test_client.get( | ||
f"/local/{oid}", | ||
auth=BearerAuth(issue_client_access_token()), | ||
) | ||
|
||
assert r.status_code == status.HTTP_404_NOT_FOUND | ||
assert detail_of(r) == f"Object with ID {oid} does not exist" |