rest-api-response


Namerest-api-response JSON
Version 0.1.1 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_time2023-06-28 12:07:08
maintainer
docs_urlNone
authorAbram
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

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": "",
    "docs_url": null,
    "requires_python": ">=3",
    "maintainer_email": "",
    "keywords": "api,response,custom api response",
    "author": "Abram",
    "author_email": "israelvictory87@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/4d/80/d72b9ef86d256f29c6a4bd3f02d0100e88dc7be4f423a5615dae2250de5e/rest_api_response-0.1.1.tar.gz",
    "platform": null,
    "description": "# Production API Response\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.1.1",
    "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": "accfea9467ba33b437c914138f1dcc013cf20c764d794681eeb8418602bb3696",
                "md5": "62618ffcf96c130f93538a1cb8712849",
                "sha256": "b9cab09f24b371d7495956a0ab73110aa2e0e3528ae200e1133cdc918b048180"
            },
            "downloads": -1,
            "filename": "rest_api_response-0.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "62618ffcf96c130f93538a1cb8712849",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3",
            "size": 4284,
            "upload_time": "2023-06-28T12:07:07",
            "upload_time_iso_8601": "2023-06-28T12:07:07.431091Z",
            "url": "https://files.pythonhosted.org/packages/ac/cf/ea9467ba33b437c914138f1dcc013cf20c764d794681eeb8418602bb3696/rest_api_response-0.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4d80d72b9ef86d256f29c6a4bd3f02d0100e88dc7be4f423a5615dae2250de5e",
                "md5": "82829bbc48588d9134ee3fb9a75ae255",
                "sha256": "ee76ec92f1667c07daf8e940806587cc988918c1f8c3a6e908f771508e19b7ac"
            },
            "downloads": -1,
            "filename": "rest_api_response-0.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "82829bbc48588d9134ee3fb9a75ae255",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3",
            "size": 3862,
            "upload_time": "2023-06-28T12:07:08",
            "upload_time_iso_8601": "2023-06-28T12:07:08.676336Z",
            "url": "https://files.pythonhosted.org/packages/4d/80/d72b9ef86d256f29c6a4bd3f02d0100e88dc7be4f423a5615dae2250de5e/rest_api_response-0.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-06-28 12:07:08",
    "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.11938s