// see also: http://www.flickr.com/groups_topic.gne?id=4453 // allows us to attach the FlickrAPIInterfacesRest interface to any old object Function.prototype.implement = function(obj) { for (var i in obj ) { this.prototype[i] = obj[i]; } } _global.FlickrAPIInterfacesRest = {}; FlickrAPIInterfacesRest.fapiirest_trace = function (txt) { trace(txt); } FlickrAPIInterfacesRest.fapiirest_getAPIKey = function () { return ''; } FlickrAPIInterfacesRest.fapiirest_send_REST = function (APIMethod, params) { if (typeof params != 'object') params = {}; params.method = APIMethod; var RESTURL = 'http://api.flickr.com/services/rest/'; RESTURL+= '?api_key='+this.fapiirest_getAPIKey(); for (var p in params) { RESTURL+= '&' + p + '=' + escape(params[p]); } RESTURL+= '&cb='+new Date().getTime(); params.RESTURL = RESTURL; var callingOBJ = this; var RESTXML = new XML(); RESTXML.ignoreWhite = true; RESTXML.onLoad = function(success) { callingOBJ.fapiirest_onLoad(success, this, APIMethod, params); } this.fapiirest_trace('RESTURL: '+RESTURL); RESTXML.load(RESTURL); return true; } FlickrAPIInterfacesRest.fapiirest_getCallBackName = function (dotted) { return dotted.split('.').join('_')+'_onLoad'; } FlickrAPIInterfacesRest.fapiirest_onLoad = function (success, rsp, APIMethod, params) { this.fapiirest_trace(params.method + 'fapiirest_onLoad'); var callBackName = this.fapiirest_getCallBackName(APIMethod); var callBack = this[callBackName]; var txt = 'The response to a call to '+APIMethod +' '; if (success) { if (rsp.firstChild.attributes.stat == 'ok') { this.fapiirest_trace(txt + 'was ok: '+rsp.toString()); } else { if (rsp.firstChild.attributes.stat == 'fail') { txt+= 'was fail ('; if (rsp.firstChild.firstChild.attributes.code == '0') { txt+= 'method not found'; } else if (rsp.firstChild.firstChild.attributes.code == '10') { txt+= 'method disabled'; } else if (rsp.firstChild.firstChild.attributes.code == '99') { txt+= 'method not found'; } else if (rsp.firstChild.firstChild.attributes.code == '100') { txt+= 'bad API key'; } else { txt+= 'method specific error'; } this.fapiirest_trace(txt + '): ' + rsp.toString()); } else { this.fapiirest_trace(txt + 'was unrecognized: ' + rsp.toString()); } } } else { this.fapiirest_trace(txt + 'was undefined.'); } callBack.apply(this, [success, rsp, params]); } FlickrAPIInterfacesRest.__resolve = function(name) { if (name.indexOf('flickr_') != 0) return; if (name.indexOf('_onLoad') != -1) return; var APIMethod = name.split('_').join('.') this[name] = function(params) { return this.fapiirest_send_REST(APIMethod, params); } return this[name]; } /*------------------------------------------ USAGE BELOW --------------------------------------------*/ // if you want to attach this interface to an object, build a class constructor: _global.myClass = function() {} myClass.implement(FlickrAPIInterfacesRest); // now you can make an object that will have the FlickrAPIInterfacesRest interface: var myObject = new myClass(); // and you can define a callBack handler for any flickr API method like this: myObject.flickr_test_echo_onLoad = function(success, rsp, params) { /* - success is boolean - rsp is the XML response from the server as an actionscript XML object - params is a generic object that contains the parameters you passed to the API, plus a method attribute and a RESTURL attribute */ if (success) { // could not even get a response at all :( } else { if (rsp.firstChild.attributes.stat == 'ok') { // this is what we'd hoped for } else { // inspect the rsp XML to see what went wrong. } } } // and you can call any flickr API method like this (API key and // method are handled internally; you canont pass them as parameters): myObject.flickr_test_echo({foo:'bar', bar:'foo'});