Services¶
WebDSL includes a simple way to define string and JSON-based webservices.
JSON API¶
The JSON interface is defined as follows:
native class org.json.JSONObject as JSONObject {
constructor()
constructor(String)
get(String) : Object
getBoolean(String) : Bool
getDouble(String) : Double
getInt(String) : Int
getJSONArray(String) : JSONArray
getJSONObject(String) : JSONObject
getString(String) : String
has(String) : Bool
names() : JSONArray
put(String, Object)
toString() : String
toString(Int) : String
}
native class org.json.JSONArray as JSONArray {
constructor()
constructor(String)
get(Int) : Object
getBoolean(Int) : Bool
getDouble(Int) : Double
getInt(Int) : Int
getJSONArray(Int) : JSONArray
getJSONObject(Int) : JSONObject
getString(Int) : String
length() : Int
join(String) : String
put(Object)
remove(Int)
toString() : String
toString(Int) : String
}
Example use in WebDSL:
function myJsonFun() : String {
var obj := JSONObject("{}");
obj.put("name", "Pete");
obj.put("age", 27);
return obj.toString();
// Will return '{"name": "Pete", "age": 27}'
}
Defining services¶
A service is simply a WebDSL function that uses the service
keyword instead of function
, you don't have to specify a return type, it will convert anything you return to a string (using .toString()
):
entity Document {
title : String (id, name)
text : Text
}
service document(id : String) {
if(getHttpMethod() == "GET") {
var doc := findDocument(id);
var json := JSONObject();
json.put("title", doc.title);
json.put("text", doc.text);
return json;
}
if(getHttpMethod() == "PUT") {
var doc := getUniqueDocument(id);
var json := JSONObject(readRequestBody());
doc.text := json.getString("text");
return doc.title;
}
}
services are mapped to /serviceName
, e.g. /document
. Here's a few sample requests to test (note, these are services part of an application called "hellojson"):
$ curl -X PUT 'http://localhost:8080/hellojson/document/my-first-doc' \
-d '{"text": "This is my first document"}'
my-first-doc
$ curl http://localhost:8080/hellojson/document/my-first-doc
{"text":""This is my first document","title":"my-first-doc"}
But, like pages, services can also have entities as arguments:
service documentJson(doc : Document) {
var obj := JSONObject();
obj.put("title", doc.title);
obj.put("text", doc.text);
return obj;
}