rollback flash apiinvoker based on 8ee8eddcff26d72a89538bcdac39f2f730acd3b8

This commit is contained in:
wing328 2015-06-09 13:33:30 +08:00
parent 7fd996bc86
commit a593271de1

View File

@ -1,41 +1,8 @@
package io.swagger.common
{
import asaxb.xml.bind.ASAXBContext;
import asaxb.xml.bind.Unmarshaller;
package io.swagger.common {
import io.swagger.event.ApiClientEvent;
import io.swagger.event.Response;
import io.swagger.common.ApiUserCredentials;
import flash.events.EventDispatcher;
import flash.utils.Dictionary;
import flash.utils.describeType;
import flash.xml.XMLDocument;
import flash.xml.XMLNode;
import mx.messaging.ChannelSet;
import mx.messaging.channels.HTTPChannel;
import mx.messaging.messages.HTTPRequestMessage;
import mx.rpc.AsyncToken;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;
import mx.rpc.xml.SimpleXMLEncoder;
import mx.utils.ObjectUtil;
public class ApiInvoker extends EventDispatcher
{
private var _apiUsageCredentials:ApiUserCredentials;
internal var _apiProxyServerUrl:String = "";
private var _baseUrl: String = "";
internal var _useProxyServer: Boolean = true;
private var _proxyHostName:String = "";
private var _apiPath: String = "";
private var _proxyPath: String = "";
public var _apiEventNotifier:EventDispatcher;
public class ApiInvoker extends EventDispatcher {
private static const DELETE_DATA_DUMMY:String = "dummyDataRequiredForDeleteOverride";
private static const X_HTTP_OVERRIDE_KEY:String = "X-HTTP-Method-Override";
@ -52,6 +19,14 @@ public class ApiInvoker extends EventDispatcher
_apiProxyServerUrl = _apiUsageCredentials.apiProxyServerUrl;
_apiEventNotifier = eventNotifier;
}
public var _apiEventNotifier:EventDispatcher;
internal var _apiProxyServerUrl:String = "";
internal var _useProxyServer:Boolean = true;
private var _apiUsageCredentials:ApiUserCredentials;
private var _baseUrl:String = "";
private var _proxyHostName:String = "";
private var _apiPath:String = "";
private var _proxyPath:String = "";
public function invokeAPI(resourceURL:String, method:String, queryParams:Dictionary, postObject:Object, headerParams:Dictionary):AsyncToken {
//make the communication
@ -95,119 +70,6 @@ public class ApiInvoker extends EventDispatcher
}
private function doRestCall( url : String, resultFunction : Function, faultFunction : Function = null,
restMethod : String = "GET",
bodyData : Object = null, headers: Object = null, contentType:String = "application/xml" ) : AsyncToken
{
var httpService : HTTPService = new HTTPService( );
if(headers == null){
headers = new Object();
}
httpService.method = restMethod;
if ( restMethod.toUpperCase() != HTTPRequestMessage.GET_METHOD )
{
//httpService.method = HTTPRequestMessage.POST_METHOD; - not required as we're using the proxy
if( bodyData == null )
{
bodyData = new Object();
}
if(restMethod == HTTPRequestMessage.DELETE_METHOD){
headers[X_HTTP_OVERRIDE_KEY]= HTTPRequestMessage.DELETE_METHOD;
bodyData = DELETE_DATA_DUMMY;
}
else if(restMethod == HTTPRequestMessage.PUT_METHOD){
headers[X_HTTP_OVERRIDE_KEY]= HTTPRequestMessage.PUT_METHOD;
}
else{
headers[CONTENT_TYPE_HEADER_KEY]= contentType;
}
}
else
{
//if the request type is GET and content type is xml then the Flex HTTPService converts it to a POST ... yeah
contentType = null;
}
httpService.url = url;
httpService.contentType = contentType;
httpService.resultFormat = "e4x";
httpService.headers = headers;
httpService.addEventListener( ResultEvent.RESULT, resultFunction );
if( faultFunction != null )
{
httpService.addEventListener( FaultEvent.FAULT, faultFunction );
}
if(_useProxyServer){
httpService.useProxy = true;
var channelSet: ChannelSet = new ChannelSet();
var httpChannel: HTTPChannel = new HTTPChannel();
httpChannel.uri = ApiUrlHelper.getProxyUrl(_proxyHostName, _proxyPath);
channelSet.addChannel(httpChannel);
httpService.channelSet = channelSet;
}
return httpService.send( bodyData );
}
private function onApiRequestResult(event:ResultEvent):void
{
var completionListener: Function = event.token.completionListener;
var result: Object = event.result;
var resultType: Class = event.token.returnType;
var resultObject:Object;
if(resultType != null) {
var context:ASAXBContext = ASAXBContext.newInstance(resultType);
var unmarshaller:Unmarshaller = context.createUnmarshaller();
var resultXML: XML = new XML(event.result);
try{
resultObject = unmarshaller.unmarshal(resultXML);
}
catch(error: TypeError){
var errorResponse: Response = new Response(false, null, "Could not unmarshall response");
if (_apiEventNotifier != null) { //dispatch event via assigned dispatcher
var failureEvent: ApiClientEvent = new ApiClientEvent(event.token.completionEventType);
failureEvent.response = errorResponse;
_apiEventNotifier.dispatchEvent(failureEvent);
}
}
if(resultObject is ListWrapper){
resultObject = ListWrapper(resultObject).getList();
}
}
var response : Response = new Response(true, resultObject);
response.requestId = event.token.requestId;
var successEventType: String = event.token.completionEventType != null ? event.token.completionEventType : ApiClientEvent.SUCCESS_EVENT;
if (_apiEventNotifier != null) { //dispatch event via assigned dispatcher
var successEvent: ApiClientEvent = new ApiClientEvent(successEventType);
successEvent.response = response;
_apiEventNotifier.dispatchEvent(successEvent);
}
}
private function onApiRequestFault(event:FaultEvent):void
{
var completionListener: Function = event.token.completionListener;
if(completionListener != null){
completionListener.call( null, new Response( false, null, event.fault.faultString) );
}
var failureEventType: String = event.token.completionEventType != null ? event.token.completionEventType : ApiClientEvent.FAILURE_EVENT;
if (_apiEventNotifier != null) { //dispatch event via assigned dispatcher
var failureEvent: ApiClientEvent = new ApiClientEvent(failureEventType);
failureEvent.response = new Response( false, null, event.fault.faultString);
_apiEventNotifier.dispatchEvent(failureEvent);
}
}
public function marshal(source:Object):Object {
// trace("marshal got - " + source)
if (source is String) {
@ -227,8 +89,7 @@ public class ApiInvoker extends EventDispatcher
return marshalObject(source);
}
public function marshalObject(source:Object):XML
{
public function marshalObject(source:Object):XML {
var writer:XMLWriter = new XMLWriter();
var objDescriptor:XML = describeType(source);
var property:XML;
@ -273,6 +134,111 @@ public class ApiInvoker extends EventDispatcher
return str;
}
private function doRestCall(url:String, resultFunction:Function, faultFunction:Function = null,
restMethod:String = "GET",
bodyData:Object = null, headers:Object = null, contentType:String = "application/xml"):AsyncToken {
var httpService:HTTPService = new HTTPService();
if (headers == null) {
headers = new Object();
}
httpService.method = restMethod;
if (restMethod.toUpperCase() != HTTPRequestMessage.GET_METHOD) {
//httpService.method = HTTPRequestMessage.POST_METHOD; - not required as we're using the proxy
if (bodyData == null) {
bodyData = new Object();
}
if (restMethod == HTTPRequestMessage.DELETE_METHOD) {
headers[X_HTTP_OVERRIDE_KEY] = HTTPRequestMessage.DELETE_METHOD;
bodyData = DELETE_DATA_DUMMY;
}
else if (restMethod == HTTPRequestMessage.PUT_METHOD) {
headers[X_HTTP_OVERRIDE_KEY] = HTTPRequestMessage.PUT_METHOD;
}
else {
headers[CONTENT_TYPE_HEADER_KEY] = contentType;
}
}
else {
//if the request type is GET and content type is xml then the Flex HTTPService converts it to a POST ... yeah
contentType = null;
}
httpService.url = url;
httpService.contentType = contentType;
httpService.resultFormat = "e4x";
httpService.headers = headers;
httpService.addEventListener(ResultEvent.RESULT, resultFunction);
if (faultFunction != null) {
httpService.addEventListener(FaultEvent.FAULT, faultFunction);
}
if (_useProxyServer) {
httpService.useProxy = true;
var channelSet:ChannelSet = new ChannelSet();
var httpChannel:HTTPChannel = new HTTPChannel();
httpChannel.uri = ApiUrlHelper.getProxyUrl(_proxyHostName, _proxyPath);
channelSet.addChannel(httpChannel);
httpService.channelSet = channelSet;
}
return httpService.send(bodyData);
}
private function onApiRequestResult(event:ResultEvent):void {
var completionListener:Function = event.token.completionListener;
var result:Object = event.result;
var resultType:Class = event.token.returnType;
var resultObject:Object;
if (resultType != null) {
var context:ASAXBContext = ASAXBContext.newInstance(resultType);
var unmarshaller:Unmarshaller = context.createUnmarshaller();
var resultXML:XML = new XML(event.result);
try {
resultObject = unmarshaller.unmarshal(resultXML);
}
catch (error:TypeError) {
var errorResponse:Response = new Response(false, null, "Could not unmarshall response");
if (_apiEventNotifier != null) { //dispatch event via assigned dispatcher
var failureEvent:ApiClientEvent = new ApiClientEvent(event.token.completionEventType);
failureEvent.response = errorResponse;
_apiEventNotifier.dispatchEvent(failureEvent);
}
}
if (resultObject is ListWrapper) {
resultObject = ListWrapper(resultObject).getList();
}
}
var response:Response = new Response(true, resultObject);
response.requestId = event.token.requestId;
var successEventType:String = event.token.completionEventType != null ? event.token.completionEventType : ApiClientEvent.SUCCESS_EVENT;
if (_apiEventNotifier != null) { //dispatch event via assigned dispatcher
var successEvent:ApiClientEvent = new ApiClientEvent(successEventType);
successEvent.response = response;
_apiEventNotifier.dispatchEvent(successEvent);
}
}
private function onApiRequestFault(event:FaultEvent):void {
var completionListener:Function = event.token.completionListener;
if (completionListener != null) {
completionListener.call(null, new Response(false, null, event.fault.faultString));
}
var failureEventType:String = event.token.completionEventType != null ? event.token.completionEventType : ApiClientEvent.FAILURE_EVENT;
if (_apiEventNotifier != null) { //dispatch event via assigned dispatcher
var failureEvent:ApiClientEvent = new ApiClientEvent(failureEventType);
failureEvent.response = new Response(false, null, event.fault.faultString);
_apiEventNotifier.dispatchEvent(failureEvent);
}
}
private function getArrayEnclosure(arr:Array):String {
if (arr != null && arr.length > 0) {
var className:String = flash.utils.getQualifiedClassName(arr[0])