Spaces:
Build error
Build error
Commit
·
ed2e4a9
1
Parent(s):
23b1a91
refactor: update response object schema
Browse files- apis/s3.py +21 -2
- app.py +12 -9
- models/response.py +6 -0
- requirements.txt +1 -0
apis/s3.py
CHANGED
|
@@ -1,11 +1,17 @@
|
|
| 1 |
import os
|
| 2 |
import boto3
|
|
|
|
|
|
|
| 3 |
from dotenv import load_dotenv
|
|
|
|
|
|
|
| 4 |
load_dotenv()
|
| 5 |
|
| 6 |
class S3:
|
| 7 |
|
| 8 |
-
def __init__(self) -> None:
|
|
|
|
|
|
|
| 9 |
self.aws_access_key_id = os.getenv('AWS_ACCESS_KEY_ID', '')
|
| 10 |
self.aws_secret_access_key = os.getenv('AWS_SECRET_ACCESS_KEY', '')
|
| 11 |
self.aws_default_region = os.getenv('AWS_DEFAULT_REGION', '')
|
|
@@ -28,7 +34,20 @@ class S3:
|
|
| 28 |
|
| 29 |
# Function to upload file to S3
|
| 30 |
def upload_file(self, bucket_name, file, name):
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
# Function to delete a file from S3
|
| 34 |
def delete_image(self, bucket_name, file_key):
|
|
|
|
| 1 |
import os
|
| 2 |
import boto3
|
| 3 |
+
import botocore
|
| 4 |
+
import logging
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
+
|
| 7 |
+
from demo.qccamapi.models.response import Response
|
| 8 |
load_dotenv()
|
| 9 |
|
| 10 |
class S3:
|
| 11 |
|
| 12 |
+
def __init__(self, custom_logging=None) -> None:
|
| 13 |
+
self.logging = custom_logging if custom_logging is not None else logging
|
| 14 |
+
|
| 15 |
self.aws_access_key_id = os.getenv('AWS_ACCESS_KEY_ID', '')
|
| 16 |
self.aws_secret_access_key = os.getenv('AWS_SECRET_ACCESS_KEY', '')
|
| 17 |
self.aws_default_region = os.getenv('AWS_DEFAULT_REGION', '')
|
|
|
|
| 34 |
|
| 35 |
# Function to upload file to S3
|
| 36 |
def upload_file(self, bucket_name, file, name):
|
| 37 |
+
res = Response()
|
| 38 |
+
try:
|
| 39 |
+
self.client.upload_fileobj(file, bucket_name, name)
|
| 40 |
+
res.status = 200
|
| 41 |
+
except botocore.exceptions.ClientError as error:
|
| 42 |
+
res.status = 429
|
| 43 |
+
if error.response['Error']['Code'] == 'LimitExceededException':
|
| 44 |
+
res.error = ValueError('API call limit exceeded; backing off and retrying...')
|
| 45 |
+
else:
|
| 46 |
+
res.error = error
|
| 47 |
+
except botocore.exceptions.ParamValidationError as error:
|
| 48 |
+
res.status = 400
|
| 49 |
+
res.error = ValueError('The parameters you provided are incorrect: {}'.format(error))
|
| 50 |
+
return res
|
| 51 |
|
| 52 |
# Function to delete a file from S3
|
| 53 |
def delete_image(self, bucket_name, file_key):
|
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import logging
|
|
| 4 |
from fastapi import FastAPI, Header
|
| 5 |
from apis.s3 import S3
|
| 6 |
from dotenv import load_dotenv
|
|
|
|
| 7 |
from models.image_object import ImageObject
|
| 8 |
from apis.common import Common
|
| 9 |
from fastapi.openapi.docs import (
|
|
@@ -18,15 +19,17 @@ AWS_S3_BUCKET_NAME = os.getenv('AWS_S3_BUCKET_NAME', '')
|
|
| 18 |
X_REQUEST_USER = os.environ.get('X_REQUEST_USER')
|
| 19 |
X_API_KEY = os.environ.get('X_API_KEY')
|
| 20 |
|
| 21 |
-
s3client = S3()
|
| 22 |
-
common = Common()
|
| 23 |
-
app = FastAPI(docs_url=None, redoc_url=None)
|
| 24 |
logging.basicConfig(
|
| 25 |
level=logging.DEBUG if IS_DEV else logging.INFO,
|
| 26 |
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
| 27 |
datefmt='%Y-%m-%d %H:%M:%S'
|
| 28 |
)
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
@app.get("/docs", include_in_schema=False)
|
| 31 |
async def custom_swagger_ui_html():
|
| 32 |
return get_swagger_ui_html(
|
|
@@ -52,11 +55,11 @@ async def redoc_html():
|
|
| 52 |
|
| 53 |
@app.get("/")
|
| 54 |
def root():
|
| 55 |
-
return
|
| 56 |
|
| 57 |
@app.get("/health")
|
| 58 |
def healthcheck():
|
| 59 |
-
return
|
| 60 |
|
| 61 |
@app.post("/image")
|
| 62 |
async def upload(
|
|
@@ -64,7 +67,7 @@ async def upload(
|
|
| 64 |
x_request_user: str = Header(...),
|
| 65 |
x_api_key: str = Header(...)
|
| 66 |
):
|
| 67 |
-
res =
|
| 68 |
logging.info("--------------------------------")
|
| 69 |
logging.info("Received request to upload image")
|
| 70 |
|
|
@@ -86,11 +89,11 @@ async def upload(
|
|
| 86 |
logging.info("File uploaded successfully")
|
| 87 |
|
| 88 |
except Exception as e:
|
| 89 |
-
res = str(e)
|
| 90 |
-
logging.warning(f"Error during upload: {res}")
|
| 91 |
return {"error": "Failed to upload content"}
|
| 92 |
|
| 93 |
-
return
|
| 94 |
|
| 95 |
def is_valid(u, p):
|
| 96 |
return u == X_REQUEST_USER and p == X_API_KEY
|
|
|
|
| 4 |
from fastapi import FastAPI, Header
|
| 5 |
from apis.s3 import S3
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
+
from demo.qccamapi.models.response import Response
|
| 8 |
from models.image_object import ImageObject
|
| 9 |
from apis.common import Common
|
| 10 |
from fastapi.openapi.docs import (
|
|
|
|
| 19 |
X_REQUEST_USER = os.environ.get('X_REQUEST_USER')
|
| 20 |
X_API_KEY = os.environ.get('X_API_KEY')
|
| 21 |
|
|
|
|
|
|
|
|
|
|
| 22 |
logging.basicConfig(
|
| 23 |
level=logging.DEBUG if IS_DEV else logging.INFO,
|
| 24 |
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
| 25 |
datefmt='%Y-%m-%d %H:%M:%S'
|
| 26 |
)
|
| 27 |
|
| 28 |
+
s3client = S3(logging)
|
| 29 |
+
common = Common()
|
| 30 |
+
app = FastAPI(docs_url=None, redoc_url=None)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
@app.get("/docs", include_in_schema=False)
|
| 34 |
async def custom_swagger_ui_html():
|
| 35 |
return get_swagger_ui_html(
|
|
|
|
| 55 |
|
| 56 |
@app.get("/")
|
| 57 |
def root():
|
| 58 |
+
return Response(status=200, data='ok')
|
| 59 |
|
| 60 |
@app.get("/health")
|
| 61 |
def healthcheck():
|
| 62 |
+
return Response(status=200, data='ok')
|
| 63 |
|
| 64 |
@app.post("/image")
|
| 65 |
async def upload(
|
|
|
|
| 67 |
x_request_user: str = Header(...),
|
| 68 |
x_api_key: str = Header(...)
|
| 69 |
):
|
| 70 |
+
res = Response()
|
| 71 |
logging.info("--------------------------------")
|
| 72 |
logging.info("Received request to upload image")
|
| 73 |
|
|
|
|
| 89 |
logging.info("File uploaded successfully")
|
| 90 |
|
| 91 |
except Exception as e:
|
| 92 |
+
res.error = str(e)
|
| 93 |
+
logging.warning(f"Error during upload: {res.error}")
|
| 94 |
return {"error": "Failed to upload content"}
|
| 95 |
|
| 96 |
+
return res
|
| 97 |
|
| 98 |
def is_valid(u, p):
|
| 99 |
return u == X_REQUEST_USER and p == X_API_KEY
|
models/response.py
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydantic import BaseModel
|
| 2 |
+
|
| 3 |
+
class Response(BaseModel):
|
| 4 |
+
status: int = 500
|
| 5 |
+
data: any = None
|
| 6 |
+
error: any = None
|
requirements.txt
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
boto3
|
|
|
|
| 2 |
gradio
|
| 3 |
pillow
|
| 4 |
pydantic
|
|
|
|
| 1 |
boto3
|
| 2 |
+
botocore
|
| 3 |
gradio
|
| 4 |
pillow
|
| 5 |
pydantic
|