Created
January 16, 2012 00:01
-
-
Save FrancisVarga/1618120 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package beatguide.core.jsonrpc | |
| { | |
| import flash.events.Event; | |
| import flash.events.EventDispatcher; | |
| import flash.events.HTTPStatusEvent; | |
| import flash.events.IEventDispatcher; | |
| import flash.events.IOErrorEvent; | |
| import flash.events.ProgressEvent; | |
| import flash.events.SecurityErrorEvent; | |
| import mx.rpc.events.FaultEvent; | |
| import mx.rpc.events.ResultEvent; | |
| import mx.rpc.http.HTTPService; | |
| /** | |
| * | |
| * @author Francis Varga | |
| * @date Mar 23, 2011 | |
| * | |
| */ | |
| public class TinyRPCClient extends EventDispatcher | |
| { | |
| private var _client : HTTPService; | |
| private var _method : String; | |
| private var _gatewayURL : String; | |
| private var _params : Object; | |
| private var _addData : Object; | |
| private var _headerData : Object; | |
| public function TinyRPCClient(target : IEventDispatcher = null) | |
| { | |
| super(target); | |
| this.initClient(); | |
| } | |
| public function getGatewayURL() : String | |
| { | |
| return _gatewayURL; | |
| } | |
| public function setGatewayURL(value : String) : void | |
| { | |
| _gatewayURL = value; | |
| } | |
| private function initClient() : void | |
| { | |
| _client = new HTTPService(); | |
| _client.addEventListener(FaultEvent.FAULT, onFault); | |
| _client.addEventListener(Event.COMPLETE, onResult); | |
| _client.addEventListener(IOErrorEvent.IO_ERROR, onIOError); | |
| _client.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurity); | |
| _client.addEventListener(HTTPStatusEvent.HTTP_STATUS, onHTTPStatus); | |
| _client.addEventListener(ProgressEvent.PROGRESS, onProgress); | |
| _client.addEventListener(HTTPStatusEvent.HTTP_RESPONSE_STATUS, onHttpResponseStatus); | |
| } | |
| private function onFault(event : FaultEvent) : void | |
| { | |
| dispatchEvent(event); | |
| } | |
| private function onHttpResponseStatus(event : HTTPStatusEvent) : void | |
| { | |
| dispatchEvent(event); | |
| } | |
| protected function onProgress(event : ProgressEvent) : void | |
| { | |
| dispatchEvent(event); | |
| } | |
| protected function onHTTPStatus(event : HTTPStatusEvent) : void | |
| { | |
| dispatchEvent(event); | |
| } | |
| private function onSecurity(event : SecurityErrorEvent) : void | |
| { | |
| var logJSONString : String = JSON.stringify(event); | |
| trace("================================================"); | |
| trace(logJSONString); | |
| trace("================================================"); | |
| dispatchEvent(event); | |
| } | |
| private function onIOError(event : IOErrorEvent) : void | |
| { | |
| var errorJSON : String = JSON.stringify(event); | |
| trace("================================================"); | |
| trace(errorJSON); | |
| trace("================================================"); | |
| dispatchEvent(event); | |
| } | |
| private function onResult(event : ResultEvent) : void | |
| { | |
| IEventDispatcher(event.currentTarget).removeEventListener(event.type, arguments['callee']); | |
| try | |
| { | |
| var newEvent : TinyRPCClientEvent = new TinyRPCClientEvent(TinyRPCClientEvent.COMPLETE); | |
| newEvent.data = JSON.parse(String(event.result))['result']; | |
| dispatchEvent(newEvent); | |
| } | |
| catch(error : Error) | |
| { | |
| throw error; | |
| } | |
| } | |
| public function sendRequest() : void | |
| { | |
| _method = _method || ""; | |
| _params = _params || ""; | |
| _gatewayURL = "http://beta.beatguide.me/api/v1/pub/index.php"; | |
| var jsonRequest : Object = {"id":1, "method":_method, "params":_params, "jsonrpc":"2.0"}; | |
| jsonRequest['extendsParams'] = _addData; | |
| var jsonRequestString : String = JSON.stringify(jsonRequest); | |
| _client.url = _gatewayURL; | |
| _client.method = "POST"; | |
| _client.request = jsonRequestString; | |
| _client.contentType = "application/json"; | |
| _client.resultFormat = "text"; | |
| _client.addEventListener(ResultEvent.RESULT, onResult); | |
| _client.send(); | |
| } | |
| public function setHeaderData(data : Object) : TinyRPCClient | |
| { | |
| _headerData = data; | |
| return this; | |
| } | |
| public function setAdditionData(addData : Object) : TinyRPCClient | |
| { | |
| _addData = addData; | |
| return this; | |
| } | |
| public function setGetewayURL(getewayURL : String) : TinyRPCClient | |
| { | |
| _gatewayURL = getewayURL; | |
| return this; | |
| } | |
| public function setMethod(method : String) : TinyRPCClient | |
| { | |
| _method = method; | |
| return this; | |
| } | |
| public function setParams(params : Object) : TinyRPCClient | |
| { | |
| _params = params; | |
| return this; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment