Skip to main content

Partial JSON API implementation in Python on top of Schematics

Project description

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 though.

Tutorial
--------
First let's define some serializers for your models:

```python
from marshmallow import Serializer, fields


class CommentSerializer(Serializer):
id = fields.Integer()
content = fields.String()


class PersonSerializer(Serializer):
id = fields.Integer()
name = fields.String()


class PostSerializer(Serializer):
id = fields.Integer()
title = fields.String()
```

We can then create our own responders using the `hyp.Responders` class:

```python
from hyp.responder import Responder


class CommentResponder(Responder):
TYPE = 'comment'
SERIALIZER = CommentSerializer


class PersonResponder(Responder):
TYPE = 'person'
SERIALIZER = PersonSerializer


class PostResponder(Responder):
TYPE = 'post'
SERIALIZER = PostSerializer
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 like so:

```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)
```

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hyp-1.0.0.tar.gz (3.6 kB view details)

Uploaded Source

File details

Details for the file hyp-1.0.0.tar.gz.

File metadata

  • Download URL: hyp-1.0.0.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for hyp-1.0.0.tar.gz
Algorithm Hash digest
SHA256 4bdf1f3af1bb20ca621cc5fa21b79c048f5aad39280c14c81d2402efebad54aa
MD5 a86281d94c76fbf08f65e3ebee24183e
BLAKE2b-256 e6a4c279db9d2c1ba8fa04e7dd612d3cdb16bfa6161976549856e819a71dd33d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page