/dougrain-forms

A dougrain extension to generate HAL forms

Primary LanguagePythonOtherNOASSERTION

dougrain-forms

image

A generator for hypermedia forms, following an unofficial draft by Mike Kelly. The format is roughly based on this Gist by Mike Kelly.

Installation

pip install dougrain-forms

Usage

Example:

from dougrain_forms import FormsMixin

class FormsDocument(Document, FormsMixin):
    pass

doc = FormsDocument.empty()
doc.add_link('self', '/foo')

doc.set_form(
    'attack',
    '/attacks',
    headers={
        'Content-Type': 'application/json'
    },
    method='POST',
    schema=ATTACK_SCHEMA
)

print(doc.as_object())

Output:

{
    "_forms": {
        "attack": {
            "headers": {
                "Content-Type": "application/json"
            },
            "href": "/attacks",
            "method": "POST",
            "schema": {
                "required": [
                    "name",
                    "damage"
                ],
                "type": "object",
                "properties": {
                    "name": {
                        "type": "string"
                    },
                    "damage": {
                        "minimum": 0,
                        "type": "integer",
                        "description": "How much does it hurt?"
                    }
                },
                "title": "Damage Schema"
            }
        }
    },
    "_links": {
        "self": {
            "href": "/foo"
        }
    }
}

API

By mixing in FormMixin into your document, you get three new methods:

  • set_form(self, rel, target, **kwargs)
  • delete_form(self, rel)
  • form(self, href, **kwargs)

Until real docs have been written, take a look at the source.