Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nzaillian/717185 to your computer and use it in GitHub Desktop.
Save nzaillian/717185 to your computer and use it in GitHub Desktop.
Issuing queries against Flickr's REST API from AS3
/*
Example usage:
var fj:FlickrJSON = new FlickrJSON(YOUR_API_KEY);
fj.loader.addEventListener(Event.COMPLETE, YOUR_LOAD_COMPLETE_CALLBACK_FUNCTION);
fj.doCall(FLICKR_API_METHOD_AS_STRING, {PARAM_1_NAME_AS_STRING : PARAM_1_VALUE_AS_STRING, ...});
you will want to convert the response string that you get in your callback to a native Object. To do this, simply use the
static method FlickrJSON.objectFromResponse.
Note: leverages a json to AS3 Object deserialization function as3corelib
*/
public class FlickrJSON
{
import flash.events.*;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.utils.*;
import com.adobe.serialization.json.JSON;
private var apiHome:String = "http://api.flickr.com/services/rest";
private var baseParams:Object;
private var curRequest:URLRequest;
private var apiKey:String;
/*CRITICAL: owner of a FlickrJSON instance is expected to set loader callbacks...*/
public var loader:URLLoader;
/*constructor*/
public function FlickrJSON(apiKey:String)
{
this.apiKey=apiKey;
this.baseParams = {"format":"json"};
this.loader = new URLLoader();
}
/*This is a simple function to issue a REST request to Flickr's REST server. The JSON response
will be passed along to whatever function you have externally set as the "Event.COMPLETE" callback for
the public "loader" property of a given instance of this FlickrJSON class.*/
public function doCall(method:String, args:Object):void
{
var requestString:String;
requestString = this.apiHome + "/?api_key=" + this.apiKey + "&method=" + method;
for (var index:Object in this.baseParams)
{
requestString+="&" + index + "=" + this.baseParams[index];
}
for (var index:Object in args)
{
requestString += "&" + index + "=" + args[index];
}
this.curRequest = new URLRequest( requestString );
this.loader.load( this.curRequest );
}
/*Converts a String of the form returned by the JSON API at http://api.flickr.com/services/rest
and converts it into a Object*/
public static function objectFromResponse(response:String):Object
{
var jsonStr:String = response;
jsonStr = jsonStr.substring( "jsonFlickrApi(".length, jsonStr.length-1 );
var objectFromJSON:Object = JSON.decode( jsonStr );
return objectFromJSON;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment