Skip to content

Instantly share code, notes, and snippets.

@pfrazee
Last active August 29, 2015 13:58
Show Gist options
  • Save pfrazee/10021337 to your computer and use it in GitHub Desktop.
Save pfrazee/10021337 to your computer and use it in GitHub Desktop.
How can the underlying protocols of services be abstracted into the language itself? Looking at it here with a transpiled JS language, but a library might work as well. Best past attempt is a JS library, https://github.com/pfraze/servware, which is convenient, but still exposes the HTTP. This removes protocol details (headers, status codes) enti…
// defining services
var usersCollection = [];
service Main {
link collection = Users;
meta rel = 'service';
meta id = 'main';
}
local.hostChannel.bind(Main);
service Users {
link up = Main;
link item = User;
meta rel = 'collection stdrel.com/crud-coll';
meta title = 'Users Collection';
meta id = 'users';
method GET/json(online)
{
if (online) {
return usersCollection.filter(isOnline);
}
return usersCollection;
}
method GET/html()
{
return '<h1>Hello, World</h1>';
}
method POST/json()
{
// :TODO: how is stream received?
usersCollection.push(newuser);
}
}
dynamic service User (id) {
required userItem = usersCollection[id];
link up = Users;
meta rel = 'item stdrel.com/crud-item';
meta title = userItem.name;
meta id = id;
method GET/json() {
return userItem;
}
method PATCH/json() {
receive updates;
for (var k in updates) {
userItem[k] = updates[k];
}
}
method DELETE() {
delete usersCollection[id];
}
}
// defining agents
agent Coll(id) from Service [{
rel = 'stdrel.com/crud-coll';
id = id;
}]
agent Item(id) from Coll [{
rel = 'stdrel.com/crud-item';
id = id;
}]
agent Item(coll, id) from Service [
{ rel = 'stdrel.com/crud-coll'; id = coll; },
{ rel = 'stdrel.com/crud-item'; id = id; }
]
agent User(id) from Service [
{ rel = 'stdrel.com/crud-coll'; id = 'users'; },
{ rel = 'stdrel.com/crud-item'; id = id; }
]
// using agents, streams
var myHost = Service('http://myhost.com');
var users = myHost.Coll('users');
var bob = myHost.User('bob');
var bobData = yield bob.GET/json();
var json = yield users.GET(); // will default to json because json is first in the service definition
var html = yield users.GET/html();
var json = yield users.GET({online: true});
var patch = stream(bob.PATCH/json);
patch().then(onSuccess, onFailure);
patch.end(bobData);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment