RAWS tutorial

The 'RAWS tutorial' series is designed to allow our users to get up to speed quickly with Rambla's Web Services (RAWS) and their usage. Our API's are based on REST principles and open standards (eg. ATOM and Atom Publishing Protocol) and accommodate for rapid client application development in any programming language.

The following tutorials are already available:

In the previous tutorial we've shown the different uses of the "link" object. In this tutorial, we will use the PHP client that was introduced in tutorial 2 to show you how to handle this object. We'll also learn how to retrieve partial responses. The complete source code for this tutorial is also part of the client download package. You can try running it yourself, after having edited the named constants on top of the script. For more information on using the PHP client, see tutorial 2.

This tutorial assumes that a directory named "tutorial6" has already been created on the CDN and 15 files have been uploaded to this directory (named "test1.mp4", "test2.mp4" ...). The sample script uses a local file, which path needs to be set in the named constant 'LOCAL_FILE' in order to create these files.

Getting entry links

You can use the properties of an entry object (= in this case returned by RASS as a response to a PUT item request) to directly access each link element and its attributes. The following code loops through all 'link' elements that are part of the entry and displays the relevant attributes.

  foreach($item->entry->link as $link) {
echo "\nType of relation: " . $link->rel;
echo "\nLink URI: " . $link->href;
echo "\nExpected type: " . $link->type . "\n";
}

The $rass object also has helper methods which you can use to retrieve the 'href' value of a link with a given relation type. Since this entry is returned by the RASS item resource, it will contain an enclosure link that points to CDN location at which the file (attached to this resource) can be downloaded by end-users.

  echo "\nGetting enclosure link via helper method: " . $rass->getEnclosureLink($item) . "\n";
>> http://monty.cdn03.rambla.be/tutorial6/test15.mp4

Getting feed links

In the same way as for an entry, you can use a feed object's properties (or helper methods) to directly access each link element and its attributes. The following code loops through all 'link' objects that are part of the feed and displays the relevant attributes.

  foreach($dir_list->feed->link as $link) {
echo "\nType of relation: " . $link->rel;
echo "\nLink URI: " . $link->href;
echo "\nExpected type: " . $link->type . "\n";
}

Using pagination

In the previous tutorial, we already learned how the 'link' element is being used by some RAWS resources to return partial collections. These resources will return a feed that may contain a 'link' object with its 'rel' attribute set to "next", to indicate that not all entries could be fitted inside of the response. The next page can then be retrieved by sending a GET request to the URI inside of the link element's 'href' attribute.

Most resources that support pagination also return a 'last' link that points to a URL that can be used to retrieve the last batch of entries from a given collection. You can use the $rass object's helper methods getNextLink() and getLastLink() to retrieve these URLs.

For this tutorial we have created a directory that contains 15 files. By passing the 'paginate_by' as part of the query-string arguments (for details, see the 'pagination' wiki page) and setting it to "10", we instruct RASS to return only 10 entries in each (partial) response. Therefore, the response should also contain a 'next' link that points to an URI that can be used to retrieve the 5 remaining entries.

  $dir_list = $rass->getDirList($dir->entry->content->params->path, "kind=file;paginate_by=10");

echo "\nNext link URI: " . $rass->getNextLink($dir_list) . "\n";
>> Next link URI: http://rass.cdn03.rambla.be/dir/tutorial6/?kind=file;page=2;paginate_by=10;
echo "Last link URI: " . $rass->getLastLink($dir_list) . "\n";
>> Last link URI: http://rass.cdn03.rambla.be/dir/tutorial6/?kind=file;page=2;paginate_by=10;

Note that in this case the 'next' and 'last' link both point to the same URL. This is normal, since RASS only needs to return 5 item entries in its next response. If instead our 'tutorial6' directory would contain more than 20 files, the 'next' and 'last' link would point to different URLs.

The connection object also has two helper methods - getNextList() and getLastList() - that automatically return the $feed object for the 'next' or 'last' link (by sending a GET request to the URL inside their 'href' attribute). When calling these methods, you need to pass the original $feed object as their argument.

   $next_list = $rass->getNextList($dir_list);

Pagination best practices

