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 = {
|
||||
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
|
||||
|
21
src/main.py
21
src/main.py
@ -44,7 +44,7 @@ class MinSizePerRequestError(Exception):
|
||||
|
||||
|
||||
@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:
|
||||
if api_key not in AUTHORIZED_KEYS:
|
||||
raise HTTPException(
|
||||
@ -54,11 +54,11 @@ async def test_data(api_key: str, request: Request, size: str) -> StreamingRespo
|
||||
|
||||
try:
|
||||
size = convert_to_bytes(size)
|
||||
except ValueError:
|
||||
except ValueError as err:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail='Invalid format format for size.'
|
||||
)
|
||||
) from err
|
||||
|
||||
if size < 0:
|
||||
raise MinSizePerRequestError
|
||||
@ -78,19 +78,22 @@ async def test_data(api_key: str, request: Request, size: str) -> StreamingRespo
|
||||
return StreamingResponse(
|
||||
status_code=status.HTTP_200_OK,
|
||||
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(
|
||||
status_code=status.HTTP_416_REQUESTED_RANGE_NOT_SATISFIABLE,
|
||||
detail=f'Size has to be not-negative.'
|
||||
)
|
||||
except MaxSizePerRequestError:
|
||||
detail='Size has to be not-negative.'
|
||||
) from err
|
||||
except MaxSizePerRequestError as err:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_416_REQUESTED_RANGE_NOT_SATISFIABLE,
|
||||
detail=f'Exceeded max size per request of {MAX_SIZE} Bytes.'
|
||||
)
|
||||
) from err
|
||||
|
||||
|
||||
def main():
|
||||
|
Loading…
Reference in New Issue
Block a user