Spaces:
Running
Running
patharanor
commited on
Commit
·
b935292
1
Parent(s):
0725aa3
fix: proved request schema of fastapi
Browse files- app.py +15 -13
- models/response.py +12 -1
app.py
CHANGED
@@ -1,5 +1,7 @@
|
|
1 |
from http import HTTPStatus
|
2 |
import os, io, base64
|
|
|
|
|
3 |
from typing_extensions import Annotated
|
4 |
import uvicorn
|
5 |
import logging
|
@@ -95,17 +97,17 @@ async def upload(
|
|
95 |
logging.warning(f"Error during upload: {res.error}")
|
96 |
|
97 |
res.data = o.ref
|
98 |
-
return res
|
99 |
|
100 |
|
101 |
-
@app.post("/
|
102 |
async def upload2(
|
103 |
x_request_user: str = Header(...),
|
104 |
x_api_key: str = Header(...),
|
105 |
job_no: Annotated[str, Form()] = '',
|
106 |
s3key: Annotated[str, Form()] = '',
|
107 |
ref: Annotated[str, Form()] = '',
|
108 |
-
|
109 |
):
|
110 |
res = Response()
|
111 |
res.data = ref
|
@@ -117,31 +119,31 @@ async def upload2(
|
|
117 |
if not is_valid(x_request_user, x_api_key):
|
118 |
res.status = HTTPStatus.FORBIDDEN
|
119 |
res.error = "Invalid credentials"
|
120 |
-
return res
|
121 |
|
122 |
-
if
|
123 |
res.status = HTTPStatus.BAD_REQUEST
|
124 |
res.error = "File not found"
|
125 |
-
return res
|
126 |
|
127 |
-
key = f'{s3key}/{job_no}/{
|
128 |
logging.info(f'Key for S3 upload: {key}')
|
129 |
|
130 |
try:
|
131 |
# Read the file content directly from UploadFile
|
132 |
-
file_content = await
|
133 |
logging.info(f'File content length: {len(file_content)} bytes')
|
134 |
|
135 |
-
# Upload file object to S3
|
136 |
-
# logging.info(f"Uploading file to S3 bucket '{AWS_S3_BUCKET_NAME}' with key '{key}'")
|
137 |
-
res = s3client.upload_file(AWS_S3_BUCKET_NAME, file, key)
|
138 |
-
logging.info("File uploaded successfully")
|
139 |
|
140 |
except Exception as e:
|
141 |
res.error = str(e)
|
142 |
logging.warning(f"Error during upload: {res.error}")
|
143 |
|
144 |
-
return res
|
145 |
|
146 |
def is_valid(u, p):
|
147 |
return u == X_REQUEST_USER and p == X_API_KEY
|
|
|
1 |
from http import HTTPStatus
|
2 |
import os, io, base64
|
3 |
+
from typing import Optional
|
4 |
+
from fastapi.responses import JSONResponse
|
5 |
from typing_extensions import Annotated
|
6 |
import uvicorn
|
7 |
import logging
|
|
|
97 |
logging.warning(f"Error during upload: {res.error}")
|
98 |
|
99 |
res.data = o.ref
|
100 |
+
return res.json()
|
101 |
|
102 |
|
103 |
+
@app.post("/image-multiparts")
|
104 |
async def upload2(
|
105 |
x_request_user: str = Header(...),
|
106 |
x_api_key: str = Header(...),
|
107 |
job_no: Annotated[str, Form()] = '',
|
108 |
s3key: Annotated[str, Form()] = '',
|
109 |
ref: Annotated[str, Form()] = '',
|
110 |
+
fileb: Optional[UploadFile] = File(None),
|
111 |
):
|
112 |
res = Response()
|
113 |
res.data = ref
|
|
|
119 |
if not is_valid(x_request_user, x_api_key):
|
120 |
res.status = HTTPStatus.FORBIDDEN
|
121 |
res.error = "Invalid credentials"
|
122 |
+
return res.json()
|
123 |
|
124 |
+
if fileb is None:
|
125 |
res.status = HTTPStatus.BAD_REQUEST
|
126 |
res.error = "File not found"
|
127 |
+
return res.json()
|
128 |
|
129 |
+
key = f'{s3key}/{job_no}/{fileb.filename}'
|
130 |
logging.info(f'Key for S3 upload: {key}')
|
131 |
|
132 |
try:
|
133 |
# Read the file content directly from UploadFile
|
134 |
+
file_content = await fileb.read()
|
135 |
logging.info(f'File content length: {len(file_content)} bytes')
|
136 |
|
137 |
+
# # Upload file object to S3
|
138 |
+
# # logging.info(f"Uploading file to S3 bucket '{AWS_S3_BUCKET_NAME}' with key '{key}'")
|
139 |
+
# res = s3client.upload_file(AWS_S3_BUCKET_NAME, file, key)
|
140 |
+
# logging.info("File uploaded successfully")
|
141 |
|
142 |
except Exception as e:
|
143 |
res.error = str(e)
|
144 |
logging.warning(f"Error during upload: {res.error}")
|
145 |
|
146 |
+
return res.json()
|
147 |
|
148 |
def is_valid(u, p):
|
149 |
return u == X_REQUEST_USER and p == X_API_KEY
|
models/response.py
CHANGED
@@ -1,7 +1,18 @@
|
|
1 |
from typing import Any
|
|
|
2 |
from pydantic import BaseModel
|
3 |
|
4 |
class Response(BaseModel):
|
5 |
status: int = 500
|
6 |
data: Any = None
|
7 |
-
error: Any = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from typing import Any
|
2 |
+
from fastapi.responses import JSONResponse
|
3 |
from pydantic import BaseModel
|
4 |
|
5 |
class Response(BaseModel):
|
6 |
status: int = 500
|
7 |
data: Any = None
|
8 |
+
error: Any = None
|
9 |
+
|
10 |
+
def json(self):
|
11 |
+
return JSONResponse(
|
12 |
+
content={
|
13 |
+
"status": self.status,
|
14 |
+
"data": self.data,
|
15 |
+
"error": self.error,
|
16 |
+
},
|
17 |
+
status_code=self.status
|
18 |
+
)
|