Giter Club home page Giter Club logo

hyp's Introduction

Hyp Build Status

JSON-API responses in Python.

About

Hyp is a library implementing the must parts of the JSON-API 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.

It has built in support for both Schematics and Marshmallow 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 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:

from marshmallow import Schema, fields


class 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:

from hyp.marshmallow import Responder


class CommentResponder(Responder):
    TYPE = 'comments'
    SERIALIZER = CommentSchema


class PersonResponder(Responder):
    TYPE = 'people'
    SERIALIZER = PersonSchema


class 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:

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:

{
    "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, then you can use the build method instead:

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)

hyp's People

Contributors

adregan avatar hellograyson avatar kalasjocke avatar sloria avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.