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.