added header and small fixes

This commit is contained in:
Kristian Krsnik 2024-07-05 20:09:52 +02:00
parent 9e83da6b1a
commit c191f983fe
Signed by: Kristian
GPG Key ID: FD1330AC9F909E85
2 changed files with 13 additions and 10 deletions

View File

@ -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";

View File

@ -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():