Skip to main content

A to-go-to production API payload with an easy format for building APIs with Python.

Project description

Production API Payload

A to-go-to production API payload 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-payload

2). In the file (.py) that you wish to use it, import it:

    from rest_api_payload 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:

...
    def list_of_posts(request):
        """Returns a list of posts"""
        post = Post.objects.all()
        post_serializer = PostSerializer(post, many=True)
        return Response(post_serializer.data)

The above response output 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 function to the next level by doing this:

...
from rest_api_payload import success_response


    def list_of_posts(request):
        """Returns a list of post"""
        post = Post.objects.all()
        post_serializer = PostSerializer(post, many=True)

        payload = success_response(
            status=True,
            message="Post retrieved!",
            data=post_serializer.data
        )
        return Response(data=payload, status=status.HTTP_200_OK)

The above response output 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
            }
        }
    ]

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.

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_payload-0.0.7.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

rest_api_payload-0.0.7-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file rest_api_payload-0.0.7.tar.gz.

File metadata

  • Download URL: rest_api_payload-0.0.7.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for rest_api_payload-0.0.7.tar.gz
Algorithm Hash digest
SHA256 6473dbc5cb0187faf8cdbee98b9ecff26ab973baa5e48673449b87107d8291cd
MD5 2c221485dd5d068aadeaa6a64ee1d30d
BLAKE2b-256 b1f62612c6f5e30c9de763b459a9d10b0680a39196b8859a1570d9402fa673d4

See more details on using hashes here.

File details

Details for the file rest_api_payload-0.0.7-py3-none-any.whl.

File metadata

File hashes

Hashes for rest_api_payload-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 f726e74949c3010843c3d267a5dae8301e977b583cd7528f40f813e567b0fd95
MD5 e250869927a8d5534cd534637a2b3426
BLAKE2b-256 95a2552285cfa68f3bbefcca6a14d1cd55bcacf1b057849d960a982a4a0f93c3

See more details on using hashes here.

Supported by

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