Skip to content

Commit c1bae4a

Browse files
committed
refactor: BaseApiResponse to use code instead of errno
1 parent 40107b7 commit c1bae4a

File tree

4 files changed

+14
-14
lines changed

4 files changed

+14
-14
lines changed

easy/controller/meta.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,21 @@ async def get_obj(self, request: HttpRequest, id: int) -> Any: # type: ignore
6666
qs = await self.service.get_obj(id)
6767
except Exception as e: # pragma: no cover
6868
logger.error(f"Get Error - {e}", exc_info=True)
69-
return BaseApiResponse(str(e), message="Get Failed", errno=500)
69+
return BaseApiResponse(str(e), message="Get Failed", code=500)
7070
if qs:
7171
return qs
7272
else:
73-
return BaseApiResponse(message="Not Found", errno=404)
73+
return BaseApiResponse(message="Not Found", code=404)
7474

7575
async def del_obj(self, request: HttpRequest, id: int) -> Any: # type: ignore
7676
"""
7777
DELETE /{id}
7878
Delete a single Object
7979
"""
8080
if await self.service.del_obj(id):
81-
return BaseApiResponse("Deleted.", errno=204)
81+
return BaseApiResponse("Deleted.", code=204)
8282
else:
83-
return BaseApiResponse("Not Found.", errno=404)
83+
return BaseApiResponse("Not Found.", code=404)
8484

8585
@paginate
8686
async def get_objs(self, request: HttpRequest, filters: str = None) -> Any: # type: ignore
@@ -131,9 +131,9 @@ async def add_obj( # type: ignore
131131
"""
132132
obj_id = await self.service.add_obj(**data.dict())
133133
if obj_id:
134-
return BaseApiResponse({"id": obj_id}, errno=201)
134+
return BaseApiResponse({"id": obj_id}, code=201)
135135
else:
136-
return BaseApiResponse("Add failed", errno=204) # pragma: no cover
136+
return BaseApiResponse("Add failed", code=204) # pragma: no cover
137137

138138
async def patch_obj( # type: ignore
139139
self, request: HttpRequest, id: int, data: DataSchema
@@ -145,7 +145,7 @@ async def patch_obj( # type: ignore
145145
if await self.service.patch_obj(id=id, payload=data.dict()):
146146
return BaseApiResponse("Updated.")
147147
else:
148-
return BaseApiResponse("Update Failed", errno=400)
148+
return BaseApiResponse("Update Failed", code=400)
149149

150150
DataSchema.__name__ = (
151151
f"{model_opts.model.__name__}__AutoSchema({str(uuid.uuid4())[:4]})"

easy/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def create_response(
126126
data = django_serializer.serialize_data(data)
127127
except Exception as e: # pragma: no cover
128128
logger.error(f"Creat Response Error - {e}", exc_info=True)
129-
return BaseApiResponse(str(e), errno=500)
129+
return BaseApiResponse(str(e), code=500)
130130

131131
if self.easy_output:
132132
if temporal_response:

easy/response.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ class BaseApiResponse(JsonResponse):
1818
def __init__(
1919
self,
2020
data: Union[Dict, str] = None,
21-
errno: int = None,
21+
code: int = None,
2222
message: str = None,
2323
**kwargs: Any
2424
):
25-
if errno:
25+
if code:
2626
message = message or UNKNOWN_ERROR_MSG
2727
else:
2828
message = SUCCESS_MESSAGE
29-
errno = ERRNO_SUCCESS
29+
code = ERRNO_SUCCESS
3030

3131
_data: Union[Dict, str] = {
32-
"code": errno,
32+
"code": code,
3333
"message": message,
3434
"data": data if data is not None else {},
3535
}

tests/test_api_base_response.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def test_base_api_result_dict():
3232

3333
def test_base_api_result_message():
3434
assert (
35-
BaseApiResponse(errno=-1, message="error test").json_data["message"]
35+
BaseApiResponse(code=-1, message="error test").json_data["message"]
3636
== "error test"
3737
)
3838
assert BaseApiResponse().json_data["message"]
@@ -41,7 +41,7 @@ def test_base_api_result_message():
4141
def test_base_api_edit():
4242
orig_resp = BaseApiResponse(
4343
{"item_id": 2, "im": 14},
44-
errno=0,
44+
code=0,
4545
)
4646

4747
with pytest.raises(KeyError):

0 commit comments

Comments
 (0)