Skip to content

Instantly share code, notes, and snippets.

@ttahmouch
Created August 20, 2014 04:10
Show Gist options
  • Save ttahmouch/e736de6bd5923843fc00 to your computer and use it in GitHub Desktop.
Save ttahmouch/e736de6bd5923843fc00 to your computer and use it in GitHub Desktop.
Compare RESTful API Client with RESTless API Client
<!DOCTYPE html>
<html>
<head>
<title>Coast Browser</title>
</head>
<body>
<script src="http://127.0.0.1:8080/ua"></script>
<script>
/**
* @param window represents a window containing a DOM document.
* @see https://developer.mozilla.org/en-US/docs/Web/API/Window
*/
(function (window) {
/**
* The load event fires at the end of the document loading process. At this point, all of the objects in the
* document are in the DOM, and all the images, scripts, links and sub-frames have finished loading.
* @see https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers.onload
*/
window.onload = function () {
function onGetTodos(error, res) {
console.log('The user agent ' + (this.apis.length > 0 ? 'has' : 'has no') + ' affordances.');
if (error) {
console.error(error);
} else {
console.log('The todos are: ' + JSON.stringify(JSON.parse(res.body), null, ' '));
}
}
function onUpsertTodo(error, res) {
console.log('The user agent ' + (this.apis.length > 0 ? 'has' : 'has no') + ' affordances.');
if (error) {
console.error(error);
} else {
if (this.possible('getTodos')) {
console.log('Get all todos using hypermedia.');
this.follow('getTodos', onGetTodos);
} else {
console.log('Get all todos without using hypermedia.');
this.request({
method: 'GET',
uri: 'http://127.0.0.1:8080/todos'
}, onGetTodos);
}
}
}
function onEnter(error, res) {
console.log('The user agent ' + (this.apis.length > 0 ? 'has' : 'has no') + ' affordances.');
if (error) {
console.error(error);
} else {
console.log('All todos: ' + JSON.stringify(JSON.parse(res.body), null, ' '));
if (this.possible('upsertTodo')) {
console.log('Create new todo using hypermedia.');
this.follow({
rel: 'upsertTodo',
uri: { id: 1 },
body: {
title: 'Need to do something cool.',
due: '2014-08-17',
description: 'Cool stuff, yo.'
}
}, onUpsertTodo);
} else {
console.log('Create new todo without using hypermedia.');
this.request({
method: 'PUT',
uri: 'http://127.0.0.1:8080/todos/1',
body: JSON.stringify({
title: 'Need to do something cool.',
due: '2014-08-17',
description: 'Cool stuff, yo.'
})
}, onUpsertTodo);
}
}
}
hypermedia.Api.enter('http://127.0.0.1:8080/todos', onEnter);
// hypermedia.Api.create().request({ method: 'GET', uri: 'http://127.0.0.1:8080/todos' }, onEnter);
};
})(window);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment