Web services

General syntax example:

service ServiceName {
   root = "/api"
   resource getUser(username : String) : User {
     method = "GET"
     uri = "/user/" + username
     mapper = userMapper
   }
   resource setUser(username : String, data : JSON)
               : void {
     method = "PUT"
     uri = "/user/" + username
     data = JSON.stringify(data)
   }
}
function userMapper(data : JSON) : User {
  return User.fromSelectJSON(data);
}

Service attributes

root (optional)

Sets a root URI for the service, e.g. if all resources are to be found under /api (e.g. /api/user/...), this attribute can be used.

Example:

root = "/api"

Resource attributes

method (optional, defaults to "GET")

The HTTP method to use for the service call. Values can be "GET", "POST" or "PUT".

uri

The URI to send the request to (relative to the service's root, if set).

Example:

uri = "/getuser.php?username=" + escape(username)

data (optional)

The contents of the request body, represented as a string.

Example:

data = JSON.stringify(user.toJSON())

Example 2:

data = "name=" + escape(name) + "&password="
       + escape(password)

encoding (optional, defaults to "json")

Possible values:

  • "json": JSON
  • "text": plain text
  • "xml": XML document

mapper (optional)

A function that maps from JSON to the return type of the resource.

Example:

resource bla() : Stats {
  uri = "/stats"
  mapper = statsMapper
}
...
function statsMapper(statsJSON : JSON) : Stats {
  // do stuff with statsJSON
  return stats;
}
language/service.txt · Last modified: 2013/10/01 02:29 (external edit)