PDA

View Full Version : Is it possible to autocreate Models based on the JSON schema from a REST server?



justinzane
15 May 2012, 10:47 AM
My REST api, django-tastypie, provides a nice schema for each model:


{
"allowed_detail_http_methods": ["get", "post", "put", "delete", "patch"],
"allowed_list_http_methods": ["get", "post", "put", "delete", "patch"],
"default_format": "application/json",
"default_limit": 20,
"fields": {
"id": {
"blank": false,
"default": "",
"help_text":
"Unicode string data. Ex: \"Hello World\"",
"nullable": false,
"readonly": false,
"type": "string",
"unique": true
},
"resource_uri": {
"blank": false,
"default": "No default provided.",
"help_text": "Unicode string data. Ex: \"Hello World\"",
"nullable": false,
"readonly": true,
"type": "string",
"unique": false
},
"text": {
"blank": false,
"default": "",
"help_text":
"Unicode string data. Ex: \"Hello World\"",
"nullable": false,
"readonly": false,
"type": "string",
"unique": false
}
}
}

It strikes me that coding models in both the RESTful server and the RESTful client, in this case django-tastypie and ExtJS, is a violation of the DRY (don't repeat yourself) development pattern.

What I am hoping for is guidance on how to create dynamic Models/Stores based on the schema data pulled by a proxy.

Some background info:
http://webofdata.wordpress.com/2011/08/07/json-data-and-the-rest/
http://json-schema.org/

JSON Schema provides a contract for what JSON data is required for a given application and how to interact with it. JSON Schema is intended to define validation, documentation, hyperlink navigation, and interaction control of JSON data.

lukepatrick
28 Feb 2013, 4:45 PM
Well, it seems like it is kind-of possible through the Response MetaData and the 'metachange' listner. http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.reader.Json
though this isn't exactly what you are looking for. I would be interested in a better answer as I'm looking for the same thing you are.