If you're using a RAWS resource that supports pagination and you want to retrieve all entries from a given collection, you should check for the presence of a 'next' link inside the feed object. You can automate this behaviour by nesting a call to the connection object's getNextList() inside a while-loop that checks on the feed object returned by this call. If there's no 'next' link present (= no more entries), the variable will be set to null and your code will exit the loop.

  $dir_list = $rass->getDirList($dir->entry->content->params->path, 'kind=file;paginate_by=10');
while($dir_list)
{
foreach ($dir_list->feed->entry as $entry) {
echo "\nFound entry with path = " . $entry->content->params->path;
}
$dir_list = $rass->getNextList($dir_list);
}

In this tutorial we will zoom in on the link object (originally defined in the Atom Protocol) and the way it's being used in the RAWS APIs. More specifically, we will look at the different kinds of link objects that may be present inside of a feed or entry object. In our next tutorial, you will learn how to handle these links using the RAWS PHP client libraries.

The link OBJECT

According to the atom protocol, the 'link' element defines a reference from an entry or feed to a Web resource. The reference itself is contained inside the "href" attribute. The "rel" attribute is used to give meaning to the reference: it specifies the type of relation that exists between the entry or feed that contains the link and the web resource that is referenced by the link. The "type" attribute contains a hint about the type of representation that you can expect when following the link. The protocol specifies a few more attributes, but they are currently not being used by RAWS.

Some of the relation types currently used by RAWS are 'enclosure', 'first', 'next' and 'last'.

Enclosure links

The value enclosure signifies that the IRI in the value of the "href" attribute identifies a related resource that is potentially large in size and might require special handling. In the RAWS APIs, enclosure is used in entries that contain metadata about a file. The type attribute can be used as an indication of the file's mimetype. Depending on the specific service and/or resource, this file may be located on the CDN or may be accessible through a web service.

For example, an enclosure link that is returned by the RASS 'item', 'dir' or 'meta' resource refers to the public location of a file on the CDN.

{
 "href": "http://monty.cdn01.rambla.be/test1.mp4",
"type": "video/mp4",
"rel": "enclosure"
}

First, Next and Last links

These types of links are used for pagination. For collections that have too many entries to be returned in a single feed, RAWS uses a mechanism that allows the server to return the entries in consecutive, partial feeds. To have control over the pagination, a client application can check for "link" objects in the feed with a "first", "next" or "last" relation type.

If a feed contains a link object with a "next" relation type, this means that the response contains a partial feed and more entries can be retrieved by sending an HTTP GET request to the URL in the "href" attribute. The client may keep doing this as long as the returned feed contains a "next" link. When the last partial feed is being returned by the server (= all entries have been returned), there will be no more "next" link in the feed.

{
"href": "http://rass.cdn01.rambla.be/dir/?kind=root,file,dir;page=2;paginate_by=100;",
"type": "application/json",
"rel": "next"
}

If a feed contains a link object with a "last" relation type, the client can jump to the last (partial) feed by sending an HTTP GET request to the URL in the "href" attribute.

{
"href": "http://rass.cdn01.rambla.be/dir/?kind=root,file,dir;page=4;paginate_by=100;",
"type": "application/json",
"rel": "last"
}

If a feed contains a link object with a "first" relation type, the client can jump to the first (partial) feed by sending an HTTP GET request to the URL in the "href" attribute.

Currently, pagination support within RAWS is limited to resources that return large feeds. These include the RASS dir and meta resources and most of the RAMS resources. All other RAWS resources will return their collections in one single feed. For more details about RAWS and pagination, see the dedicated wiki page.

In the previous tutorial, we learned how to retrieve ATOM collections from RAWS. More specifically, we retrieved information about our files and directories on the CDN using the RASS API's dir resource. This time, we will repeat these requests using the PHP client from tutorial 2.

The complete source code for this tutorial is also part of the client download package. You can try running it yourself, after having edited the named constants on top of the script. For more information about using the PHP client, see tutorial 2.

Pre-configuration

This tutorial assumes that some files and directories have already been created on the CDN for user 'monty'. The sample script uses a local file (-> path needs to be set in the named constant 'LOCAL_FILE') to create the files.

  • Files located under the root-directory: test1.mp4, test2.mp4.
  • Files located under a 'bucks' sub-directory: bunny1.mp4, bunny2.mp4.

