A simple ActionScript / Flex REST client

When I started my new job a few months ago, we inherited a Cairngorm-based Flex REST client that was ugly, clunky and hard to maintain. Worst of all, due to Cairngorm’s brutal MVC model, it didn’t allow us to call the service in the context of the Flex component.

Our architect Tim re-wrote it into a single class, stripping out all the Cairngorm madness so it can be
called within a single service. More recently, I refactored the class into a singleton and removed some legacy Spring Security authentication — now handled by the Jsecurity plugin in Grails.

Combined with the Content Negotiation features of Grails, this allows us to quickly communicate with a flex application without the need to the complex binding provided by the BlazeDS platform from Adobe ( which, by the way, is available to Grails through the Flex Plugin ).

Rest.as

package
{
import mx.rpc.events.FaultEvent;
import flash.events.Event;
import mx.rpc.http.HTTPService;
import mx.messaging.messages.HTTPRequestMessage;
import mx.rpc.events.ResultEvent;
import EnvironmentVars

public class Rest
{

private static var instance : Rest;

public static function getInstance() : Rest {
if( instance == null )
instance = new Rest();
return instance;
}

public function doRestCall( url : String, resultFunction : Function, faultFunction : Function = null,
restMethod : String = “GET”, parms : Object = null ) : void
{
var httpService : HTTPService = new HTTPService( );

if ( restMethod.toUpperCase() != “GET” )
{
httpService.method = HTTPRequestMessage.POST_METHOD;
if( parms == null )
{
parms = new Object();
}
parms._method = restMethod;
}
else
{
httpService.method = HTTPRequestMessage.GET_METHOD;
}

httpService.url = EnvironmentVars.SERVER_URL + url;
httpService.resultFormat = “e4x”;
httpService.addEventListener( ResultEvent.RESULT, resultFunction );
if( faultFunction != null )
{
httpService.addEventListener( FaultEvent.FAULT, faultFunction );
}
httpService.send( parms );
}
}
}

Usage

Rest.getInstance().doRestCall( URL , resultFunction, faultFunction, method, object);

This is a singleton. So it only has one instance. You need to define a server url in your EnvironmentVars.as file, which should be accessible in this package. The faultFunction, method and object params are all optional.

Example

private var rest : Rest = Rest.getInstance();
[Bindable] private var files : XMLList = null;

rest.doRestCall(
“files/list.xml”,
function(result: ResultEvent):void
{
products = XML(result.result).file;
},
function(event: FaultEvent) : void
{
Alert.show(“error occured ” + event.fault);
}
);

You can also do POSTS by setting the method to “POST” and then passing an object that will get serialized with e4x. You can bind your XML to your components easily, as they don’t live in some far away carngorm service.

6 thoughts on “A simple ActionScript / Flex REST client

  1. Tomas Lin Post author

    That’s not quite right. We allow HTTP get and HTTP post as the request methods because that is what they accept. But if you look at the code, you’ll see that the object’s _method property gets set, which allows for DELETE, POST, PUT and the myriad of custom methods like the ones made by FACEBOOK API. One is an action, the other is just a form of posting this data ( POST vs GET )

    Reply

Leave a comment