Library to work with Siren protocol
Project description
lila
Lila is a Python library to work with Siren protocol. It aims to encapsulate most of the requirements of the protocol, so that one can utilize EAFP principle instead of validating incoming and outgoing data.
Installation
$ python -m pip install lila
Documentation
Documentation for the package can be found on the Wiki.
Quickstart
Assume we expect to receive or need to send the following json data:
entity_data = {
"class": [ "order" ],
"properties": {
"orderNumber": 42,
"itemCount": 3,
"status": "pending"
},
"entities": [
{
"class": [ "items", "collection" ],
"rel": [ "http://x.io/rels/order-items" ],
"href": "http://api.x.io/orders/42/items"
},
{
"class": [ "info", "customer" ],
"rel": [ "http://x.io/rels/customer" ],
"properties": {
"customerId": "pj123",
"name": "Peter Joseph"
},
"links": [
{ "rel": [ "self" ], "href": "http://api.x.io/customers/pj123" }
]
}
],
"actions": [
{
"name": "add-item",
"title": "Add Item",
"method": "POST",
"href": "http://api.x.io/orders/42/items",
"type": "application/x-www-form-urlencoded",
"fields": [
{ "name": "orderNumber", "type": "hidden", "value": "42" },
{ "name": "productCode", "type": "text" },
{ "name": "quantity", "type": "number" }
]
}
],
"links": [
{ "rel": [ "self" ], "href": "http://api.x.io/orders/42" },
{ "rel": [ "previous" ], "href": "http://api.x.io/orders/41" },
{ "rel": [ "next" ], "href": "http://api.x.io/orders/43" }
]
}
One can parse these data into a python object and access parts of it (client-side):
from lila.serialization.json.parser import JSONParser
entity = JSONParser().parse_entity(entity_data)
assert entity.actions[0].fields[0].value == "42"
Or start with a python object and build this json (server-side):
from lila.core.field import InputType, Field
from lila.core.action import Method, Action
from lila.core.link import Link, EmbeddedLink
from lila.core.entity import Entity, EmbeddedRepresentation
from lila.serialization.json.marshaler import JSONMarshaler
entity = Entity(
classes=["order"],
properties={
"orderNumber": 42,
"itemCount": 3,
"status": "pending",
},
entities=[
EmbeddedLink(
classes=["items", "collection"],
relations=["http://x.io/rels/order-items"],
target="http://api.x.io/orders/42/items",
),
EmbeddedRepresentation(
classes=["info", "customer"],
relations=["http://x.io/rels/customer"],
properties={
"customerId": "pj123",
"name": "Peter Joseph",
},
links=[
Link(
relations=["self"],
target="http://api.x.io/customers/pj123",
),
],
),
],
actions=[
Action(
name="add-item",
title="Add Item",
method=Method.POST,
target="http://api.x.io/orders/42/items",
media_type="application/x-www-form-urlencoded",
fields=[
Field(
name="orderNumber",
input_type=InputType.HIDDEN,
value="42"
),
Field(
name="productCode",
input_type=InputType.TEXT,
),
Field(
name="quantity",
input_type=InputType.NUMBER,
),
]
)
],
links=[
Link(
relations=["self"],
target="http://api.x.io/orders/42",
),
Link(
relations=["previous"],
target="http://api.x.io/orders/41",
),
Link(
relations=["next"],
target="http://api.x.io/orders/43",
),
]
)
entity_data = JSONMarshaler().marshal_entity(entity)
assert entity_data["actions"][0]["fields"][0]["value"] == "42"
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
lila-1.0.0.tar.gz
(14.6 kB
view details)
Built Distribution
lila-1.0.0-py3-none-any.whl
(20.6 kB
view details)
File details
Details for the file lila-1.0.0.tar.gz
.
File metadata
- Download URL: lila-1.0.0.tar.gz
- Upload date:
- Size: 14.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.5.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 85aaa2cf71aa7b3e3594f827120e3a6fb6bb016c055bcc5c232007279c66f1d1 |
|
MD5 | 3329f4443dd6b744bffffca10bd8be4c |
|
BLAKE2b-256 | 0f231cbcc8490bd4535fc928d4aa3e07d2e2c570ef043f48cb7847d8d46b6a1f |
File details
Details for the file lila-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: lila-1.0.0-py3-none-any.whl
- Upload date:
- Size: 20.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.5.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ae7f6a86c46cbead02b3aa4b4c21c4e431e3093a68cf5102925a9b5d1c86d846 |
|
MD5 | 2c37666fafcb1bf8462394dbd575d03a |
|
BLAKE2b-256 | d7ce218bc32366ce325c967f1044c64a5c794dba6fcfd44bb417618e5ae9b012 |