Get files list

First we'll retrieve a list of all files inside our root-directory, by sending a GET request to "http://rass.cdn01.rambla.be/dir/?kind=file;alt=json". To do this, we create a Rass connection object and call getDirList() on it, passing "/" as the first argument. As the second argument, we pass a query-string with the 'kind' argument set to "file".

Query-string arguments are used by RAWS to further specify a request. Depending on the resource, they can be used as a search query, a resource filter, a response format indicator...

If the request succeeds, our method will return a RASS dir feed object. Otherwise, it will raise an exception.

require_once 'raws_json/rass_service.php';
$dir_feed = $rass->getDirList("/", "kind=file");

The returned $feed object contains a list of entry objects which provide information about our files.

foreach($dir_feed->feed->entry as $entry) {
echo "\nFound " . $entry->content->params->kind . " entry with path = " . $entry->content->params->path . "\n";
echo "Filename: " . $entry->content->params->filename . "\n";
echo "Filesize: " . $entry->content->params->size . "\n";
echo "Download URL: " . $rass->getEnclosureLink($entry) . "\n";
}

>> Found file entry with path = /test1.mp4
>> Filename: test1.mp4
>> Filesize: 13827
>> Download URL: http://monty.cdn01.rambla.be/test1.mp4
>>
>> Found file entry with path = /test2.mp4
>> ...

Get files list for sub-directory

To get the list of files stored under our 'bucks' sub-directory, we only need to change the first getDirFeed() parameter from "/" to "/bucks". The client will now send a GET request to 'http://rass.cdn01.rambla.be/dir/bucks/?kind=file;alt=json'.

$dir_feed = $rass->getDirList("/bucks", "kind=file");
foreach ($dir_feed->feed->entry as $entry) {
echo "\nFound " . $entry->content->params->kind . " entry with path = " .$entry->content->params->path . "\n";
}
>> Found file entry with path = /bucks/bunny1.mp4
>> Found file entry with path = /bucks/bunny2.mp4

Get list of sub-directories

In this case, we simply set the 'kind' argument to "dir". This will result in a GET request to "http://rass.cdn01.rambla.be/dir/?kind=dir;alt=json".

$dir_feed = $rass->getDirList("/", "kind=dir");
foreach ($dir_feed->feed->entry as $entry) {
echo "\nFound " . $entry->content->params->kind . " entry with path = " . $entry->content->params->path . "\n";
}
>> Found dir entry with path = /bucks

In this third episode of our RAWS tutorial series, we will show you how to retrieve a collection of resources. We'll also explain how to tweak RAWS requests using query-string arguments. To demonstrate some of the REST mechanics involved, we will be using the cURL command line tool. In our next tutorial, we will show you how to make the same requests using the PHP client libraries.

Most RAWS resources are able to return a collection of resources in response to a GET request, in the form of an JSON feed object. The RASS API's dir resource returns a feed if the URL path part (after the resource indicator 'dir') points to a directory on the CDN. This feed contains information about files, sub-directories and/or the directory itself, depending on the arguments passed in the query-string part of the GET request.

To demonstrate this, some files and directories have been created on the CDN for user 'monty':

  • Files located under the root-directory: test1.mp4, test2.mp4.
  • Files located under a 'bucks' sub-directory: bunny1.mp4, bunny2.mp4.

Get files LIST

We'll start this tutorial by sending a 'GET dir' request for our root directory in order to retrieve the files it contains: "http://rass.cdn01.rambla.be/dir/?kind=file".

  • The path part of the URL is set to "/dir/". Since the resource indicator is only followed by a slash, the GET request is pointed at the root-directory.
  • The query-string contains a kind argument which is set to "file", indicating that we only want to receive entries that reference files.

The (relevant) output from curl looks like this:

curl --url http://rass.cdn01.rambla.be/dir/?kind=file --request GET --user monty:mypwd -H "Accept: application/json" 
--verbose

> GET /dir/?kind=file HTTP/1.1
> Host: rass.cdn01.rambla.be
> Accept: application/json

< HTTP/1.1 200 OK
< Content-Type: application/json

