Tomás Lin’s Programming Brain Dump

Flex, Grails, Facebook, iPhone and all that Jazz

A simple ActionScript / Flex REST client

with 6 comments

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.

Written by Tomas Lin

June 12, 2008 at 1:46 am

Posted in Uncategorized

Tagged with , ,

6 Responses

Subscribe to comments with RSS.

  1. Nice Post, very very helpful, looking for some more REST posts. Keep it up

    -Jonnie

    Jonnie

    August 8, 2008 at 8:09 am

  2. Nice post!

    Debabrata Acharjee

    September 1, 2008 at 1:55 am

  3. Nice, but why do you limit yourself to HTTP GET or HTTP POST?

    I thought REST requires usage of both?

    Trails

    September 5, 2008 at 4:00 pm

  4. 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 )

    Tomas Lin

    September 5, 2008 at 6:29 pm

  5. Good job. Glad to see that others are working on ways to get around Flex’s REST limitations. I’ve been working on a replacement for the HTTPService that adds full rest support. It’s an open source project hosted on Google. If interested, you can find out more here:

    http://code.google.com/p/resthttpservice/

    Dustin

    January 8, 2009 at 8:43 pm

  6. Hi! I was surfing and found your blog post… nice! I love your blog. :) Cheers! Sandra. R.

    sandrar

    September 10, 2009 at 5:34 pm


Leave a Reply