Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/weaming/sanic-json
A thin wrapper on sanic web framework to help writting JSON API
https://github.com/weaming/sanic-json
api json sanic
Last synced: about 2 months ago
JSON representation
A thin wrapper on sanic web framework to help writting JSON API
- Host: GitHub
- URL: https://github.com/weaming/sanic-json
- Owner: weaming
- Created: 2018-08-24T07:45:03.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-07-17T08:21:49.000Z (over 5 years ago)
- Last Synced: 2024-10-11T23:50:31.724Z (3 months ago)
- Topics: api, json, sanic
- Language: Python
- Homepage:
- Size: 10.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# sanic json
A thin wrapper on sanic web framework to help writting JSON API.
Recommond use [json-api](https://github.com/weaming/json-api) instead. It's a abstraction library of this library.
```
pip3 install sanic-json
```## Exmaple
file `api/random.py`
```python
def obj_to_dict(obj):
if not obj or isinstance(obj, (str, int, float)):
return obj
elif isinstance(obj, list):
return [obj_to_dict(x) for x in obj]
elif isinstance(obj, dict):
return {k: obj_to_dict(v) for k, v in obj.items()}
else:
return {k: obj_to_dict(v) for k, v in obj.__dict__.items()}# the wrapper will get the args and kwargs value from the `request`
async def random_pohoto(req, count=10):
res = api.photo.random(count=count)
return {"data": obj_to_dict(res)}
```file `app.py`
```python
from sanic import Sanic
from sanic_json import get_json_route
from api.random import random_pohotoapp = Sanic()
json_route = get_json_route(app)json_route("/api/random", random_pohoto)
if __name__ == "__main__":
import osdebug = bool(os.getenv("DEBUG"))
# hot reload in next release: https://github.com/channelcat/sanic/issues/168
app.run(host="0.0.0.0", port=8000, debug=debug)
```