the first part of this tutorial can be found here: Tutorial – Consuming REST web services in ActionScript 3 – Part 1.
Today I will continue on the exploration of consuming REST web services in ActionScript 3. We will cover possible errors first, then create a versatile web service client object which is easily adaptable to all your projects.
Yesterday in my exploration of the new YouTube Data API roll-out, I ran into a wall concerning PUT and DELETE requests. The method described in these tutorials doesn’t permit those operations because of limitations in the URLRequest object. It actually throws exceptions if you try to put the .method parameter to anything other than GET or POST. I find this ridiculous. Completely ridiculous. The method of the HTTP Request is a simple string in the HTTP header, 3 or 4 characters long. Why not give us control over it? For Flex users, you can use the HTTPService class, but Flash users? Left to nada. I prefer not using Flex, it’s BLOATED. More on that in another post..
Where we were
At the end of the last tutorial, we had the following code:
Then for the handler function, where we converted the results to XML
So how many events do the URLLoader throw? And what do they do? Here is a summary :
I am mostly interested in “complete”, “ioError”, “securityError”, and eventually for requests which return anything other than 200 (201 Created is one example), we need to catch the “httpStatus”. So let’s handle those new events..
For these new error event handlers, we implement the functions. What you do exactly inside those functions depends on the situation, you might want to display an error message, notify another service of the problem, or simply trace them to your output if you are in testing phase. This is what I will demonstrate now.
On with the Singleton
The idea of the Singleton design pattern is to create an object that can be easily accessible from anywhere in your code. You import it, then you do :
and off you go. You will always deal with the same object as this pattern ensures you only have one instance of it. In my head a little bulb lights up and says: this is what you need! Here is the code we need to create our singleton:
Now, it is impossible to have a private or protected constructor. Hence some people came up with nifty tricks to enfore the Singleton; personally I work alone on most projects and don’t need to. You can search on google for “as3 singleton” and you’ll find good examples.
To use the WSClient we would do
But then you might ask, how does my calling code know when the results are ready?
Dispatching Events when the data comes in..
Once the data is handled by our handleResults function, it would be ideal if the WSClient would dispatch an event, telling the calling code that the results are ready to be used. We will therefore derive from EventDispatcher (some people prefer composition) this way:
Now for the dispatching of the event:
Our caller code can now be aware that the data is ready, but how to access it?
The approach I like is to create custom event classes. One such event class is used to notify the caller code that the data is ready and transport the data to it. To do this, we need to derive from the flash.event.Event class. We will define one event type, add a property to contain the data, and define a getter to access it:
On with dispatching the custom event:
Back to the caller code:
Looks like there will be a third installment! I will describe how to handle situations were multiple callers use the same function: how to ensure the results you are getting are yours and not the results from somebody else’s request. I will also standardize certain tasks inside of the WSClient.
[…] the next installment, I demonstrate how I wrap all this functionality in a […]
very good I waiting for this long time ago and I cannot wait for the third part, thanks!!!
[…] This is the third installment of the series, you can find the first two here: Part 1 – http://blog.martinlegris.com/?p=87 Part 2 – http://blog.martinlegris.com/?p=90 […]
Any news on the rare HTTPService/URLRequest implementation and other HTTP methods like PUT/HEAD? Do you know if something changed with Flash Player 10? I could find a bug report on Adobe’s JIRA yet.
Thanks for this, it is great! But I’m stuck. Maybe I’m just not getting it, but what is the YouTubeClient() all about? I assume it has to do with the YouTube Data API.
What if I wasn’t using that, what would I put there instead?
Disregard the previous message! I’m an idiot.
You could always use the HTTP Service class from inside Flash.
I like this tutorial though… Very helpful.
Is there a sourcecode example available? I am trying to implement a webservice call in AS3. Thnks in advance