diff --git a/backend/api/endpoints/command.py b/backend/api/endpoints/command.py index 6da4497..f0cbb01 100644 --- a/backend/api/endpoints/command.py +++ b/backend/api/endpoints/command.py @@ -40,15 +40,17 @@ def create_command(payload: CommandRequest, db: Session = Depends(get_db)): @return returns a json object with field of "data" under which there is the payload now pulled from the database """ # TODO:(Member) Implement this endpoint - new_command = Command(command_type=payload.command_type, params=payload.params) - - db.add(new_command) - db.commit() - db.refresh(new_command) - - - return {"data": new_command} - + try: + new_command = Command(command_type=payload.command_type, params=payload.params) + + db.add(new_command) + db.commit() + db.refresh(new_command) + + return {"data": new_command} + except Exception as e: + db.rollback() + raise HTTPException(status_code=500, detail="Fail to create command {str(e)}") diff --git a/backend/api/middlewares/logger_middleware.py b/backend/api/middlewares/logger_middleware.py index 6813824..dcf4070 100644 --- a/backend/api/middlewares/logger_middleware.py +++ b/backend/api/middlewares/logger_middleware.py @@ -1,4 +1,4 @@ -import time +from time import time, strftime from collections.abc import Callable from typing import Any from fastapi import Request, Response @@ -20,14 +20,18 @@ async def dispatch( @return Response from endpoint """ # TODO:(Member) Finish implementing this method - start_time = time.time() - request_time = time.strftime('%Y-%m-%d %H:%M:%S') + start_time = time() + request_time = strftime('%Y-%m-%d %H:%M:%S') logger.info(f"Request started: [{request_time}] | {request.method} | {request.url.path} | Params: {request.query_params}") response = await call_next(request) + raw_byte = b"" + async for chunk in response.body_iterator: + raw_byte += chunk + # Log response details and duration - duration = time.time() - start_time + duration = time() - start_time - logger.info(f"Response sent: {response.status_code} | Duration: {duration:.4f}s") - return response + logger.info(f"Response sent: {raw_byte} {response.status_code} | Duration: {duration:.4f}s") + return Response(content=raw_byte, status_code=response.status_code, headers=dict(response.headers), media_type=response.media_type) diff --git a/backend/data/data_models.py b/backend/data/data_models.py index bceaf55..583ccad 100644 --- a/backend/data/data_models.py +++ b/backend/data/data_models.py @@ -39,15 +39,15 @@ def validate_params_format(self): The format of the comma seperated values is "data1,data2" so no spaces between data and the commas. """ # TODO: (Member) Implement this method - if self.params == None and self.format == None: + if self.params is None and self.format is None: return self - elif self.params == None or self.format == None: - raise ValueError() + elif self.params is None or self.format is None: + raise ValueError("One of params or format is not initialized.") if len(self.params.split(",")) == len(self.format.split(",")): return self else: - raise ValueError() + raise ValueError("Number of comma separated value of params and format does not match.")