Sanic-RESTful is an extension for Sanic that adds support for quickly building REST APIs. It is a lightweight abstraction that works with your existing ORM/libraries. Sanic-RESTful encourage best practices with minimal setup. if you are familiar with Sanic, Sanic-RESTful should be easy to pick up. It provides a coherent collection of decorators and tools to describe your API
There are still references to modules from Flask and Werkzeug in the repo. But It work fine now.
Sanic-RESTful requires Python 3.6+
pip install sanic-restful
With Sanic-RESTful, you only import the api instance to route or sanic class instance, and document your endpoints Example (class-view): ```python from sanic import Sanic from sanic_restful import Api, Resource, reqparse
app = Sanic(name) api = Api(app, errors=errors)
TODOS = { 'todo1': {'task': 'build an API'}, 'todo2': {'task': '?????'}, 'todo3': {'task': 'profit!'}, }
parser = reqparse.RequestParser() parser.add_argument('id', location=form)
class Todo(Resource): async def get(self, request, to_id): return TODOS[to_id]
async def delete(self, request, todo_id):
del TODOS[todo_id]
return '', 204
async def put(self, request, todo_id):
args = parser.parse_args(request)
task = {'task': args['task']}
TODOS[todo_id] = task
return task, 201
async def patch(self, request, todo_id):
raise Unauthorized('Fail')
api.add_resource(Todo, '/todos/
if name == 'main': app.run(debug=True) ```
Decorators example: ```python from functools import wraps
from sanic import Sanic, Blueprint from sanic_restful import Resource, Api
TODOS = { 'todo1': {'task': 'build an API'}, 'todo2': {'task': '?????'}, 'todo3': {'task': 'profit!'}, }
def decorator(func): @wraps(func) async def wrapper(request, args, kwargs): print(type(request)) request.user = 'User' return await func(request, args, **kwargs) return wrapper
class TodoSimple(Resource): """ You can try this example as follow: $ curl http://localhost:5000/todo1 -d "data=Remember the milk" -X PUT $ curl http://localhost:5000/todo1 {"todo1": "Remember the milk"} $ curl http://localhost:5000/todo2 -d "data=Change my breakpads" -X PUT $ curl http://localhost:5000/todo2 {"todo2": "Change my breakpads"}
Or from python if you have requests :
>>> from requests import put, get
>>> put('http://localhost:5000/todo1', data={'data': 'Remember the milk'}).json
{u'todo1': u'Remember the milk'}
>>> get('http://localhost:5000/todo1').json
{u'todo1': u'Remember the milk'}
>>> put('http://localhost:5000/todo2', data={'data': 'Change my breakpads'}).json
{u'todo2': u'Change my breakpads'}
>>> get('http://localhost:5000/todo2').json
{u'todo2': u'Change my breakpads'}
"""
method_decorators = {'get': decorator}
async def get(self, request, todo_id):
return {todo_id: TODOS[todo_id]}
async def put(self, request, todo_id):
TODOS[todo_id] = request.form.get('data')
return {todo_id: TODOS[todo_id]}
app = Sanic(name) bp = Blueprint(name, url_prefix='/test') api = Api(bp)
api.add_resource(TodoSimple, '/<todo_id:string') app.register_blueprint(bp)
if name == 'main': app.run(debug=True) ``` Note: Sanic-RESTful is different with Flask-RESTful, Must be "async" that decoratored function. Otherwise, The app will raise "TypeError: xxxx can't be used in 'await' expression"
This part of the documention will show you how to get started in using Sanic-RESTful with Sanic here
python setup.py test
Bumps certifi from 2019.11.28 to 2022.12.7.
9e9e840
2022.12.07b81bdb2
2022.09.24939a28f
2022.09.14aca828a
2022.06.15.2de0eae1
Only use importlib.resources's new files() / Traversable API on Python ≥3.11 ...b8eb5e9
2022.06.15.147fb7ab
Fix deprecation warning on Python 3.11 (#199)b0b48e0
fixes #198 -- update link in license9d514b4
2022.06.154151e88
Add py.typed to MANIFEST.in to package in sdist (#196)Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase
.
Bumps py from 1.8.1 to 1.10.0.
Sourced from py's changelog.
1.10.0 (2020-12-12)
- Fix a regular expression DoS vulnerability in the py.path.svnwc SVN blame functionality (CVE-2020-29651)
- Update vendored apipkg: 1.4 => 1.5
- Update vendored iniconfig: 1.0.0 => 1.1.1
1.9.0 (2020-06-24)
Add type annotation stubs for the following modules:
py.error
py.iniconfig
py.path
(not including SVN paths)py.io
py.xml
There are no plans to type other modules at this time.
The type annotations are provided in external .pyi files, not inline in the code, and may therefore contain small errors or omissions. If you use
py
in conjunction with a type checker, and encounter any type errors you believe should be accepted, please report it in an issue.1.8.2 (2020-06-15)
- On Windows,
py.path.local
s which differ only in case now have the same Python hash value. Previously, such paths were considered equal but had different hashes, which is not allowed and breaks the assumptions made by dicts, sets and other users of hashes.
e5ff378
Update CHANGELOG for 1.10.094cf44f
Update vendored libs5e8ded5
testing: comment out an assert which fails on Python 3.9 for nowafdffcc
Rename HOWTORELEASE.rst to RELEASING.rst2de53a6
Merge pull request #266 from nicoddemus/gh-actionsfa1b32e
Merge pull request #264 from hugovk/patch-2887d6b8
Skip test_samefile_symlink on pypy3 on Windowse94e670
Fix test_comments() in test_sourcefef9a32
Adapt test4a694b0
Add GitHub Actions badge to READMEDependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase
.
Bumps sanic from 19.12.2 to 20.12.7.
Sourced from sanic's releases.
Version 20.12.7
Resolves #2477 and #2478 See also #2495 and https://github.com/sanic-org/sanic/security/advisories/GHSA-8cw9-5hmv-77w6
Full Changelog: https://github.com/sanic-org/sanic/compare/v20.12.6...v20.12.7
Version 20.12.6
What's Changed
- Potential server crash if running Python 3.10 w/ Sanic 20.12 by
@ahopkins
in sanic-org/sanic#2400Full Changelog: https://github.com/sanic-org/sanic/compare/v20.12.5...v20.12.6
Version 20.12.5
#2366 Upgrade
websockets
version - SECURITY UPDATEVersion 20.12.4
- #2129 Unpin uvloop
Version 20.12.3
Bugfixes
- #2021 Remove prefix from websocket handler name
Version 20.12.2
Dependencies
- #2026 Fix uvloop to 0.14 because 0.15 drops Python 3.6 support
- #2029 Remove old chardet requirement, add in hard multidict requirement
Version 20.12.1
Features
- #1993 Add disable app registry
Version 20.12.0
Features
- [#1945][] Static route more verbose if file not found
- [#1954][] Fix static routes registration on a blueprint
- [#1961][] Add Python 3.9 support
- [#1962][] Sanic CLI upgrade
- [#1967][] Update aiofile version requirements
- [#1969][] Update multidict version requirements
- [#1970][] Add py.typed file
- [#1972][] Speed optimization in request handler
- [#1979][] Add app registry and Sanic class level app retrieval
Bugfixes
... (truncated)
Sourced from sanic's changelog.
.. note::
CHANGELOG files are maintained in
./docs/sanic/releases
. To view the full CHANGELOG, please visit https://sanic.readthedocs.io/en/stable/sanic/changelog.html.Version 21.6.1
Bugfixes
[#2178](https://github.com/sanic-org/sanic/issues/2178) <https://github.com/sanic-org/sanic/pull/2178>
_ Update sanic-routing to allow for better splitting of complex URI templates[#2183](https://github.com/sanic-org/sanic/issues/2183) <https://github.com/sanic-org/sanic/pull/2183>
_ Proper handling of chunked request bodies to resolve phantom 503 in logs[#2181](https://github.com/sanic-org/sanic/issues/2181) <https://github.com/sanic-org/sanic/pull/2181>
_ Resolve regression in exception logging[#2201](https://github.com/sanic-org/sanic/issues/2201) <https://github.com/sanic-org/sanic/pull/2201>
_ Cleanup request info in pipelined requestsVersion 21.6.0
Features
[#2094](https://github.com/sanic-org/sanic/issues/2094) <https://github.com/sanic-org/sanic/pull/2094>
_ Addresponse.eof()
method for closing a stream in a handler
[#2097](https://github.com/sanic-org/sanic/issues/2097) <https://github.com/sanic-org/sanic/pull/2097>
_ Allow case-insensitive HTTP Upgrade header
[#2104](https://github.com/sanic-org/sanic/issues/2104) <https://github.com/sanic-org/sanic/pull/2104>
_ Explicit usage of CIMultiDict getters
[#2109](https://github.com/sanic-org/sanic/issues/2109) <https://github.com/sanic-org/sanic/pull/2109>
_ Consistent use of error loggers
[#2114](https://github.com/sanic-org/sanic/issues/2114) <https://github.com/sanic-org/sanic/pull/2114>
_ Newclient_ip
access of connection info instance
[#2119](https://github.com/sanic-org/sanic/issues/2119) <https://github.com/sanic-org/sanic/pull/2119>
_ Alternatate classes on instantiation forConfig
andSanic.ctx
[#2133](https://github.com/sanic-org/sanic/issues/2133) <https://github.com/sanic-org/sanic/pull/2133>
_ Implement new version of AST router
- Proper differentiation between
alpha
andstring
param types- Adds a
slug
param type, example:<foo:slug>
- Deprecates
<foo:string>
in favor of<foo:str>
- Deprecates
<foo:number>
in favor of<foo:float>
- Adds a
route.uri
accessor
[#2136](https://github.com/sanic-org/sanic/issues/2136) <https://github.com/sanic-org/sanic/pull/2136>
_ CLI improvements with new optional params
[#2137](https://github.com/sanic-org/sanic/issues/2137) <https://github.com/sanic-org/sanic/pull/2137>
_ Addversion_prefix
to URL builders
[#2140](https://github.com/sanic-org/sanic/issues/2140) <https://github.com/sanic-org/sanic/pull/2140>
_ Event autoregistration withEVENT_AUTOREGISTER
... (truncated)
05002d7
Path protection with pathlibb4360d4
Path protection with pathlib3b85b3b
Potential server crash if running Python 3.10 w/ Sanic 20.12 (#2400)6e55e73
fix: websocket dependency for websockets 9.1 security fix (#2366)89d9424
Merge branch 'pr2129' into 20.12LTS4d6205e
Bump version1684b0b
remove reference to yanked packages4f5faa4
unpin uvloopcbb77b5
fix issue where request.args.pop removed parameters inconsistently (#2112)35c7625
Bump version 20.12.3 (#2062)Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase
.
Bumps aiohttp from 3.6.2 to 3.8.1.
Sourced from aiohttp's releases.
v3.8.1
Bugfixes
- Fix the error in handling the return value of
getaddrinfo
.getaddrinfo
will return an(int, bytes)
tuple, if CPython could not handle the address family. It will cause a index out of range error in aiohttp. For example, if user compile CPython with--disable-ipv6
option but his system enable the ipv6. (#5901)- Do not install "examples" as a top-level package. (#6189)
- Restored ability to connect IPv6-only host. (#6195)
- Remove
Signal
from__all__
, replaceaiohttp.Signal
withaiosignal.Signal
in docs (#6201)- Made chunked encoding HTTP header check stricter. (#6305)
Improved Documentation
- update quick starter demo codes. (#6240)
- Added an explanation of how tiny timeouts affect performance to the client reference document. (#6274)
- Add flake8-docstrings to flake8 configuration, enable subset of checks. (#6276)
- Added information on running complex applications with additional tasks/processes -- :user:
Dreamsorcerer
. (#6278)Misc
- (#6205)
v3.8.0
Features
... (truncated)
Sourced from aiohttp's changelog.
3.8.1 (2021-11-14)
Bugfixes
- Fix the error in handling the return value of
getaddrinfo
.getaddrinfo
will return an(int, bytes)
tuple, if CPython could not handle the address family. It will cause an index out of range error in aiohttp. For example, if user compile CPython with--disable-ipv6
option, but his system enable the ipv6.[#5901](https://github.com/aio-libs/aiohttp/issues/5901) <https://github.com/aio-libs/aiohttp/issues/5901>
_- Do not install "examples" as a top-level package.
[#6189](https://github.com/aio-libs/aiohttp/issues/6189) <https://github.com/aio-libs/aiohttp/issues/6189>
_- Restored ability to connect IPv6-only host.
[#6195](https://github.com/aio-libs/aiohttp/issues/6195) <https://github.com/aio-libs/aiohttp/issues/6195>
_- Remove
Signal
from__all__
, replaceaiohttp.Signal
withaiosignal.Signal
in docs[#6201](https://github.com/aio-libs/aiohttp/issues/6201) <https://github.com/aio-libs/aiohttp/issues/6201>
_- Made chunked encoding HTTP header check stricter.
[#6305](https://github.com/aio-libs/aiohttp/issues/6305) <https://github.com/aio-libs/aiohttp/issues/6305>
_Improved Documentation
- update quick starter demo codes.
[#6240](https://github.com/aio-libs/aiohttp/issues/6240) <https://github.com/aio-libs/aiohttp/issues/6240>
_- Added an explanation of how tiny timeouts affect performance to the client reference document.
[#6274](https://github.com/aio-libs/aiohttp/issues/6274) <https://github.com/aio-libs/aiohttp/issues/6274>
_- Add flake8-docstrings to flake8 configuration, enable subset of checks.
[#6276](https://github.com/aio-libs/aiohttp/issues/6276) <https://github.com/aio-libs/aiohttp/issues/6276>
_- Added information on running complex applications with additional tasks/processes -- :user:
Dreamsorcerer
.[#6278](https://github.com/aio-libs/aiohttp/issues/6278) <https://github.com/aio-libs/aiohttp/issues/6278>
_Misc
[#6205](https://github.com/aio-libs/aiohttp/issues/6205) <https://github.com/aio-libs/aiohttp/issues/6205>
_
3.8.0 (2021-10-31)
Features
- Added a
GunicornWebWorker
feature for extending the aiohttp server configuration by allowing the 'wsgi' coroutine to returnweb.AppRunner
object.
... (truncated)
cc6dc0c
Release 3.8.1dd5b4b3
Fix #6236: Remove chardet from extra dependencies8adddad
Restored ability to connect IPv6-only host. (#6309)8457ffd
Made chunked encoding HTTP header check stricter (#6305) (#6306)5795bbc
Sort spelling whitelist alphabetically065c15b
Clarify tiny timeouts behavior and how it can affect performance (#6274) (#6303)c4502b0
Improve documentation for running complex applications (#6278) (#6299)dcffb53
Add flake8-docstrings, fix bunch of flagged issues (#6276)04d6008
Recompile dependencies246d541
Bump coverage from 6.1.1 to 6.1.2 (#6297)Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase
.
Bumps ipython from 7.12.0 to 7.16.3.
d43c7c7
release 7.16.35fa1e40
Merge pull request from GHSA-pq7m-3gw7-gq5x8df8971
back to dev9f477b7
release 7.16.2138f266
bring back release helper from master branch5aa3634
Merge pull request #13341 from meeseeksmachine/auto-backport-of-pr-13335-on-7...bcae8e0
Backport PR #13335: What's new 7.16.28fcdcd3
Pin Jedi to <0.17.2.2486838
release 7.16.120bdc6f
fix conda buildDependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase
.