JSON Resource¶
JSON resources are dict, and list, etc. types. They behave like oridnary python dicts and lists, but have extra functionality from several json standards.
Installation¶
pip install json_resource
Resources¶
JSON resources are subclasses of basic python classes, that can be validated and located using json schema.
>>> from json_resource import Object, Schema
>>> dict_resource = Object(
{'test': 'bla'},
schema=Schema({
'properties': {'test': {'type': 'string'},
'links': [{'href': '/resource/{test}/', 'rel': 'self'}]
})
)
>>> dict_resource.url
/resource/bla/
Validation¶
>>> dict_resource.validate()
>>> dict_resource['test'] = 1
>>> dict_resource.validate()
Exception: '/test' should be of type string
JSON-pointer¶
Resources suppert indexing by json-pointer
>>> from json_pointer import Pointer
>>> pointer = Pointer('/list/1')
>>> resource[pointer]
4
JSON-patch¶
Resources can be modified using json-patch
>>> from json_patch import Patch
>>> patch = [{'op': 'add': 'path': '/list/4', 'value': 6}]
>>> resource.patch(patch)
>>> resource['list']
[3,4,5,6]
Sub-resources¶
When possible indexing returns another json resource:
>>> dict_resource['test'].schema
{'type': 'string'}
>>> dict_resource['test'].validate()
Exception: '/' should be of type string
>>> dict_resource['test'].schema
{'type': 'string'}
>>> dict_resource['test'].validate()
Exception: '/' should be of type string
Schemas¶
Schemas can be loaded automatically:
class ExampleResource(Object):
schema = Schema({'id': 'test'})
This will load a schema called “test.json” from the schemas directory in your package. Other directories can be registered by calling Schema.register_schema_dir.
Storing Resource¶
Example¶
Stored Resources can be stored in a mongo database:
>>> from json_resource import Resource, Schema
>>> class ExampleResource(Resource):
schema = Schema({
'properties': {'id': {'type': 'string'},
'required': 'id',
'links': [{'rel': 'self', 'href': '/test/{id}'}]
})
@staticmethod
def db(cls)
pymongo.connection()
>>> example = Example({'id': 'test', 'tags': [1,2,3])
>>> example.exists
False
>>> example.save()
>>> example.exists
True
>>> Example({'id': 'test'}).load()
{'id': 'test', 'tags': [1,2,3]}
>>> Example.objects
QuerySet[Example]<{'id': 'test', 'tags': [1,2,3]}>
>>> Example.objects.filter({'id': 'tast'})
QuerySet[Example]<>
>>> Example({'id': 'tast'}).load()
Exception: Resource not found: /resource/tast/
Mongo¶
By default the resources are stored in a collection named after the id of the resource. This can be overriden be overwrintg the collection method.
The _id of the resource will be the url of the particular resource. If the resource does not have an url, you can override the _id property:
class ExampleResource(Resource):
@property
def _id(self):
return something_unique()
Indexes can be added to the resources by specifying a indexes property in the top-level schema of the resource:
from json_resource import Resource, Schema
class ExampleResource(Resource):
schema = Schema({
'properties': {'id': {'type': 'string'}, 'indexed': {'type': 'string'}},
'required': 'id',
'indexes': ['indexed'],
'links': [{'rel': 'self', 'href': '/test/{id}'}]
})