Skip to main content

Production API Response

Package Code Quality Package Published to PypI

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.

Metadata

Release files for rest-api-response 0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rest-api-response 0.2
File Size Uploaded Provenance
rest_api_response-0.2.tar.gz 3.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for rest-api-response 0.2
File Interpreter ABI Platform
rest_api_response-0.2-py3-none-any.whl Python 3 none any Details

Total release size: 7.6 kB

Release files / rest_api_response-0.2.tar.gz

Download URL rest_api_response-0.2.tar.gz
Size 3.7 kB
Tags Source
SHA-256 checksum
How to use checksums
0af8bf10e920448daa09951fcc3e5d3d6d5289712a2cc9cad2562416680b8b53
BLAKE2b-256 checksum
How to use checksums
0f12186e77d9bfde26e61cca14c0353cd6f7449487d53fcad8c057ea8436af29
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.9.19

Release files / rest_api_response-0.2-py3-none-any.whl

Download URL rest_api_response-0.2-py3-none-any.whl
Size 3.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7933c9bfc5468c443256193c04bc3bc5fd3194d70260ce48e6c630045c27dd53
BLAKE2b-256 checksum
How to use checksums
2def33414f942a36789c1c65093668bd26d84e35b1b17d2340ad8325b2d416fa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.9.19

Security for rest-api-response 0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page