Skip to content

Instantly share code, notes, and snippets.

@mash
Created October 28, 2008 08:49
Show Gist options
  • Save mash/20327 to your computer and use it in GitHub Desktop.
Save mash/20327 to your computer and use it in GitHub Desktop.
package net {
import flash.events.*;
import flash.net.*;
import caurina.transitions.Tweener;
import com.adobe.serialization.json.*;
public class API extends EventDispatcher{
private var debug :Boolean = false;
public function API(){
}
public function GET(o:Object):void{
logger("[GET]",o);
var l:URLLoader = new URLLoader();
l.addEventListener( Event.COMPLETE,function(e:Event):void{
l.removeEventListener( IOErrorEvent.IO_ERROR,onIOError );
l.removeEventListener( SecurityErrorEvent.SECURITY_ERROR,onSecurityError );
l.removeEventListener( Event.COMPLETE, arguments.callee );
var decoder :JSONDecoder = new JSONDecoder(e.target.data);
var json :Object = decoder.getValue();
o.onComplete(json);
});
l.addEventListener( IOErrorEvent.IO_ERROR,onIOError );
l.addEventListener( SecurityErrorEvent.SECURITY_ERROR,onSecurityError );
l.load( new URLRequest(o.url) );
}
public function POST(o:Object,v:URLVariables=null):void{
logger("[POST]",o);
var l:URLLoader = new URLLoader();
l.addEventListener( Event.COMPLETE,function(e:Event):void{
l.removeEventListener( IOErrorEvent.IO_ERROR,onIOError );
l.removeEventListener( SecurityErrorEvent.SECURITY_ERROR,onSecurityError );
l.removeEventListener( Event.COMPLETE, arguments.callee );
var decoder :JSONDecoder = new JSONDecoder(e.target.data);
var json :Object = decoder.getValue();
o.onComplete(json);
});
l.addEventListener( IOErrorEvent.IO_ERROR,onIOError );
l.addEventListener( SecurityErrorEvent.SECURITY_ERROR,onSecurityError );
var r:URLRequest = new URLRequest( o.url );
r.method = URLRequestMethod.POST;
r.data = v;
logger("[POST]r: ",r)
logger("[POST]r.data: ",r.data);
l.load( r );
}
private function onSecurityError(e:SecurityErrorEvent):void{
logger("[SecurityErrorEvent.SECURITY_ERROR]e: "+e);
}
private function onIOError(e:IOErrorEvent):void{
logger("[IOErrorEvent.IO_ERROR]e: "+e);
}
private function logger(... args):void{
if(!debug){ return; }
var a:Array = ["[API]"+args.shift()];
a = a.concat(args);
log(a);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment