-
Notifications
You must be signed in to change notification settings - Fork 1
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 #6 from akvo/implement-routes
Implement proxy routes
- Loading branch information
Showing
24 changed files
with
751 additions
and
297 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from collections.abc import Callable | ||
|
||
from app.s3 import S3Bucket | ||
|
||
|
||
def make_bucket(bucket: str, access_key_id: str, secret_access_key: str) -> S3Bucket: | ||
return S3Bucket(bucket, access_key_id, secret_access_key) | ||
|
||
|
||
def bucket_factory() -> Callable[[str, str, str], S3Bucket]: | ||
return make_bucket |
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 |
---|---|---|
@@ -1,8 +1,127 @@ | ||
from fastapi import FastAPI | ||
from collections.abc import Callable | ||
from typing import Annotated | ||
|
||
from fastapi import Depends, FastAPI, HTTPException, Path, UploadFile, status | ||
from starlette.responses import StreamingResponse | ||
|
||
from app.dependencies import bucket_factory | ||
from app.flow_config import get_config | ||
from app.messages import ResultMessage | ||
from app.s3 import S3Bucket | ||
|
||
FormIdParam = Annotated[str, Path(pattern=r"^\d+$")] | ||
app = FastAPI() | ||
|
||
|
||
def validate_form_id(form_id: str) -> None: | ||
# TODO: form id validation | ||
if not form_id: | ||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) # pragma: no cover | ||
|
||
|
||
def get_config_for(instance: str) -> dict[str, str]: | ||
config = get_config(instance) | ||
if not config: | ||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) | ||
|
||
return config | ||
|
||
|
||
async def upload( | ||
instance: str, | ||
form_id: str, | ||
file: UploadFile, | ||
folder: str, | ||
make_bucket: Callable[[str, str, str], S3Bucket], | ||
) -> ResultMessage: | ||
config = get_config_for(instance) | ||
validate_form_id(form_id) | ||
bucket = make_bucket( | ||
str(config.get("awsBucket")), | ||
str(config.get("awsAccessKeyId")), | ||
str(config.get("awsSecretKey")), | ||
) | ||
extra_args = {"ContentType": file.content_type} | ||
if folder == "images": | ||
extra_args["ACL"] = "public-read" | ||
file_key = f"{folder}/{str(file.filename)}" | ||
bucket.upload(file.file, file_key, extra_args) | ||
return ResultMessage.success("OK!") | ||
|
||
|
||
@app.put("/{instance}/devicezip/{form_id}/", status_code=status.HTTP_201_CREATED) | ||
async def put_devicezip( | ||
instance: str, | ||
form_id: FormIdParam, | ||
file: UploadFile, | ||
make_bucket: Annotated[ | ||
Callable[[str, str, str], S3Bucket], Depends(bucket_factory) | ||
], | ||
) -> ResultMessage: | ||
return await upload(instance, form_id, file, "devicezip", make_bucket) | ||
|
||
|
||
@app.put("/{instance}/images/{form_id}/", status_code=status.HTTP_201_CREATED) | ||
async def put_images( | ||
instance: str, | ||
form_id: FormIdParam, | ||
file: UploadFile, | ||
make_bucket: Annotated[ | ||
Callable[[str, str, str], S3Bucket], Depends(bucket_factory) | ||
], | ||
) -> ResultMessage: | ||
return await upload(instance, form_id, file, "images", make_bucket) | ||
|
||
|
||
@app.get("/{instance}/surveys/{form_id}.zip") | ||
async def get_survey_form( | ||
instance: str, | ||
form_id: FormIdParam, | ||
make_bucket: Annotated[ | ||
Callable[[str, str, str], S3Bucket], Depends(bucket_factory) | ||
], | ||
) -> StreamingResponse: | ||
config = get_config_for(instance) | ||
validate_form_id(form_id) | ||
bucket = make_bucket( | ||
str(config.get("awsBucket")), | ||
str(config.get("awsAccessKeyId")), | ||
str(config.get("awsSecretKey")), | ||
) | ||
|
||
try: | ||
res = bucket.download(f"surveys/{form_id}.zip") | ||
return StreamingResponse( | ||
content=res["Body"].iter_chunks(), media_type=res["ContentType"] | ||
) | ||
except Exception as e: | ||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) from e | ||
|
||
|
||
@app.get("/{instance}/images/{filename}") | ||
async def get_image( | ||
instance: str, | ||
filename: str, | ||
make_bucket: Annotated[ | ||
Callable[[str, str, str], S3Bucket], Depends(bucket_factory) | ||
], | ||
) -> StreamingResponse: | ||
config = get_config_for(instance) | ||
bucket = make_bucket( | ||
str(config.get("awsBucket")), | ||
str(config.get("awsAccessKeyId")), | ||
str(config.get("awsSecretKey")), | ||
) | ||
|
||
try: | ||
res = bucket.download(f"images/{filename}") | ||
return StreamingResponse( | ||
content=res["Body"].iter_chunks(), media_type=res["ContentType"] | ||
) | ||
except Exception as e: | ||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) from e | ||
|
||
|
||
@app.get("/healtz", include_in_schema=False) | ||
async def healt_check() -> dict[str, str]: | ||
return {"message": "OK!"} | ||
async def healt_check() -> ResultMessage: | ||
return ResultMessage.success("OK!") |
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,27 @@ | ||
from enum import StrEnum, auto | ||
from typing import Self | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class MessageStatus(StrEnum): | ||
SUCCESS = auto() | ||
FAIL = auto() | ||
ERROR = auto() | ||
|
||
|
||
class ResultMessage(BaseModel): | ||
status: MessageStatus | ||
message: str | None = None | ||
|
||
@classmethod | ||
def success(cls, message: str) -> Self: | ||
return cls(status=MessageStatus.SUCCESS, message=message) | ||
|
||
@classmethod | ||
def fail(cls, message: str) -> Self: | ||
return cls(status=MessageStatus.FAIL, message=message) | ||
|
||
@classmethod | ||
def error(cls, message: str) -> Self: | ||
return cls(status=MessageStatus.ERROR, message=message) |
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,21 @@ | ||
from typing import Any, BinaryIO | ||
|
||
import boto3 | ||
|
||
|
||
class S3Bucket: | ||
def __init__(self, bucket: str, access_key_id: str, secret_access_key: str): | ||
self.bucket = bucket | ||
self.client = boto3.client( | ||
"s3", | ||
aws_access_key_id=access_key_id, | ||
aws_secret_access_key=secret_access_key, | ||
) | ||
|
||
def upload( | ||
self, fileobj: BinaryIO, key: str, extra: dict[str, Any] | None = None | ||
) -> None: | ||
self.client.upload_fileobj(fileobj, self.bucket, key, ExtraArgs=extra) | ||
|
||
def download(self, key: str): # type: ignore | ||
return self.client.get_object(Bucket=self.bucket, Key=key) |
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
Oops, something went wrong.