Tutorial – Consuming REST web services in ActionScript 3 – Part 4

1 actionscript,as3,as3 tutorials,flash,webservices,work  |   December 11, 2009  |     2974

Hi Everyone,

this is a follow-up to the following posts:

In this post, I will add code to identify each request uniquely, give you a class called “AbstractClient” that will handle most of the mechanics and is easily extended. The idea is to give you a base on which to build. Essentially, for every service call you will want to make you will need 2 functions:

  • A public function that will send the request out
  • a protected function that will receive the response, digest it and dispatch an event

For those who’d like to go ahead and enjoy the goods right away, I will write this tutorial so it can be read and understood on it’s own.

Class Member Variables
AbstractClient will contain 2 variables:

  • protected var _requestQueue:Array; — used to store current requests
  • protected var _requestSequence:Number = 0; — used to give each request a unique identifier

So here is what it looks like so far:

So the queue is initialized to an empty array, and the sequence is set to 0.

The core functionality
So the idea is to keep currently occuring requests in the queue, and to describe the request using a wrapper object. This object will contain:

  • requestId
  • loader (URLLoader reference) that is running it
  • success flag
  • any other information the caller wishes to have

The wrapper object is passed as a parameter to the public function being called from the outside. We then add information into it and put it in the _requestQueue.

When the loader (URLLoader) receives the result, we get the proper wrapper object from the queue, and then use this information as necessary.

Here is the main function, called runLoader.

So basically the loader is created, we set the right format for the data to be sent and received, listen to all potential events. The Event.COMPLETE event handler is specified by the subclass calling this function. Then assign the wrapper properties and push it in the queue, and return the id of this request to the subclass’ function.

Error Handling
What’s left is the code to handle errors. This is, however, not fully implemented yet. First lets get a few function to retrieve the right Wrapper objects from the queue..

Next the actual error handling functions, which you will have to implement yourself..

Handling Progress
You could handle progress from a request, although I doubt that in most cases the “bytesTotal” will be know. Here is some sample code:

AbstractClient Complete Source
here is the source, complete..

Sample Usage
So as per Part 2, we woud like it to be a singleton. So here is the start..

Now, here is where the action happens..

This function is public, so it can be called by the object using this WSClient. It returns a number, the unique identifier of the request. First we create the URLRequest object pointing the url of your service. Then we assign GET method to it. Then we put all the variables inside of a URLVariables object, and assign it to the request. Then create the wrapper object, giving it certain params that could be useful later and call runLoader which does the magic for us.

You will notice that runLoader receives a reference to handleProductSizes which is actually a function that we need to define like so:

As a bonus, here is the code for the ProductSizeEvent

Hope this helps! full source code… well you can ask; but this is straightforward, I encourage READING and TRYING and getting it to work. This is invaluable experience and cannot be bought or copy-pasted.

Cheers!

Martin

Comments
  • March 12, 2014 at 2:42 pm

    Scott Berfield

    Just want to say thanks for the excellent tutorials. This is incredibly helpful stuff.

Leave a Reply