/ Published in: JavaScript
                    
                                        
Class to create Flickr-API REST calls.
                
                            
                                Expand |
                                Embed | Plain Text
                            
                        
                        Copy this code and paste it in your HTML
/* ***********************************************************************
* Class to create Flickr-API REST calls.
* See http://www.flickr.com/services/api/ for supported
* methods, params and returns.
* 1. You need a valid API-Key
* (http://www.flickr.com/services/apps/create/apply/)
* 2. This class is supported on
* - Internet Explorer 8+
* - Firefox 3.1+
* - Safari 4+
* - Chrome 3+
* - Opera 10.5+
* If you're using an older browser, include the json2.js library
* (https://github.com/douglascrockford/JSON-js/blob/master/json2.js)
* 3. This class only supports JSON-response-format
* 4. This class doesn's support authentication and methods which
* require user authorization.
*
* CONSTRUCTOR(apiKey):
* Parameters: apiKey:
* Your valid API key
*
* METHODS:
* Api(method, callback, params, cparams):
* Creates a async request for given method with given params.
* Parameters: method:
* Name of the Flickr-API-REST method as STRING.
* callback:
* Function to call after response. Should take minimum
* one parameter as API-Response as JS-Object,
* 2. optional parameter = cparams (callback-params)
* params:
* API-method parameters as Object
* cparams (optional). Additional parameters for call-
* back function.
* Return: VOID
*
* Api_S(method, callback)
* Creates a sync request for given method with given params.
* Parameters: See Flickr.Api-method
* Return: API-Response as JS-Object.
*
* //////////////// EXAMPLE /////////////////
* var f = new Flickr("[MyApiKey]");
* f.Api(
* "flickr.photos.search",
* function(resp){
* if(resp.stat != "fail"){
* alert("Number of photos: " + resp.photos.total);}
* }else{
* alert("API-error: " + resp.message);
* }
* },
* {user_id: "[MyFlickrUserId]"}
* );
*
* **********************************************************************/
function Flickr(apiKey){
if(!apiKey){throw "InvalidApiKeyProvided";}
this.key = apiKey;
this.Api = function(method, callback, params, cparams){
var xhttp = new XMLHttpRequest();
xhttp.open('GET', this.createUrl(method, params), true);
xhttp.onreadystatechange = function(){
if(xhttp.readyState == 4){
callback(JSON.parse(xhttp.responseText), cparams);
}
}
xhttp.send(null);
}
this.Api_S = function(method, params){
var xhttp = new XMLHttpRequest();
xhttp.open('GET', this.createUrl(method, params), false);
xhttp.send(null);
return JSON.parse(xhttp.responseText);
}
this.createUrl = function(method, params){
var url =
"http://api.flickr.com/services/rest/?method=" + method +
"&api_key=" + this.key +
"&format=json" +
"&nojsoncallback=1";
if(params){
for(var p in params){
url += "&" + p + "=" + params[p];
}
}
return url;
}
}
Comments
 Subscribe to comments
                    Subscribe to comments
                
                