Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kalasjocke/hyp
Partial JSON API implementation in Python
https://github.com/kalasjocke/hyp
Last synced: about 1 month ago
JSON representation
Partial JSON API implementation in Python
- Host: GitHub
- URL: https://github.com/kalasjocke/hyp
- Owner: kalasjocke
- License: mit
- Created: 2014-02-10T20:39:16.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2014-12-08T12:54:51.000Z (about 10 years ago)
- Last Synced: 2024-12-09T23:06:04.659Z (about 2 months ago)
- Language: Python
- Homepage:
- Size: 372 KB
- Stars: 127
- Watchers: 5
- Forks: 15
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- starred-awesome - hyp - Partial JSON API implementation in Python (Python)
README
Hyp [![Build Status](https://travis-ci.org/kalasjocke/hyp.svg)](https://travis-ci.org/kalasjocke/hyp)
===
JSON-API responses in Python.About
-----
Hyp is a library implementing the _must_ parts of the [JSON-API](http://jsonapi.org) response specification. This means that you can use Hyp to serialize your models into responses that contain links and linked compound documents. It works really good in combination with your micro web framework of choice, preferably [Flask](http://flask.pocoo.org).It has built in support for both [Schematics](https://schematics.readthedocs.org/) and [Marshmallow](http://marshmallow.readthedocs.org) in the sense that you can use any of them for serializing your models (or primitives) into JSON that Hyp creates responses from. To add support for more data serialization libraries such as [Colander](http://docs.pylonsproject.org/projects/colander/en/latest/) should be trivial.
Depending on which serialization library that you would like to use make sure to add it to your app's requirements.
Tutorial
--------
First let's define some serializers for your models:```python
from marshmallow import Schema, fieldsclass CommentSchema(Schema):
id = fields.Integer()
content = fields.String()class PersonSchema(Schema):
id = fields.Integer()
name = fields.String()class PostSchema(Schema):
id = fields.Integer()
title = fields.String()
```We can then create our own responders using the `hyp.Responders` class:
```python
from hyp.marshmallow import Responderclass CommentResponder(Responder):
TYPE = 'comments'
SERIALIZER = CommentSchemaclass PersonResponder(Responder):
TYPE = 'people'
SERIALIZER = PersonSchemaclass PostResponder(Responder):
TYPE = 'posts'
SERIALIZER = PostSchema
LINKS = {
'comments': {
'responder': CommentResponder,
'href': 'http://example.com/comments/{posts.comments}',
},
'author': {
'responder': PersonResponder,
'href': 'http://example.com/people/{posts.author}',
},
}
```Finally we can use our responders for creating responses. These responses goes perfectly into any Flask application out there:
```python
post = {
'id': 1,
'title': 'My post',
'comments': [
{'id': 1, 'content': 'A comment'},
{'id': 2, 'content': 'Another comment'},
]
}json = PostResponder.respond(post, linked={'comments': post['comments']})
```
The `json` variable will now contain some freshly squeezed JSON ready for sending back to the client:
```json
{
"posts": [
{
"id": 1,
"title": "My title",
"links": {
"comments": [1, 2]
}
}
],
"linked": {
"comments": [
{
"id": 1,
"content": "My comment"
},
{
"id": 2,
"content": "Another comment"
}
]
},
"links": {
"posts.comments": {
"type": "comments",
"href": "http://example.com/comments/{posts.comments}"
}
}
}
```If you'd like to get have dict returned instead of json, for example if you want to use flask's [jsonify](http://flask.pocoo.org/docs/api/#flask.json.jsonify), then you can use the `build` method instead:
```python
post = {
'id': 1,
'title': 'My post',
'comments': [
{'id': 1, 'content': 'A comment'},
{'id': 2, 'content': 'Another comment'},
]
}response = PostResponder.build(post, linked={'comments': post['comments']})
json = flask.jsonify(response)
```