Skip to content

Commit

Permalink
Update: modified README
Browse files Browse the repository at this point in the history
  • Loading branch information
aybruhm committed Jun 25, 2023
1 parent 908f256 commit 02f2e3d
Showing 1 changed file with 31 additions and 23 deletions.
54 changes: 31 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
[
Expand All @@ -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",
Expand All @@ -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

Expand Down

0 comments on commit 02f2e3d

Please sign in to comment.