Development

Getting started with REST

Language Specific Examples

Javascript (with jQuery)

This changes title of a page “news/2003/best-of-show” in site “example.com” by performing a “read” operation first, changing title and then performing “edit” operation:

$.get("http://localhost:8080/api/v1/read/page/example.com/news/2003/best-of-show?u=hill&p=hill", function(data) {
 if (data.success) {
   data.asset.page.metadata.title = 'New title';
   $.post("http://localhost:8080/api/v1/edit?u=hill&p=hill", JSON.stringify({
     'asset': data.asset
   }), function(data) {
     if (data.success)
       console.log('Success');
     else
       console.log('Error occurred when issuing an edit: ' + data.message);
   }, 'json');
 } else {
   console.log('Error occurred when issuing a read: ' + data.message);
 }
}, 'json');

Javascript (with fetch)

This is the same example as above but it does not require importing jQuery or any other libraries. It can be run even from browser's Developer Tools console.

fetch("http://localhost:8080/api/v1/read/page/example.com/news/2003/best-of-show", {
"headers": {
"Authorization": "Basic aGlsbDpoaWxs"
}
})
.then(r => r.json())
.then(data => {
if (data.success) {
data.asset.page.metadata.title = 'New title';
fetch("http://localhost:8080/api/v1/edit", {
method: 'POST',
headers: {
"Authorization": "Basic aGlsbDpoaWxs"
},
body: JSON.stringify({'asset': data.asset})
})
.then(r => r.json())
.then(data => {
if (data.success)
console.log('Success');
else
console.log('Error occurred when issuing an edit: ' + data.message);
});
} else {
console.log('Error occurred when issuing a read: ' + data.message);
}
});

Javascript (with fetch and async/await)

Again, this is the same example as above that does not require importing any libraries but it uses newer ES syntax, making the code a bit cleaner. It can be run from browser's Developer Tools console of a modern browser.

(async () => {
const readResult = await fetch("http://localhost:8080/api/v1/read/page/example.com/news/2003/best-of-show", {
headers: {
"Authorization": "Bearer your-api-key"
}
});
const readData = await readResult.json();
if (readData.success) {
readData.asset.page.metadata.title = 'New title';
const editResult = await fetch("http://localhost:8080/api/v1/edit", {
method: 'POST',
headers: {
"Authorization": "Bearer your-api-key"
},
body: JSON.stringify({asset: readData.asset})
});
const editData = await editResult.json();
if (editData.success)
console.log('Success');
else
console.log('Error occurred when issuing an edit: ' + editData.message);
} else {
console.log('Error occurred when issuing a read: ' + readData.message);
}
})();

Reading and parsing a File's byte array contents

(async (
  url = "http://localhost:8080/api/v1/read/file/example.com/image/sample.png"",
apiKey = "your-api-key"
) => {
const readResult = await fetch(url, {
headers: {
"Authorization": `Bearer ${apiKey}`
}
});
const readData = await readResult.json();
if (readData.success) {
const encoder = new TextEncoder();
const buffer = encoder.encode(readData.asset.file.text).buffer
const sliced = Array.prototype.slice.call(new Uint8Array(buffer), 0);
console.dir(sliced)
} else {
console.log('Error occurred when issuing a read: ' + readData.message);
}
})();

PHP

GET operations are very simple. For instance, this reads a role with id “1”:

$reply = json_decode(file_get_contents('http://localhost:8080/api/v1/read/role/1?u=admin&p=admin'));
print_r($reply);

POST operations are also simple with this utility function to be able to easily convert data between PHP array and JSON:

function apiOperation($url, $params)
{
   return json_decode(file_get_contents($url, false, stream_context_create(array('http' => array('method'  => 'POST','content' => json_encode($params))))));
}

Here is a PHP example similar to the Javascript example above:

$reply = json_decode(file_get_contents('http://localhost:8080/api/v1/read/page/example.com/news/2003/best-of-show?u=hill&p=hill'));

if ($reply->success)
{
   $reply->asset->page->metadata->title="A new title";
   $reply = apiOperation('http://localhost:8080/api/v1/edit?u=hill&p=hill', array ('asset' => $reply->asset));
   if ($reply->success)
       echo "Success.";
   else
       echo "Error occurred when issuing an edit: " . $reply->message;
}
else
   echo "Error occurred when issuing a read: " . $reply->message;