SBM ModScript Reference → SBM ModScript Samples → Sample Five: RESTDataSource
This sample provides code for working with the RESTDataSource object.
/* ModScript Example: RESTDataSource.tscm ----------------------------------- Use a RESTDataSource object. These can be created in Composer and bound to a URL, or bound to an endpoint that can have a different URL per runtime environment. Requirement: Deploy a RESTDataSource named "JSONPlaceHolder1" pointing to the "https://jsonplaceholder.typicode.com/users" service. Logic: Read a row from TS_RESTDATASOURCE, , invoke it via Get, parse the JSON into an object, process the object */ var restSource = Ext.CreateAppRecord(Ext.TableId("TS_RESTDATASOURCE")); //This is a REST Data Source restSource.Read("JSONPlaceHolder1"); var result = ""; try { restSource.Get(result); } catch(e) {} var resultObj = from_json(result); //Ext.WriteStream(result &&& "</br>"); // writing the JSON out might help you understand what you are parsing /* Sometimes, you can get lost in your JSON object; do I have a map, a vector, or perhaps a string or integer? Printing out the data type of what "from_json" created might guide you in the code to write to process that object: Ext.WriteStream( resultObj.get_type_info().name() ); */ Ext.WriteStream( "<dl>" ); for(entry : resultObj) { Ext.WriteStream( "<dt>${entry["username"]}</dt>"); // ChaiScript supports in-string script processing. →Here, we evaluate "entry["username"]", which looks in the map for the →username value and returns it for(value : entry) { Ext.WriteStream("<dd>${value.first()}: "); if(value.second().is_type(Map_type)) { // we can check if we got a map object using this call. →Another version that does the same thing: →value.second().get_type_info().name() == "Map" Ext.WriteStream("<dl>"); for(inner : value.second()) { if(inner.second().is_type(Map_type)) { // OK, we could process this map, too, but you get the idea continue; } Ext.WriteStream("<dd>${inner.first()}: ${inner.second()}</dd>"); } Ext.WriteStream("</dl>"); } else { Ext.WriteStream(value.second()); } Ext.WriteStream("</dd>"); } } Ext.WriteStream( "</dl>" );
Copyright © 2007–2017 Serena Software, Inc. All rights reserved.