Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async def root():
User(id=1, name="John Doe"),
User(id=2, name="Jane Boe")
]
return await BulkResponse(data=ls).as_json_response()
return BulkResponse(data=ls).as_json_response()

app.include_router(router)
if __name__=="__main__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class ForbiddenException(ClientErrorException):
def __init__(self, message: str = None) -> None:
if message:
errors = ErrorDetail(message=message)
errors = [ErrorDetail(message=message)]
else:
errors = None
super().__init__(http_status=403, errors=errors)
6 changes: 3 additions & 3 deletions src/basalam/backbone_api/exceptions/client_error/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ def map_exception_to_response(exception: ClientErrorException, response_class: T
response_data = exception.response_data
errors = [error_class(message=data.message, code=data.code, fields=data.fields) for data in response_data]
if is_conflict:
return response_class(errors=errors, data=response_data[0].data).as_json_response()
return response_class(errors=errors).as_json_response()
return response_class(message=exception.message, errors=errors, data=response_data[0].data).as_json_response()
return response_class(message=exception.message, errors=errors).as_json_response()


async def client_error_exception_handler(request: Request, exception: ClientErrorException):
def client_error_exception_handler(request: Request, exception: ClientErrorException):
"""
Handles client error exceptions and maps them to a JSON response.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class NotFoundException(ClientErrorException):
def __init__(self, message: str = None) -> None:
if message:
errors = ErrorDetail(message=message)
errors = [ErrorDetail(message=message)]
else:
errors = None
super().__init__(http_status=404, errors=errors)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class UnauthorizedException(ClientErrorException):
def __init__(self, message: str = None) -> None:
if message:
errors = ErrorDetail(message=message)
errors = [ErrorDetail(message=message)]
else:
errors = None
super().__init__(http_status=401, errors=errors)
4 changes: 2 additions & 2 deletions src/basalam/backbone_api/responses/client_error/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@

class Error(BaseModel):
code: int | None = 0
message: str
message: str | None = None


class Base400Response(ResponseModelAbstract):
http_status: int = 400
message: str
errors: List[Error] | None

async def as_json_response(self) -> JSONResponse:
def as_json_response(self) -> JSONResponse:
return JSONResponse(
content=self.model_dump(),
status_code=HTTP_400_BAD_REQUEST
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ConflictResponse(Base400Response, Generic[T]):
errors: List[ExtendedError]
data: List[T]

async def as_json_response(self) -> JSONResponse:
def as_json_response(self) -> JSONResponse:
return JSONResponse(
content=self.model_dump(),
status_code=HTTP_409_CONFLICT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


class ForbiddenResponse(Base400Response):
async def as_json_response(self) -> JSONResponse:
def as_json_response(self) -> JSONResponse:
return JSONResponse(
content=self.model_dump(),
status_code=HTTP_403_FORBIDDEN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


class NotFoundResponse(Base400Response):
async def as_json_response(self) -> JSONResponse:
def as_json_response(self) -> JSONResponse:
return JSONResponse(
content=self.model_dump(),
status_code=HTTP_404_NOT_FOUND
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


class UnauthorizedResponse(Base400Response):
async def as_json_response(self) -> JSONResponse:
def as_json_response(self) -> JSONResponse:
return JSONResponse(
content=self.model_dump(),
status_code=HTTP_401_UNAUTHORIZED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ExtendedError(Error):
class UnprocessableContentResponse(Base400Response):
errors: List[ExtendedError]

async def as_json_response(self) -> JSONResponse:
def as_json_response(self) -> JSONResponse:
return JSONResponse(
content=self.model_dump(),
status_code=HTTP_422_UNPROCESSABLE_ENTITY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
class ResponseModelAbstract(BaseModel, ABC):

@abstractmethod
async def as_json_response(self) -> JSONResponse:
def as_json_response(self) -> JSONResponse:
raise NotImplementedError
2 changes: 1 addition & 1 deletion src/basalam/backbone_api/responses/successful/bulk.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
class BulkResponse(ResponseModelAbstract, Generic[T]):
data: List[T]

async def as_json_response(self) -> JSONResponse:
def as_json_response(self) -> JSONResponse:
return JSONResponse(content=self.model_dump(), status_code=HTTP_200_OK)
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def check_cursor(cls, values):
values['next_cursor'] = Cursor.decode_cursor(cursor)
return values

async def as_json_response(self) -> JSONResponse:
def as_json_response(self) -> JSONResponse:
return JSONResponse(content=self.model_dump(), status_code=HTTP_200_OK)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ class MultiStatusResponse(ResponseModelAbstract, Generic[T]):
success_count: int
failure_count: int

async def as_json_response(self) -> JSONResponse:
def as_json_response(self) -> JSONResponse:
return JSONResponse(content=self.model_dump(), status_code=HTTP_207_MULTI_STATUS)
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class OffsetPaginationResponse(ResponseModelAbstract, Generic[T]):
result_count: int
per_page: int

async def as_json_response(self) -> JSONResponse:
def as_json_response(self) -> JSONResponse:
return JSONResponse(content=self.model_dump(), status_code=HTTP_200_OK)


Expand Down