Javascript Flickr API Helper


/ Published in: JavaScript
Save to your folder(s)

Class to create Flickr-API REST calls.


Copy this code and paste it in your HTML
  1. /* ***********************************************************************
  2.  * Class to create Flickr-API REST calls.
  3.  * See http://www.flickr.com/services/api/ for supported
  4.  * methods, params and returns.
  5.  * 1. You need a valid API-Key
  6.  * (http://www.flickr.com/services/apps/create/apply/)
  7.  * 2. This class is supported on
  8.  * - Internet Explorer 8+
  9.  * - Firefox 3.1+
  10.  * - Safari 4+
  11.  * - Chrome 3+
  12.  * - Opera 10.5+
  13.  * If you're using an older browser, include the json2.js library
  14.  * (https://github.com/douglascrockford/JSON-js/blob/master/json2.js)
  15.  * 3. This class only supports JSON-response-format
  16.  * 4. This class doesn's support authentication and methods which
  17.  * require user authorization.
  18.  *
  19.  * CONSTRUCTOR(apiKey):
  20.  * Parameters: apiKey:
  21.  * Your valid API key
  22.  *
  23.  * METHODS:
  24.  * Api(method, callback, params, cparams):
  25.  * Creates a async request for given method with given params.
  26.  * Parameters: method:
  27.  * Name of the Flickr-API-REST method as STRING.
  28.  * callback:
  29.  * Function to call after response. Should take minimum
  30.  * one parameter as API-Response as JS-Object,
  31.  * 2. optional parameter = cparams (callback-params)
  32.  * params:
  33.  * API-method parameters as Object
  34.  * cparams (optional). Additional parameters for call-
  35.  * back function.
  36.  * Return: VOID
  37.  *
  38.  * Api_S(method, callback)
  39.  * Creates a sync request for given method with given params.
  40.  * Parameters: See Flickr.Api-method
  41.  * Return: API-Response as JS-Object.
  42.  *
  43.  * //////////////// EXAMPLE /////////////////
  44.  * var f = new Flickr("[MyApiKey]");
  45.  * f.Api(
  46.  * "flickr.photos.search",
  47.  * function(resp){
  48.  * if(resp.stat != "fail"){
  49.  * alert("Number of photos: " + resp.photos.total);}
  50.  * }else{
  51.  * alert("API-error: " + resp.message);
  52.  * }
  53.  * },
  54.  * {user_id: "[MyFlickrUserId]"}
  55.  * );
  56.  *
  57.  * **********************************************************************/
  58. function Flickr(apiKey){
  59. if(!apiKey){throw "InvalidApiKeyProvided";}
  60. this.key = apiKey;
  61. this.Api = function(method, callback, params, cparams){
  62.  
  63. var xhttp = new XMLHttpRequest();
  64. xhttp.open('GET', this.createUrl(method, params), true);
  65. xhttp.onreadystatechange = function(){
  66. if(xhttp.readyState == 4){
  67. callback(JSON.parse(xhttp.responseText), cparams);
  68. }
  69. }
  70. xhttp.send(null);
  71. }
  72. this.Api_S = function(method, params){
  73. var xhttp = new XMLHttpRequest();
  74. xhttp.open('GET', this.createUrl(method, params), false);
  75. xhttp.send(null);
  76. return JSON.parse(xhttp.responseText);
  77. }
  78. this.createUrl = function(method, params){
  79. var url =
  80. "http://api.flickr.com/services/rest/?method=" + method +
  81. "&api_key=" + this.key +
  82. "&format=json" +
  83. "&nojsoncallback=1";
  84. if(params){
  85. for(var p in params){
  86. url += "&" + p + "=" + params[p];
  87. }
  88. }
  89. return url;
  90. }
  91. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.