added header and small fixes
This commit is contained in:
parent
9e83da6b1a
commit
c191f983fe
@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
description = "PLACEHOLDER";
|
description = "A webserver to create files for tetsing purposes";
|
||||||
|
|
||||||
inputs = {
|
inputs = {
|
||||||
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
|
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
|
||||||
|
21
src/main.py
21
src/main.py
@ -44,7 +44,7 @@ class MinSizePerRequestError(Exception):
|
|||||||
|
|
||||||
|
|
||||||
@api.get('/')
|
@api.get('/')
|
||||||
async def test_data(api_key: str, request: Request, size: str) -> StreamingResponse:
|
async def test_data(api_key: str, size: str) -> StreamingResponse:
|
||||||
try:
|
try:
|
||||||
if api_key not in AUTHORIZED_KEYS:
|
if api_key not in AUTHORIZED_KEYS:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
@ -54,11 +54,11 @@ async def test_data(api_key: str, request: Request, size: str) -> StreamingRespo
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
size = convert_to_bytes(size)
|
size = convert_to_bytes(size)
|
||||||
except ValueError:
|
except ValueError as err:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_400_BAD_REQUEST,
|
status_code=status.HTTP_400_BAD_REQUEST,
|
||||||
detail='Invalid format format for size.'
|
detail='Invalid format format for size.'
|
||||||
)
|
) from err
|
||||||
|
|
||||||
if size < 0:
|
if size < 0:
|
||||||
raise MinSizePerRequestError
|
raise MinSizePerRequestError
|
||||||
@ -78,19 +78,22 @@ async def test_data(api_key: str, request: Request, size: str) -> StreamingRespo
|
|||||||
return StreamingResponse(
|
return StreamingResponse(
|
||||||
status_code=status.HTTP_200_OK,
|
status_code=status.HTTP_200_OK,
|
||||||
content=generate_data(size, BUFFER_SIZE),
|
content=generate_data(size, BUFFER_SIZE),
|
||||||
media_type='application/octet-stream'
|
media_type='application/octet-stream',
|
||||||
|
headers={
|
||||||
|
'Content-Length': size
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
except MinSizePerRequestError:
|
except MinSizePerRequestError as err:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_416_REQUESTED_RANGE_NOT_SATISFIABLE,
|
status_code=status.HTTP_416_REQUESTED_RANGE_NOT_SATISFIABLE,
|
||||||
detail=f'Size has to be not-negative.'
|
detail='Size has to be not-negative.'
|
||||||
)
|
) from err
|
||||||
except MaxSizePerRequestError:
|
except MaxSizePerRequestError as err:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_416_REQUESTED_RANGE_NOT_SATISFIABLE,
|
status_code=status.HTTP_416_REQUESTED_RANGE_NOT_SATISFIABLE,
|
||||||
detail=f'Exceeded max size per request of {MAX_SIZE} Bytes.'
|
detail=f'Exceeded max size per request of {MAX_SIZE} Bytes.'
|
||||||
)
|
) from err
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
Loading…
Reference in New Issue
Block a user