Skip to content

Commit

Permalink
rename: BaseApiResponse -> BaseAPIResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
freemindcore committed Oct 20, 2022
1 parent 73294d0 commit 91bb1bd
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 33 deletions.
18 changes: 9 additions & 9 deletions easy/controller/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from easy.controller.meta_conf import MODEL_FIELDS_ATTR_DEFAULT, ModelOptions
from easy.domain.meta import CrudModel
from easy.response import BaseApiResponse
from easy.response import BaseAPIResponse
from easy.services import BaseService
from easy.utils import copy_func

Expand Down Expand Up @@ -66,21 +66,21 @@ async def get_obj(self, request: HttpRequest, id: int) -> Any: # type: ignore
qs = await self.service.get_obj(id)
except Exception as e: # pragma: no cover
logger.error(f"Get Error - {e}", exc_info=True)
return BaseApiResponse(str(e), message="Get Failed", code=500)
return BaseAPIResponse(str(e), message="Get Failed", code=500)
if qs:
return qs
else:
return BaseApiResponse(message="Not Found", code=404)
return BaseAPIResponse(message="Not Found", code=404)

async def del_obj(self, request: HttpRequest, id: int) -> Any: # type: ignore
"""
DELETE /{id}
Delete a single Object
"""
if await self.service.del_obj(id):
return BaseApiResponse("Deleted.", code=204)
return BaseAPIResponse("Deleted.", code=204)
else:
return BaseApiResponse("Not Found.", code=404)
return BaseAPIResponse("Not Found.", code=404)

@paginate
async def get_objs(self, request: HttpRequest, filters: Optional[str] = None) -> Any: # type: ignore
Expand Down Expand Up @@ -142,9 +142,9 @@ async def add_obj( # type: ignore
"""
obj_id = await self.service.add_obj(**data.dict())
if obj_id:
return BaseApiResponse({"id": obj_id}, code=201, message="Created.")
return BaseAPIResponse({"id": obj_id}, code=201, message="Created.")
else:
return BaseApiResponse(
return BaseAPIResponse(
code=204, message="Add failed."
) # pragma: no cover

Expand All @@ -156,9 +156,9 @@ async def patch_obj( # type: ignore
Update a single object
"""
if await self.service.patch_obj(id=id, payload=data.dict()):
return BaseApiResponse(message="Updated.")
return BaseAPIResponse(message="Updated.")
else:
return BaseApiResponse(code=400, message="Updated Failed")
return BaseAPIResponse(code=400, message="Updated Failed")

DataSchema.__name__ = (
f"{model_opts.model.__name__}__AutoSchema({str(uuid.uuid4())[:4]})"
Expand Down
8 changes: 4 additions & 4 deletions easy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from easy.controller.auto_api import create_admin_controller
from easy.domain.orm import django_serializer
from easy.renderer.json import EasyJSONRenderer
from easy.response import BaseApiResponse
from easy.response import BaseAPIResponse

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -126,14 +126,14 @@ def create_response(
data = django_serializer.serialize_data(data)
except Exception as e: # pragma: no cover
logger.error(f"Creat Response Error - {e}", exc_info=True)
return BaseApiResponse(str(e), code=500)
return BaseAPIResponse(str(e), code=500)

if self.easy_output:
if temporal_response:
status = temporal_response.status_code
assert status

_temp = BaseApiResponse(
_temp = BaseAPIResponse(
data, status=status, content_type=self.get_content_type()
)

Expand All @@ -154,6 +154,6 @@ def create_response(

def create_temporal_response(self, request: HttpRequest) -> HttpResponse:
if self.easy_output:
return BaseApiResponse("", content_type=self.get_content_type())
return BaseAPIResponse("", content_type=self.get_content_type())
else:
return super().create_temporal_response(request)
2 changes: 1 addition & 1 deletion easy/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
SUCCESS_MESSAGE = "success"


class BaseApiResponse(JsonResponse):
class BaseAPIResponse(JsonResponse):
"""
Base for all API responses
"""
Expand Down
6 changes: 3 additions & 3 deletions tests/easy_app/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
IsAuthenticated,
IsSuperUser,
)
from easy.response import BaseApiResponse
from easy.response import BaseAPIResponse

from .models import Client, Event
from .schema import EventSchema
Expand Down Expand Up @@ -113,7 +113,7 @@ class APIMeta:

@http_get("/base_response/")
async def generate_base_response(self, request):
return BaseApiResponse({"data": "This is a BaseApiResponse."})
return BaseAPIResponse({"data": "This is a BaseAPIResponse."})

@http_get("/qs_paginated/", auth=None)
@paginate
Expand All @@ -136,7 +136,7 @@ async def list_events(self):
await sync_to_async(list)(qs)
if qs:
return qs
return BaseApiResponse()
return BaseAPIResponse()


@api_controller("unittest")
Expand Down
30 changes: 15 additions & 15 deletions tests/test_api_base_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,44 @@

import pytest

from easy.response import BaseApiResponse
from easy.response import BaseAPIResponse


def test_base_api_result_base():

assert BaseApiResponse("").json_data["data"] == ""
assert BaseAPIResponse("").json_data["data"] == ""

assert BaseApiResponse("1").json_data["data"] == "1"
assert BaseAPIResponse("1").json_data["data"] == "1"

assert BaseApiResponse("0").json_data["data"] == "0"
assert BaseApiResponse().json_data["data"] == {}
assert BaseApiResponse([]).json_data["data"] == []
assert BaseApiResponse(True).json_data["data"] is True
assert BaseApiResponse(False).json_data["data"] is False
assert BaseApiResponse([1, 2, 3]).json_data["data"] == [1, 2, 3]
assert BaseAPIResponse("0").json_data["data"] == "0"
assert BaseAPIResponse().json_data["data"] == {}
assert BaseAPIResponse([]).json_data["data"] == []
assert BaseAPIResponse(True).json_data["data"] is True
assert BaseAPIResponse(False).json_data["data"] is False
assert BaseAPIResponse([1, 2, 3]).json_data["data"] == [1, 2, 3]


def test_base_api_result_dict():

assert BaseApiResponse({"a": 1, "b": 2}).json_data["data"] == {
assert BaseAPIResponse({"a": 1, "b": 2}).json_data["data"] == {
"a": 1,
"b": 2,
}

assert (BaseApiResponse({"code": 2, "im": 14})).json_data["data"]["im"] == 14
assert (BaseApiResponse({"code": 2, "im": 14})).json_data["data"]["code"] == 2
assert (BaseAPIResponse({"code": 2, "im": 14})).json_data["data"]["im"] == 14
assert (BaseAPIResponse({"code": 2, "im": 14})).json_data["data"]["code"] == 2


def test_base_api_result_message():
assert (
BaseApiResponse(code=-1, message="error test").json_data["message"]
BaseAPIResponse(code=-1, message="error test").json_data["message"]
== "error test"
)
assert BaseApiResponse().json_data["message"]
assert BaseAPIResponse().json_data["message"]


def test_base_api_edit():
orig_resp = BaseApiResponse(
orig_resp = BaseAPIResponse(
{"item_id": 2, "im": 14},
code=0,
)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_async_other_apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async def test_base_response(self, transactional_db, easy_api_client):
"/base_response/",
)
assert response.status_code == 200
assert response.json().get("data")["data"] == "This is a BaseApiResponse."
assert response.json().get("data")["data"] == "This is a BaseAPIResponse."

async def test_qs_paginated(self, transactional_db, easy_api_client):
client = easy_api_client(EasyCrudAPIController)
Expand Down

0 comments on commit 91bb1bd

Please sign in to comment.