-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
136 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
build/ | ||
dist/ | ||
dist/ | ||
__pycache__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
Metadata-Version: 2.1 | ||
Name: rest-api-payload | ||
Version: 0.0.4 | ||
Summary: Industry ready custom API payload with an easy format for building Python APIs (Django/Django Rest Framework) | ||
Home-page: https://github.com/israelabraham/API-Payload | ||
Author: Abram 🐼 | ||
Author-email: israelvictory87@gmail.com | ||
License: MIT | ||
Project-URL: Bug Tracker, https://github.com/israelabraham/API-Payload/issues | ||
Description: Industry ready custom API payload with an easy format for building Python APIs (Django/Django Rest Framework) | ||
|
||
Yosh! If you are a django backend developer and have used the amazing utility toolkit for creating amazing APIs. You probably have come across the way django rest framework lets you return data by default. Not industry standard, if I may add. | ||
|
||
So you serialize what object you want, and it serialized and you return it like so; | ||
|
||
``` | ||
class GetPostsAPiView(APIView): | ||
|
||
def get(self, request): | ||
posts = Post.objects.all() | ||
post_serializer = PostSerializer(posts, many=True) | ||
|
||
if post_serializer.is_valid(): | ||
post_serializer.save() | ||
return Response(serializer.data) | ||
|
||
else: | ||
return Response(serializer.errors) | ||
|
||
|
||
# OUTPUT | ||
----------- | ||
- success response | ||
{ | ||
'title': 'First blog post', | ||
'content': 'Lorem ipsume content', | ||
'author': 1 | ||
} | ||
|
||
- error response | ||
{ | ||
['title']: 'field is required' | ||
} | ||
|
||
``` | ||
|
||
Does the above ouput makes sense to you? I mean, it clearly doesn't help the frontend devs, trying putting yourself in their shoes. Imagine the dev is trying to check for the status code in the data been outputted, and the above is what the developer got. Dang! Extra work, right? I happen to work with a mobile developer, and he changed the way I build APIs. So instead of the above way, what do you think of this; | ||
|
||
``` | ||
from module import success_response, error_response | ||
|
||
|
||
class GetPostsAPiView(APIView): | ||
|
||
def get(self, request): | ||
posts = Post.objects.all() | ||
post_serializer = PostSerializer(posts, many=True) | ||
|
||
if post_serializer.is_valid(): | ||
post_serializer.save() | ||
|
||
payload = success_response( | ||
status="200 ok", | ||
message="All the posts don come, chief!" | ||
data=serializer.data | ||
) | ||
return Response(data=payload) | ||
|
||
else: | ||
payload = error_response( | ||
status="400 bad request", | ||
message="Something went wrong, chief! Try again sometime" | ||
) | ||
return Response(data=payload) | ||
|
||
|
||
# OUTPUT | ||
----------- | ||
- success response | ||
{ | ||
'status': '200 ok', | ||
'message':'All the posts don come, chief!', | ||
'data': {'title': 'First blog post', 'content': 'Lorem ipsume content', 'author': 1}' | ||
} | ||
|
||
- error response | ||
{ | ||
'status': '400 bad request', | ||
'message': 'Ahh, chief, nothing dey here oooo!', | ||
'data': {['title']: 'field is required'} | ||
} | ||
|
||
``` | ||
|
||
What do you think about the above? Pretty neat and industry standard, right? Installing the package is pretty is easy, fam. Here's how to: | ||
|
||
> pip install rest-api-payload | ||
|
||
In the file (.py) that you wish to use it, import it. <br> | ||
|
||
> from rest_api_payload import success_response, error_response | ||
|
||
And that's all, you can begin calling the function and passing arguments! | ||
|
||
<br> | ||
|
||
## Contribute | ||
|
||
All contributions are welcome: | ||
|
||
- Read the issues, Fork the project and do a Pull Request. | ||
- Request a new topic creating a `New issue` with the `enhancement` tag. | ||
- Find any kind of errors in the `README` and create a `New issue` with the details or fork the project and do a Pull Request. | ||
- Suggest a better or more pythonic way for existing examples. | ||
Keywords: api,payload,custom api payload | ||
Platform: UNKNOWN | ||
Classifier: Development Status :: 5 - Production/Stable | ||
Classifier: Intended Audience :: Education | ||
Classifier: Intended Audience :: Developers | ||
Classifier: License :: OSI Approved :: MIT License | ||
Classifier: Programming Language :: Python :: 3 | ||
Requires-Python: >=3 | ||
Description-Content-Type: text/markdown |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
README.md | ||
pyproject.toml | ||
setup.cfg | ||
setup.py | ||
rest_api_payload/__init__.py | ||
rest_api_payload.egg-info/PKG-INFO | ||
rest_api_payload.egg-info/SOURCES.txt | ||
rest_api_payload.egg-info/dependency_links.txt | ||
rest_api_payload.egg-info/top_level.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
rest_api_payload |