Gotta simplify your REST APIs creation!
=======================================
Sanic's built-in `HTTPMethodView` is a useful thing, but sometimes one wants more sugar.
Whay not?
## Usage example
```python
import logging
from sanic import Sanic
from sanic_restful import Api, ApiEndpoint
logger = logging.getLogger(__name__)
app = Sanic()
@app.middleware('response')
async def cors_headers(request, response):
"""
Add CORS headers to all responses, including errors.
"""
response.headers['Access-Control-Allow-Origin'] = request.headers.get('Origin', '*')
response.headers['Access-Control-Allow-Credentials'] = 'true'
response.headers['Access-Control-Allow-Methods'] = 'POST, PUT, PATCH, DELETE, OPTIONS, GET'
response.headers['Access-Control-Allow-Headers'] = "X-Requested-With,X-Prototype-Version," \
"Content-Type,Cache-Control,Pragma,Origin,Cookie"
response.headers['Access-Control-Max-Age'] = '3600'
class TodoResource(ApiEndpoint):
# method_decorators = [auth.auth_required]
endpoint = 'todos/<todo_id>'
async def get(self, request, todo_id):
return {'id': todo_id}
async def post(self, request, todo_id):
return {'id': todo_id}, 201
api = Api(app, [TodoResource], '/v0.9/')
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000, debug=True)
```
And let's try it:
```bash
$ http POST http://localhost:8000/v0.9/todos/1
HTTP/1.1 201 Created
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: X-Requested-With,X-Prototype-Version,Content-Type,Cache-Control,Pragma,Origin,Cookie
Access-Control-Allow-Methods: POST, PUT, PATCH, DELETE, OPTIONS, GET
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 3600
Connection: keep-alive
Content-Length: 10
Content-Type: application/json
Keep-Alive: 5
{
"id": "1"
}
```
-----
Raw data
{
"_id": null,
"home_page": "https://github.com/saabeilin/sanic-restful",
"name": "sanic-simple-restful",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "sanic,authentication",
"author": "Sergei Beilin",
"author_email": "saabeilin@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/cf/56/b73897cd2a18d7a8580d44a2522faa44b39ef29ba5af9ba034de56e11869/sanic_simple_restful-0.1.0.tar.gz",
"platform": null,
"description": "Gotta simplify your REST APIs creation!\n=======================================\n\nSanic's built-in `HTTPMethodView` is a useful thing, but sometimes one wants more sugar.\n\nWhay not? \n\n## Usage example\n\n\n```python\nimport logging\n\nfrom sanic import Sanic\n\nfrom sanic_restful import Api, ApiEndpoint\n\nlogger = logging.getLogger(__name__)\n\napp = Sanic()\n\n\n@app.middleware('response')\nasync def cors_headers(request, response):\n \"\"\"\n Add CORS headers to all responses, including errors.\n \"\"\"\n response.headers['Access-Control-Allow-Origin'] = request.headers.get('Origin', '*')\n response.headers['Access-Control-Allow-Credentials'] = 'true'\n response.headers['Access-Control-Allow-Methods'] = 'POST, PUT, PATCH, DELETE, OPTIONS, GET'\n response.headers['Access-Control-Allow-Headers'] = \"X-Requested-With,X-Prototype-Version,\" \\\n \"Content-Type,Cache-Control,Pragma,Origin,Cookie\"\n response.headers['Access-Control-Max-Age'] = '3600'\n\n\nclass TodoResource(ApiEndpoint):\n # method_decorators = [auth.auth_required]\n endpoint = 'todos/<todo_id>'\n\n async def get(self, request, todo_id):\n return {'id': todo_id}\n\n async def post(self, request, todo_id):\n return {'id': todo_id}, 201\n\n\napi = Api(app, [TodoResource], '/v0.9/')\n\nif __name__ == \"__main__\":\n app.run(host=\"0.0.0.0\", port=8000, debug=True)\n```\n\nAnd let's try it:\n\n```bash\n$ http POST http://localhost:8000/v0.9/todos/1\nHTTP/1.1 201 Created\nAccess-Control-Allow-Credentials: true\nAccess-Control-Allow-Headers: X-Requested-With,X-Prototype-Version,Content-Type,Cache-Control,Pragma,Origin,Cookie\nAccess-Control-Allow-Methods: POST, PUT, PATCH, DELETE, OPTIONS, GET\nAccess-Control-Allow-Origin: *\nAccess-Control-Max-Age: 3600\nConnection: keep-alive\nContent-Length: 10\nContent-Type: application/json\nKeep-Alive: 5\n\n{\n \"id\": \"1\"\n}\n```\n\n\n-----\n\n\n",
"bugtrack_url": null,
"license": "MIT license",
"summary": "A simple class-based view for Sanic for simple RESTful APIs",
"version": "0.1.0",
"split_keywords": [
"sanic",
"authentication"
],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "d7af7b4b10be3b20f574f1d1cacddd83",
"sha256": "51f756371a2dcdec6e94949c5de77dd178c38eaa4aad7608382242fcc352e491"
},
"downloads": -1,
"filename": "sanic_simple_restful-0.1.0-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "d7af7b4b10be3b20f574f1d1cacddd83",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 4705,
"upload_time": "2023-01-01T19:58:53",
"upload_time_iso_8601": "2023-01-01T19:58:53.278664Z",
"url": "https://files.pythonhosted.org/packages/20/1d/e906790fc1fa445186a87e1792398e355a13c9658a7a7dc7ff490e5a5cfd/sanic_simple_restful-0.1.0-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"md5": "7d4589d7ad313227d2121ef57a5e2d30",
"sha256": "6c04f21e5ce87684b31d1e14dbc767e3ed369d64c95e709f548cafb9ab09ae1e"
},
"downloads": -1,
"filename": "sanic_simple_restful-0.1.0.tar.gz",
"has_sig": false,
"md5_digest": "7d4589d7ad313227d2121ef57a5e2d30",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 4524,
"upload_time": "2023-01-01T19:58:55",
"upload_time_iso_8601": "2023-01-01T19:58:55.232658Z",
"url": "https://files.pythonhosted.org/packages/cf/56/b73897cd2a18d7a8580d44a2522faa44b39ef29ba5af9ba034de56e11869/sanic_simple_restful-0.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-01-01 19:58:55",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "saabeilin",
"github_project": "sanic-restful",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "sanic-simple-restful"
}