{
"feed": {
"entry": [{
"content": {
"params": {
"mimetype": "video/mp4",
"kind": "file",
"name": "test1.mp4",
"filename": "test1.mp4",
"path": "/test1.mp4",
"size": "4537924"
}
},
"updated": "2011-09-05T12:00:15+02:00",
"link": [{
"href": "http://rass.cdn01.rambla.be/meta/monty/test1.mp4/?alt=json",
"type": "application/json",
"rel": "self"
}, {
"href": "http://monty.cdn01.rambla.be/test1.mp4",
"type": "video/mp4",
"rel": "enclosure"
}],
"id": "http://rass.cdn01.rambla.be/item/test1.mp4/"
}, {
"content": {
"params": {
"mimetype": "video/mp4",
"kind": "file",
"name": "test2.mp4",
"filename": "test2.mp4",
"path": "/test2.mp4",
"size": "7608204"
}
},
"updated": "2011-09-05T11:59:21+02:00",
"link": [{
"href": "http://rass.cdn01.rambla.be/meta/monty/test2.mp4/?alt=json",
"type": "application/json",
"rel": "self"
}, {
"href": "http://monty.cdn01.rambla.be/test2.mp4",
"type": "video/mp4",
"rel": "enclosure"
}],
"id": "http://rass.cdn01.rambla.be/item/test2.mp4/"
}],
"updated": "2012-10-12T14:57:51+02:00",
"link": [{
"href": "http://rass.cdn01.rambla.be/dir/?kind=file;page=1;paginate_by=100;",
"type": "application/json",
"rel": "self"
}, {
"href": "http://rass.cdn01.rambla.be/dir/?kind=file;page=1;paginate_by=100;",
"type": "application/json",
"rel": "last"
}],
"id": "http://rass.cdn01.rambla.be/dir/?kind=file;page=1;paginate_by=100;"
}

The response body contains a feed object which is the container for a number of entry sub-objects, each containing information about a file located directly under the root-directory. In this example, the feed contains 2 entries.

Each entry object has an id property, containing the URI at which the corresponding item resource can be accessed. Also note the 'enclosure' link which points to the file's public URL, to be used by end-users for downloading the file from the CDN. The file's properties are inside the params object (property of the content object).

Get files LIST FOR sub-directory

To get the files that are stored under our 'bucks' sub-directory, we simply have to add this directory to the URL path of our GET request: "http://rass.cdn01.rambla.be/dir/bucks/?kind=file". RASS will now return a feed with 2 entries referencing 'bunny1.mp4' and 'bunny2.mp4'. The feed's id element looks like this:

http://rass.cdn01.rambla.be/dir/bucks/?kind=file;page=1;paginate_by=100;alt=json

Get LIST OF sub-directories

In our previous requests, we've indicated to RASS that we only wanted information about files. In the same way, we can retrieve information about sub-directories by setting the 'kind' query-string argument to "dir". The following GET request will return all sub-directories of our CDN root-directory: "http://rass.cdn01.rambla.be/dir/?kind=dir;alt=json".

{
"feed": {
"entry": [{
"content": {
"params": {
"mimetype": "",
"kind": "dir",
"name": "bucks",
"filename": "",
"path": "/bucks",
"size": "31144785"
}
},
"updated": "2011-09-11T16:20:03+02:00",
"link": [{
"href": "http://rass.cdn01.rambla.be/dir/bucks/?kind=dir;alt=json",
"type": "application/json",
"rel": "self"
}],
"id": "http://rass.cdn01.rambla.be/dir/bucks/"
}],
"updated": "2012-10-12T15:04:08+02:00",
"link": [{
"href": "http://rass.cdn01.rambla.be/dir/?kind=dir;page=1;paginate_by=100;",
"type": "application/json",
"rel": "self"
}, {
"href": "http://rass.cdn01.rambla.be/dir/?kind=dir;page=1;paginate_by=100;",
"type": "application/json",
"rel": "last"
}],
"id": "http://rass.cdn01.rambla.be/dir/?kind=dir;page=1;paginate_by=100;"
},
"version": "1.0",
"encoding": "UTF-8"
}

Since we have only created a single sub-directory named "bucks", RASS returns a feed with a single entry. As you can see, the entry's kind param is now set to "dir". The path param contains the directory's relative path.

Syndicate content