From fa58c5822650b50e82604cf51a2f99c171352d1c Mon Sep 17 00:00:00 2001 From: Abram Date: Sun, 25 Jun 2023 20:04:19 +0100 Subject: [PATCH 1/7] Update: modified package name, versionn, description and url --- setup.cfg | 10 +++++----- setup.py | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/setup.cfg b/setup.cfg index 800a476..ed58e31 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,14 +1,14 @@ [metadata] -name = rest-api-payload-israelabraham -version = 0.0.7 +name = rest-api-response +version = 0.1 author = Abram author_email = israelvictory87@gmail.com -description = A to-go-to production API payload with an easy format for building APIs with Python. +description = A go-to production API response with an easy format for building APIs with Python. long_description = file: README.md long_description_content_type = text/markdown -url = https://github.com/israelabraham/API-Payload +url = https://github.com/israelabraham/api-response project_urls = - Bug Tracker = https://github.com/israelabraham/API-Payload/issues + Bug Tracker = https://github.com/israelabraham/api-response/issues classifiers = Development Status :: 5 - Production/Stable Intended Audience :: Education diff --git a/setup.py b/setup.py index 02032c1..9e8f03d 100644 --- a/setup.py +++ b/setup.py @@ -9,16 +9,16 @@ ] setup( - name="rest_api_payload", - version="0.0.7", - description="A to-go-to production API payload with an easy format for building APIs with Python.", + name="rest_api_response", + version="0.1", + description="A go-to production API response with an easy format for building APIs with Python.", # noqa: E501 long_description_content_type="text/markdown", long_description=open("README.md").read(), - url="https://github.com/israelabraham/API-Payload", + url="https://github.com/israelabraham/api-response", author="Abram", author_email="israelvictory87@gmail.com", license="MIT", classifiers=classifiers, - keywords=["api", "payload", "custom api payload"], + keywords=["api", "response", "custom api response"], packages=find_packages(), ) From ff1b4632e3a6e932cef368e5289c235adf9c3f03 Mon Sep 17 00:00:00 2001 From: Abram Date: Sun, 25 Jun 2023 20:25:55 +0100 Subject: [PATCH 2/7] Refactor: renamed directory from *_payload to *_response --- rest_api_payload/__init__.py | 41 ---------------------------------- rest_api_response/__init__.py | 42 +++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 41 deletions(-) delete mode 100644 rest_api_payload/__init__.py create mode 100644 rest_api_response/__init__.py diff --git a/rest_api_payload/__init__.py b/rest_api_payload/__init__.py deleted file mode 100644 index 445460a..0000000 --- a/rest_api_payload/__init__.py +++ /dev/null @@ -1,41 +0,0 @@ -# Typing Imports -from typing import Dict, Union - - -def error_response(status: bool, message: str) -> Dict[str, Union[bool, str]]: - """ - Custom error response - - :param status: This is a boolean value that indicates whether the request was not successful - :type status: bool - - :param message: This is the message you want to send to the user - :type message: str - - :return: A dictionary with the keys status and message. - """ - - response = {"status": status, "message": message} - return response - - -def success_response( - status: bool, message: str, data: dict = {} -) -> Dict[str, Union[bool, str, dict]]: - """ - Custom success response - - :param status: This is a boolean value that indicates whether the request was successful - :type status: bool - - :param message: This is the message you want to send to the user - :type message: str - - :param data: This is the data that you want to return to the client - :type data: dict - - :return: A dictionary with the keys status, message, and data. - """ - - response = {"status": status, "message": message, "data": data} - return response diff --git a/rest_api_response/__init__.py b/rest_api_response/__init__.py new file mode 100644 index 0000000..1f05a14 --- /dev/null +++ b/rest_api_response/__init__.py @@ -0,0 +1,42 @@ +# Typing Imports +from typing import Dict, Union + + +def error_response( + message: str, meta: dict = {} +) -> Dict[str, Union[bool, str, dict]]: + """ + Custom error response + + :param message: The error message you want to send to the user + :type message: str + + :param meta: Additional information/context to the error + :type meta: dict + + :return: response + :rtype: dict + """ + + response = {"status": False, "message": message, "meta": meta} + return response + + +def success_response( + message: str, data: dict = {} +) -> Dict[str, Union[bool, str, dict]]: + """ + Custom success response + + :param message: The success message you want to send to the user + :type message: str + + :param data: Data that you would be returned to the client + :type data: dict + + :return: response + :rtype: dict + """ + + response = {"status": True, "message": message, "data": data} + return response From 632bf30b37fa7fa1e03526f9904b3b25853640cc Mon Sep 17 00:00:00 2001 From: Abram Date: Sun, 25 Jun 2023 20:26:53 +0100 Subject: [PATCH 3/7] Update: modified test cases --- tests/test_init.py | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/tests/test_init.py b/tests/test_init.py index 0871137..f59d993 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -1,38 +1,36 @@ import unittest -from rest_api_payload import success_response, error_response +from rest_api_response import success_response, error_response class TestInit(unittest.TestCase): + """Parent test case""" + def test_error_response(self): + payload = {"message": "Error!", "meta": {}} + response = error_response(message=payload["message"]) - payload = {"status": False, "message": "BAD REQUEST!"} - self.assertEqual( - payload, - error_response( - status=payload["status"], message=payload["message"] - ), - ) + self.assertEqual(response["status"], False) + response.pop("status") + self.assertEqual(payload, response) def test_success_response(self): - payload = { - "status": True, - "message": "OKKK!", + "message": "Successful!", "data": { "title": "title 1", "content": "Some text here", "author": 1, }, } - self.assertEqual( - payload, - success_response( - status=payload["status"], - message=payload["message"], - data=payload["data"], - ), + response = success_response( + message=payload["message"], + data=payload["data"], ) + self.assertEqual(response["status"], True) + response.pop("status") + self.assertEqual(payload, response) + if __name__ == "__main__": unittest.main() From 5f49a7b9edea11b63d18555896afe9d159ba40fa Mon Sep 17 00:00:00 2001 From: Abram Date: Sun, 25 Jun 2023 20:27:32 +0100 Subject: [PATCH 4/7] Feat: created package-test workflow file --- .github/workflows/package-test.yml | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 .github/workflows/package-test.yml diff --git a/.github/workflows/package-test.yml b/.github/workflows/package-test.yml new file mode 100644 index 0000000..898696d --- /dev/null +++ b/.github/workflows/package-test.yml @@ -0,0 +1,19 @@ +name: Test API Response Package + +on: + pull_request: + branches: [ "main" ] + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Set up Python 3.9 + uses: actions/setup-python@v3 + with: + python-version: "3.9" + + - name: Test with unittest + run: python -m unittest tests/test_init.py \ No newline at end of file From 908f2566389d29e59d2abbdc4ee7d1957f82abaf Mon Sep 17 00:00:00 2001 From: Abram Date: Sun, 25 Jun 2023 20:28:01 +0100 Subject: [PATCH 5/7] Refactor: renamed python-publish to package-publish --- .github/workflows/{python-publish.yml => package-publish.yml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .github/workflows/{python-publish.yml => package-publish.yml} (100%) diff --git a/.github/workflows/python-publish.yml b/.github/workflows/package-publish.yml similarity index 100% rename from .github/workflows/python-publish.yml rename to .github/workflows/package-publish.yml From 02f2e3dc1ddb87fc7c71a9934bdfa8ef9c239d05 Mon Sep 17 00:00:00 2001 From: Abram Date: Sun, 25 Jun 2023 20:34:37 +0100 Subject: [PATCH 6/7] Update: modified README --- README.md | 54 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 7b806af..cd402db 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -# Production API Payload +# Production API Response -A to-go-to production API payload with an easy format for building APIs with Python. +A go-to production API response with an easy format for building APIs with Python. ## Quickstart @@ -9,31 +9,36 @@ To get it running, follow the steps below: 1). Pip install the package in your project terminal: ```bash -pip install rest-api-payload +pip install rest-api-response ``` 2). In the file (.py) that you wish to use it, import it: ```python - from rest_api_payload import success_response, error_response + from rest_api_response import success_response, error_response ``` That's pretty much it - you can now call the function and pass the required arguments! ## Example -Suppose you have a function that returns a response to the client: +Suppose you have an API class that returns a list of blog posts to a client: ```python +# imports goes here ... - def list_of_posts(request): +class PostListAPIView(views.APIView): + serializer_class = PostSerializer + + def get(self, request): """Returns a list of posts""" - post = Post.objects.all() - post_serializer = PostSerializer(post, many=True) - return Response(post_serializer.data) + + posts = Post.objects.all() + serializer = self.serializer_class(posts, many=True) + return Response(serializer.data) ``` -The above response output would be: +The API response would be: ```json [ @@ -55,31 +60,34 @@ The above response output would be: ] ``` -This works too, but let's take the function to the next level by doing this: +This works too, but let's take the response to the next level by doing this: ```python +# imports goes here ... -from rest_api_payload import success_response +from rest_api_response import success_response - def list_of_posts(request): - """Returns a list of post""" - post = Post.objects.all() - post_serializer = PostSerializer(post, many=True) +class PostListAPIView(views.APIView): + serializer_class = PostSerializer + + def get(self, request): + """Returns a list of posts""" - payload = success_response( - status=True, + posts = Post.objects.all() + serializer = self.serializer_class(posts, many=True) + _response = success_response( message="Post retrieved!", - data=post_serializer.data + data=serializer.data ) - return Response(data=payload, status=status.HTTP_200_OK) + return Response(data=_response, status=status.HTTP_200_OK) ``` -The above response output would be: +The API response would be: ```json [ "status": true, - "message":"Posts retrieved!", + "message": "Posts retrieved!", "data": { { "title": "First blog post", @@ -100,7 +108,7 @@ The above response output would be: ] ``` -*I built this payload because of a project I took lead in building from scratch - and literally had to sympathize with the frontend (web and mobile) engineers. I hope you find this package useful, kindly leave a star if you did.* +And that's it. You have a nicely catchy response. :-) ## Contribute From f22ebfac2c90786b389c6f0ab8e31040a251d5db Mon Sep 17 00:00:00 2001 From: Abram Date: Sun, 25 Jun 2023 20:37:02 +0100 Subject: [PATCH 7/7] Update: added 0.1 changelog --- CHANGELOG.txt | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 1fddac7..d4da856 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -40,4 +40,12 @@ Change Log -------------------- - Modified type hints - Update unit test -- Bump version to 0.0.7 \ No newline at end of file +- Bump version to 0.0.7 + +0.1 (25/06/2023) +------------------- +- Modified response functions +- Modified test cases +- Renamed package name, changed description and url +- Modified README documentation +- Bump version to 0.1 \ No newline at end of file