Skip to main content

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 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rest_api_response-0.1.1.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rest_api_response-0.1.1-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file rest_api_response-0.1.1.tar.gz.

File metadata

  • Download URL: rest_api_response-0.1.1.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for rest_api_response-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ee76ec92f1667c07daf8e940806587cc988918c1f8c3a6e908f771508e19b7ac
MD5 82829bbc48588d9134ee3fb9a75ae255
BLAKE2b-256 4d80d72b9ef86d256f29c6a4bd3f02d0100e88dc7be4f423a5615dae2250de5e

See more details on using hashes here.

File details

Details for the file rest_api_response-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for rest_api_response-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b9cab09f24b371d7495956a0ab73110aa2e0e3528ae200e1133cdc918b048180
MD5 62618ffcf96c130f93538a1cb8712849
BLAKE2b-256 accfea9467ba33b437c914138f1dcc013cf20c764d794681eeb8418602bb3696

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page