rest-api-response


Namerest-api-response JSON
Version 0.2 PyPI version JSON
download
home_pagehttps://github.com/aybruhm/api-response
SummaryA go-to production API response with an easy format for building APIs with Python.
upload_time2024-05-11 18:09:15
maintainerNone
docs_urlNone
authorAbraham 'Abram' Israel
requires_python>=3
licenseMIT
keywords api response custom api response
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Production API Response

[![Package Code Quality](https://github.com/aybruhm/api-response/actions/workflows/package-test.yml/badge.svg)](https://github.com/aybruhm/api-response/actions/workflows/package-test.yml) [![Package Published to PypI](https://github.com/aybruhm/api-response/actions/workflows/package-publish.yml/badge.svg)](https://github.com/aybruhm/api-response/actions/workflows/package-publish.yml)

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:

```bash
pip install rest-api-response
```

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

```python
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:

```python
# 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:

```json
[
    {
        "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:

```python
# 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:

```json
[   
    "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.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/aybruhm/api-response",
    "name": "rest-api-response",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3",
    "maintainer_email": null,
    "keywords": "api, response, custom api response",
    "author": "Abraham 'Abram' Israel",
    "author_email": "israelvictory87@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/0f/12/186e77d9bfde26e61cca14c0353cd6f7449487d53fcad8c057ea8436af29/rest_api_response-0.2.tar.gz",
    "platform": null,
    "description": "# Production API Response\n\n[![Package Code Quality](https://github.com/aybruhm/api-response/actions/workflows/package-test.yml/badge.svg)](https://github.com/aybruhm/api-response/actions/workflows/package-test.yml) [![Package Published to PypI](https://github.com/aybruhm/api-response/actions/workflows/package-publish.yml/badge.svg)](https://github.com/aybruhm/api-response/actions/workflows/package-publish.yml)\n\nA go-to production API response with an easy format for building APIs with Python.\n\n## Quickstart\n\nTo get it running, follow the steps below:\n\n1). Pip install the package in your project terminal:\n\n```bash\npip install rest-api-response\n```\n\n2). In the file (.py) that you wish to use it, import it:\n\n```python\nfrom rest_api_response import success_response, error_response\n```\n\nThat's pretty much it - you can now call the function and pass the required arguments!\n\n## Example\n\nSuppose you have an API class that returns a list of blog posts to a client:\n\n```python\n# imports goes here\n...\nclass PostListAPIView(views.APIView):\n    serializer_class = PostSerializer\n\n    def get(self, request):\n        \"\"\"Returns a list of posts\"\"\"\n\n        posts = Post.objects.all()\n        serializer = self.serializer_class(posts, many=True)\n        return Response(serializer.data)\n```\n\nThe API response would be:\n\n```json\n[\n    {\n        \"title\": \"First blog post\", \n        \"content\": \"Lorem ipsume content\", \n        \"author\": 1\n    },\n    {\n        \"title\": \"Second blog post\", \n        \"content\": \"Lorem ipsume content\", \n        \"author\": 2\n    },\n    {\n        \"title\": \"Third blog post\", \n        \"content\": \"Lorem ipsume content\", \n        \"author\": 3\n    }\n]\n```\n\nThis works too, but let's take the response to the next level by doing this:\n\n```python\n# imports goes here\n...\nfrom rest_api_response import success_response\n\n\nclass PostListAPIView(views.APIView):\n    serializer_class = PostSerializer\n\n    def get(self, request):\n        \"\"\"Returns a list of posts\"\"\"\n\n        posts = Post.objects.all()\n        serializer = self.serializer_class(posts, many=True)\n        _response = success_response(\n            message=\"Post retrieved!\",\n            data=serializer.data\n        )\n        return Response(data=_response, status=status.HTTP_200_OK)\n```\n\nThe API response would be:\n\n```json\n[   \n    \"status\": true, \n    \"message\": \"Posts retrieved!\", \n    \"data\": [\n        {\n            \"title\": \"First blog post\", \n            \"content\": \"Lorem ipsume content\", \n            \"author\": 1\n        },\n        {\n            \"title\": \"Second blog post\", \n            \"content\": \"Lorem ipsume content\", \n            \"author\": 2\n        },\n        {\n            \"title\": \"Third blog post\", \n            \"content\": \"Lorem ipsume content\", \n            \"author\": 3\n        }\n    ]\n]\n```\n\nAnd that's it. You have a nicely catchy response. :-)\n\n## Contribute\n\nAll contributions are welcome:\n\n- Read the issues, Fork the project and do a Pull Request.\n- Request a new topic creating a `New issue` with the `enhancement` tag.\n- 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.\n- Suggest a better or more pythonic way for existing examples.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A go-to production API response with an easy format for building APIs with Python.",
    "version": "0.2",
    "project_urls": {
        "Bug Tracker": "https://github.com/aybruhm/api-response/issues",
        "Homepage": "https://github.com/aybruhm/api-response"
    },
    "split_keywords": [
        "api",
        " response",
        " custom api response"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2def33414f942a36789c1c65093668bd26d84e35b1b17d2340ad8325b2d416fa",
                "md5": "66ab540a9099f0478d9f70facad3100f",
                "sha256": "7933c9bfc5468c443256193c04bc3bc5fd3194d70260ce48e6c630045c27dd53"
            },
            "downloads": -1,
            "filename": "rest_api_response-0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "66ab540a9099f0478d9f70facad3100f",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3",
            "size": 3940,
            "upload_time": "2024-05-11T18:09:14",
            "upload_time_iso_8601": "2024-05-11T18:09:14.157113Z",
            "url": "https://files.pythonhosted.org/packages/2d/ef/33414f942a36789c1c65093668bd26d84e35b1b17d2340ad8325b2d416fa/rest_api_response-0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0f12186e77d9bfde26e61cca14c0353cd6f7449487d53fcad8c057ea8436af29",
                "md5": "5629d6f2dbd6ba9eded8eaef78f49aad",
                "sha256": "0af8bf10e920448daa09951fcc3e5d3d6d5289712a2cc9cad2562416680b8b53"
            },
            "downloads": -1,
            "filename": "rest_api_response-0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "5629d6f2dbd6ba9eded8eaef78f49aad",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3",
            "size": 3673,
            "upload_time": "2024-05-11T18:09:15",
            "upload_time_iso_8601": "2024-05-11T18:09:15.173610Z",
            "url": "https://files.pythonhosted.org/packages/0f/12/186e77d9bfde26e61cca14c0353cd6f7449487d53fcad8c057ea8436af29/rest_api_response-0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-11 18:09:15",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "aybruhm",
    "github_project": "api-response",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "rest-api-response"
}
        
Elapsed time: 0.22246s