FlickrRestStore is an implementation of the dojo.data API provides access to the Flickr photo sharing site's REST API. Many advanced features are available, including tag search, sorting on numerous attributes, full text search, support for simultaneous clients, result caching and more.
Dojo has several examples of browser in-memory stores, such as dojo.data.ItemFileReadStore, dojox.data.CsvStore, and dojox.data.OmplStore. While these stores are useful and great examples of how data stores can be used to wrapper accessing data, they are not the only way data is served and processed. In many cases, data stores can wrapper external services. It is those services that perform the querying and filtering of data, and then provide only that as a subset back to the data store for presentation as items.
FlickrRestStore is one such store. The purpose of FlickrRestStore is to wrapper the public photo feed of the Flickr service. Then by simply using the FlickrRestStore store, as you would any data store in Dojo, you now have access to querying the vast repository of public photos made available by others on the Web. Look at http://archive.dojotoolkit.org/nightly/dojotoolkit/dojox/data/demos/demo... to see it in action, or look here for some example usages.
The following dojo.data APIs are implemented by FlickrRestStore
FlickrRestStore is built upon FlickrStore, a store which reads from Flickrs more simplistic public API. However, FlickrRestStore provides many more features:
Note: While this store wraps making calls to the Flickr service, as a user, you should still verify that you agree to the terms and conditions by which you are using the public Flickr photo service. Review their terms and conditions, and the API terms and conditions, here.
Another difference between FlickrRestStore and FlickrStore is that, due to the fact that FlickrRestStore works with the Flickr REST APIs, you will need to get a free API key from Flickr. You can do so at http://www.flickr.com/services/api/keys/apply/.
The Flickr service provides its data back in a wide variety of formats (for example, ATOM, RSS, and JSON) but FlickrRestStore only makes use of the JSON format. The following example shows a query that FlickrRestStore processes and the response:
Query the first three photos from a user:
URL: http://www.flickr.com/services/rest/?format=json&method=flickr.people.ge...
Response:
jsonFlickrApi({
"photos":{
"page":1,
"pages":98,
"perpage":3,
"total":"489",
"photo":[
{
"id":"1352049918",
"owner":"44153025@N00",
"secret":"5636009306",
"server":"1111",
"farm":2,
"title":"The Liffey Panorama",
"ispublic":1,
"isfriend":0,
"isfamily":0
},
{
"id":"1351120079",
"owner":"44153025@N00",
"secret":"880bf6a003",
"server":"1027",
"farm":2,
"title":"Many Hands make pretty flowers",
"ispublic":1,
"isfriend":0,
"isfamily":0
},
{
"id":"1322051485",
"owner":"44153025@N00",
"secret":"b7c529335d",
"server":"1110",
"farm":2,
"title":"Wok'n'Roll baby!",
"ispublic":1,
"isfriend":0,
"isfamily":0
}
]
},
"stat":"ok"}
)FlickrRestStore's role is to process the query parameters passed to the dojo.data.api.Read API and generate the appropriate service URL. It then processes the response from the service and handles accessing the items returned from the query. It also provides simple attribute access to all the values.
FlickrRestStore does not have any constructor parameters.
All items returned from FlickrRestStore have the following attributes that can be accessed using the dojo.data.api.Read API to retrieve data about the item:
The fetch method query syntax for FlickrRestStore is simple and straightforward. It allows the following attributes to be set and queried against:
start parameter is used. This is optional.If an attribute is not specified, the default is date-posted
. This is optional.
Note: Unlike many of the other example stores, the FlickrRestStore store cannot do wild-card matching of the attributes. This is because the Flickr public photo feed service cannot do it. In an ideal service implementation, the Flickr service would provide a mechanism by with to pass in wild cards as part of its query parameters.
The following example shows how you would query for the first ten images belonging to a single user, then emit the title, author, and image URL to the console:
var flickrRestStore = new dojox.data.FlickrRestStore();
function onBegin(totalCount, requestObj){
console.log("TOTAL IMAGE COUNT:" + totalCount);
}
function onItem(item, requestObj){
console.log("TITLE: " + flickrStore.getValue(item, "title");
console.log("AUTHOR: " + flickrStore.getValue(item, "author");
console.log("LINK: " + flickrStore.getValue(item, "link");
console.log("IMAGE URL: " + flickrStore.getValue(item, "imageUrl");
console.log("IMAGE URL Small: " + flickrStore.getValue(item, "imageUrlSmall");
console.log("IMAGE URL Medium: " + flickrStore.getValue(item, "imageUrlMedium");
console.log("IMAGE URL Thumbnail: " + flickrStore.getValue(item, "imageUrlThumb");
console.log("DATE TAKEN: " + flickrStore.getValue(item, "dateTaken");
console.log("DATE PUBLISHED: " + flickrStore.getValue(item, "datePublished");
}
function onComplete(items, request){
console.log("DONE!")
}
function onError(error, request){
console.log("FAILED!")
}
var request = {
query: {
userid: "44153025@N00",
apikey: "8c6803164dbc395fb7131c9d54843627"
},
onBegin: onBegin,
onItem: onItem,
onComplete: onComplete,
onError: onError
};
//Get ten photos from user "44153025@N00".
request.start = 0; //start at the beginning
request.count = 10; //Retrieve ten images
flickrRestStore.fetch(request);
//Get ten photos from user "44153025@N00",
//matching the tags volleyball or dublin.
request.start = 0; //start at the beginning
request.count = 10; //Retrieve ten images
request.query.tags = ["volleyball", "dublin"];
//This causes the search to match
//"volleyball" OR "dublin". To do an AND query,
//use request.query.tagmode = "all"
request.query.tagmode = "any";
flickrRestStore.fetch(request);
//Get fifty photos from user "44153025@N00",
//sorting descending on interestingness.
request.start = 0; //start at the beginning
request.count = 50; //Retrieve fifty images
request.query.tags = null; //delete the tags from the previous request
//The sort parameter is an array, as the
//dojo.data API specifies that a READ store should
//support multiple sort attributes.
//However, the Flickr API only supports a single
//sort parameter.
request.query.sort = [
{
//'attribute' could also be "date-taken" or "date-posted"
attribute: "interestingness",
descending: true
}
];
flickrRestStore.fetch(request);
//Get the second page of twenty photos from a given set
request.start = 20;
request.count = 20;
request.query.setid = "72157600959797470";
request.query.sort = null; //Clean up after the last request
flickrRestStore.fetch(request);
//Perform a full text search, retrieving 50 photos
request.start = 0;
request.count = 50;
//clean up after the previous request
request.query.setid = null;
//Finds all photos with "kinsale" in the title or description
request.query.text = "kinsale";
flickrRestStore.fetch(request);For further examples refer to the test cases provided in dojox/data/tests/stores/FlickrRestStore.js.