A go-to production API response with an easy format for building APIs with Python.
Project description
Production API Response
A go-to production API response with an easy format for building APIs with Python.
Quickstart
To get it running, follow the steps below:
1). Pip install the package in your project terminal:
pip install rest-api-response
2). In the file (.py) that you wish to use it, import it:
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 an API class that returns a list of blog posts to a client:
# imports goes here
...
class PostListAPIView(views.APIView):
serializer_class = PostSerializer
def get(self, request):
"""Returns a list of posts"""
posts = Post.objects.all()
serializer = self.serializer_class(posts, many=True)
return Response(serializer.data)
The API response would be:
[
{
"title": "First blog post",
"content": "Lorem ipsume content",
"author": 1
},
{
"title": "Second blog post",
"content": "Lorem ipsume content",
"author": 2
},
{
"title": "Third blog post",
"content": "Lorem ipsume content",
"author": 3
}
]
This works too, but let's take the response to the next level by doing this:
# imports goes here
...
from rest_api_response import success_response
class PostListAPIView(views.APIView):
serializer_class = PostSerializer
def get(self, request):
"""Returns a list of posts"""
posts = Post.objects.all()
serializer = self.serializer_class(posts, many=True)
_response = success_response(
message="Post retrieved!",
data=serializer.data
)
return Response(data=_response, status=status.HTTP_200_OK)
The API response would be:
[
"status": true,
"message": "Posts retrieved!",
"data": [
{
"title": "First blog post",
"content": "Lorem ipsume content",
"author": 1
},
{
"title": "Second blog post",
"content": "Lorem ipsume content",
"author": 2
},
{
"title": "Third blog post",
"content": "Lorem ipsume content",
"author": 3
}
]
]
And that's it. You have a nicely catchy response. :-)
Contribute
All contributions are welcome:
- Read the issues, Fork the project and do a Pull Request.
- Request a new topic creating a
New issuewith theenhancementtag. - Find any kind of errors in the
READMEand create aNew issuewith the details or fork the project and do a Pull Request. - Suggest a better or more pythonic way for existing examples.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rest_api_response-0.2.tar.gz.
File metadata
- Download URL: rest_api_response-0.2.tar.gz
- Upload date:
- Size: 3.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0af8bf10e920448daa09951fcc3e5d3d6d5289712a2cc9cad2562416680b8b53
|
|
| MD5 |
5629d6f2dbd6ba9eded8eaef78f49aad
|
|
| BLAKE2b-256 |
0f12186e77d9bfde26e61cca14c0353cd6f7449487d53fcad8c057ea8436af29
|
File details
Details for the file rest_api_response-0.2-py3-none-any.whl.
File metadata
- Download URL: rest_api_response-0.2-py3-none-any.whl
- Upload date:
- Size: 3.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7933c9bfc5468c443256193c04bc3bc5fd3194d70260ce48e6c630045c27dd53
|
|
| MD5 |
66ab540a9099f0478d9f70facad3100f
|
|
| BLAKE2b-256 |
2def33414f942a36789c1c65093668bd26d84e35b1b17d2340ad8325b2d416fa
|