FIX: Fix the flash client library.

This commit is contained in:
glederrey 2016-02-05 12:16:27 +01:00
parent 192f2367b3
commit 00349406f8
5 changed files with 101 additions and 15 deletions

View File

@ -29,7 +29,7 @@ public class FlashClientCodegen extends DefaultCodegen implements CodegenConfig
protected String packageVersion; protected String packageVersion;
protected String invokerPackage = "io.swagger"; protected String invokerPackage = "io.swagger";
protected String sourceFolder = "src/main/flex"; protected String sourceFolder = "flash";
public FlashClientCodegen() { public FlashClientCodegen() {
super(); super();
@ -83,7 +83,7 @@ public class FlashClientCodegen extends DefaultCodegen implements CodegenConfig
.defaultValue("1.0.0")); .defaultValue("1.0.0"));
cliOptions.add(new CliOption(CodegenConstants.INVOKER_PACKAGE, CodegenConstants.INVOKER_PACKAGE_DESC)); cliOptions.add(new CliOption(CodegenConstants.INVOKER_PACKAGE, CodegenConstants.INVOKER_PACKAGE_DESC));
cliOptions.add(new CliOption(CodegenConstants.SOURCE_FOLDER, "source folder for generated " + cliOptions.add(new CliOption(CodegenConstants.SOURCE_FOLDER, "source folder for generated " +
"code. e.g. src/main/flex")); "code. e.g. flash"));
} }
@ -124,8 +124,7 @@ public class FlashClientCodegen extends DefaultCodegen implements CodegenConfig
//modelPackage = invokerPackage + File.separatorChar + "client" + File.separatorChar + "model"; //modelPackage = invokerPackage + File.separatorChar + "client" + File.separatorChar + "model";
//apiPackage = invokerPackage + File.separatorChar + "client" + File.separatorChar + "api"; //apiPackage = invokerPackage + File.separatorChar + "client" + File.separatorChar + "api";
final String invokerFolder = (sourceFolder + File.separator + invokerPackage + File.separator final String invokerFolder = (sourceFolder + File.separator + "src/" + invokerPackage + File.separator).replace(".", File.separator).replace('.', File.separatorChar);
+ "swagger" + File.separator).replace(".", File.separator).replace('.', File.separatorChar);
supportingFiles.add(new SupportingFile("ApiInvoker.as", invokerFolder + "common", "ApiInvoker.as")); supportingFiles.add(new SupportingFile("ApiInvoker.as", invokerFolder + "common", "ApiInvoker.as"));
supportingFiles.add(new SupportingFile("ApiUrlHelper.as", invokerFolder + "common", "ApiUrlHelper.as")); supportingFiles.add(new SupportingFile("ApiUrlHelper.as", invokerFolder + "common", "ApiUrlHelper.as"));
@ -133,14 +132,15 @@ public class FlashClientCodegen extends DefaultCodegen implements CodegenConfig
supportingFiles.add(new SupportingFile("ListWrapper.as", invokerFolder + "common", "ListWrapper.as")); supportingFiles.add(new SupportingFile("ListWrapper.as", invokerFolder + "common", "ListWrapper.as"));
supportingFiles.add(new SupportingFile("SwaggerApi.as", invokerFolder + "common", "SwaggerApi.as")); supportingFiles.add(new SupportingFile("SwaggerApi.as", invokerFolder + "common", "SwaggerApi.as"));
supportingFiles.add(new SupportingFile("XMLWriter.as", invokerFolder + "common", "XMLWriter.as")); supportingFiles.add(new SupportingFile("XMLWriter.as", invokerFolder + "common", "XMLWriter.as"));
supportingFiles.add(new SupportingFile("ApiError.as", invokerFolder + "exception", "ApiErrors.as")); supportingFiles.add(new SupportingFile("ApiError.as", invokerFolder + "exception", "ApiError.as"));
supportingFiles.add(new SupportingFile("ApiErrorCodes.as", invokerFolder + "exception", "ApiErrorCodes.as")); supportingFiles.add(new SupportingFile("ApiErrorCodes.as", invokerFolder + "exception", "ApiErrorCodes.as"));
supportingFiles.add(new SupportingFile("ApiClientEvent.as", invokerFolder + "event", "ApiClientEvent.as")); supportingFiles.add(new SupportingFile("ApiClientEvent.as", invokerFolder + "event", "ApiClientEvent.as"));
supportingFiles.add(new SupportingFile("Response.as", invokerFolder + "event", "Response.as")); supportingFiles.add(new SupportingFile("Response.as", invokerFolder + "event", "Response.as"));
supportingFiles.add(new SupportingFile("build.properties", sourceFolder, "build.properties")); supportingFiles.add(new SupportingFile("build.properties", sourceFolder, "build.properties"));
supportingFiles.add(new SupportingFile("build.xml", sourceFolder, "build.xml")); supportingFiles.add(new SupportingFile("build.xml", sourceFolder, "build.xml"));
supportingFiles.add(new SupportingFile("AirExecutorApp-app.xml", sourceFolder + File.separatorChar supportingFiles.add(new SupportingFile("README.txt", sourceFolder, "README.txt"));
+ "bin", "AirExecutorApp-app.xml")); //supportingFiles.add(new SupportingFile("AirExecutorApp-app.xml", sourceFolder + File.separatorChar
// + "bin", "AirExecutorApp-app.xml"));
supportingFiles.add(new SupportingFile("ASAXB-0.1.1.swc", sourceFolder + File.separatorChar supportingFiles.add(new SupportingFile("ASAXB-0.1.1.swc", sourceFolder + File.separatorChar
+ "lib", "ASAXB-0.1.1.swc")); + "lib", "ASAXB-0.1.1.swc"));
supportingFiles.add(new SupportingFile("as3corelib.swc", sourceFolder + File.separatorChar supportingFiles.add(new SupportingFile("as3corelib.swc", sourceFolder + File.separatorChar
@ -181,13 +181,13 @@ public class FlashClientCodegen extends DefaultCodegen implements CodegenConfig
@Override @Override
public String apiFileFolder() { public String apiFileFolder() {
return (outputFolder + File.separatorChar + sourceFolder + File.separatorChar return (outputFolder + File.separatorChar + sourceFolder + File.separatorChar + "src/"
+ apiPackage().replace('.', File.separatorChar)).replace('/', File.separatorChar); + apiPackage().replace('.', File.separatorChar)).replace('/', File.separatorChar);
} }
@Override @Override
public String modelFileFolder() { public String modelFileFolder() {
return (outputFolder + File.separatorChar + sourceFolder + File.separatorChar return (outputFolder + File.separatorChar + sourceFolder + File.separatorChar + "src/"
+ modelPackage().replace('.', File.separatorChar)).replace('/', File.separatorChar); + modelPackage().replace('.', File.separatorChar)).replace('/', File.separatorChar);
} }
@ -253,7 +253,7 @@ public class FlashClientCodegen extends DefaultCodegen implements CodegenConfig
} else if (p instanceof ArrayProperty) { } else if (p instanceof ArrayProperty) {
return "new Array()"; return "new Array()";
} else { } else {
return "null"; return "NaN";
} }
} }

View File

@ -2,6 +2,19 @@ package io.swagger.common {
import io.swagger.event.ApiClientEvent; import io.swagger.event.ApiClientEvent;
import io.swagger.event.Response; import io.swagger.event.Response;
import flash.events.EventDispatcher;
import flash.utils.*;
import mx.rpc.AsyncToken;
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
import mx.utils.ObjectUtil;
import mx.rpc.http.HTTPService;
import mx.messaging.messages.HTTPRequestMessage;
import mx.messaging.ChannelSet;
import mx.messaging.channels.DirectHTTPChannel;
import asaxb.xml.bind.ASAXBContext;
import asaxb.xml.bind.Unmarshaller;
public class ApiInvoker extends EventDispatcher { public class ApiInvoker extends EventDispatcher {
private static const DELETE_DATA_DUMMY:String = "dummyDataRequiredForDeleteOverride"; private static const DELETE_DATA_DUMMY:String = "dummyDataRequiredForDeleteOverride";
@ -99,7 +112,7 @@ public class ApiInvoker extends EventDispatcher {
var qualifiedClassName:String = objDescriptor.@name; var qualifiedClassName:String = objDescriptor.@name;
qualifiedClassName = qualifiedClassName.replace("::", "."); qualifiedClassName = qualifiedClassName.replace("::", ".");
var className:String = qualifiedClassName.substring(qualifiedClassName.lastIndexOf(".") + 1); var className:String = qualifiedClassName.substring(qualifiedClassName.lastIndexOf(".") + 1);
className = className().toLowerCase() + className.substring(1); className = className.toLowerCase() + className.substring(1);
writer.xml.setName(className); writer.xml.setName(className);
for each(property in objDescriptor.elements("variable")) { for each(property in objDescriptor.elements("variable")) {
@ -178,7 +191,7 @@ public class ApiInvoker extends EventDispatcher {
httpService.useProxy = true; httpService.useProxy = true;
var channelSet:ChannelSet = new ChannelSet(); var channelSet:ChannelSet = new ChannelSet();
var httpChannel:HTTPChannel = new HTTPChannel(); var httpChannel:DirectHTTPChannel = new DirectHTTPChannel("");
httpChannel.uri = ApiUrlHelper.getProxyUrl(_proxyHostName, _proxyPath); httpChannel.uri = ApiUrlHelper.getProxyUrl(_proxyHostName, _proxyPath);
channelSet.addChannel(httpChannel); channelSet.addChannel(httpChannel);
httpService.channelSet = channelSet; httpService.channelSet = channelSet;
@ -188,6 +201,11 @@ public class ApiInvoker extends EventDispatcher {
} }
private function onApiRequestResult(event:ResultEvent):void { private function onApiRequestResult(event:ResultEvent):void {
// I put this in comments. Here, it's trying to parse XML and it fails if it's not XML.
// Therefore, it's better to have the raw result that we can parse as we want.
// TODO: Create different parser (JSON, XML, etc.)
/*
var completionListener:Function = event.token.completionListener; var completionListener:Function = event.token.completionListener;
var result:Object = event.result; var result:Object = event.result;
var resultType:Class = event.token.returnType; var resultType:Class = event.token.returnType;
@ -214,6 +232,9 @@ public class ApiInvoker extends EventDispatcher {
} }
var response:Response = new Response(true, resultObject); var response:Response = new Response(true, resultObject);
*/
var response:Response = new Response(true, event.result);
response.requestId = event.token.requestId; response.requestId = event.token.requestId;
var successEventType:String = event.token.completionEventType != null ? event.token.completionEventType : ApiClientEvent.SUCCESS_EVENT; var successEventType:String = event.token.completionEventType != null ? event.token.completionEventType : ApiClientEvent.SUCCESS_EVENT;
@ -252,4 +273,4 @@ public class ApiInvoker extends EventDispatcher {
} }
} }

View File

@ -31,7 +31,7 @@ internal class ApiUrlHelper {
} }
internal static function getProxyUrl(hostName: String, proxyPath: String): String{ internal static function getProxyUrl(hostName: String, proxyPath: String): String{
if (hostName(hostName.length - 1) == "/") //remove trailing slash if (hostName..charAt(hostName.length) == "/") //remove trailing slash
{ {
hostName = hostName.substring(0, hostName.length - 1); hostName = hostName.substring(0, hostName.length - 1);
} }

View File

@ -0,0 +1,65 @@
README for the Flash application.
These instructions are given using the version 4.7 of Flash Builder.
How to use the Flash client library:
The code which is given to you is to be considered as a Library. Therefore, in Flash Builder, you have to:
1. Create an ActionScript Library Project. (Warning: If you are dealing with Files as parameter, you need to include the Adobe AIR libraries)
2. Copy the src folder from the flash library to the project.
3. Copy the lib folder.
4. Copy the build.properties and build.xml. (You can update the paths in build.properties)
5. Right+Click on the project folder and go to Properties. Then go to the tab 'ActionScript Library Build Path' and add these two SWC: 'as3corelib.swc' and 'ASAXB-0.1.1.swc'. (They are located in the folder lib)
6. Create the Flash Application
How to use the Flash client library in an application:
Using the Flash client library (especially getting the response message from the server) is a bit tricky.
The response message is given through an EventDispatcher. Therefore, you have to create an EventDispatcher and listen to the endpoints that you're calling. Below, you will find a pseudo-code explaining how to do this.
After creating an application in java, you will have a mxml file. In this mxml file, you need to declare a Script (given below) after the Declarations.
<fx:Script>
<![CDATA[
// Import the different libraries you need
// ...
// Import the io.swagger libraries
import io.swagger.common.ApiUserCredentials;
import io.swagger.event.ApiClientEvent;
import io.swagger.client.api.[[API]]; // Put your Api here.
// Variables
private var cred:ApiUserCredentials;
private var dispatcher:EventDispatcher;
private var myAPI:[[API]]; // Change [[API]] to be the real name
// Function that print in the console the message of the response
public function onEvent(event:ApiClientEvent):void {
// trace only prints when in debug mod. Be carefule about that.
trace(event.response.payload);
}
// Main function
public main():void {
// Define a dispatcher
dispatcher = new EventDispatcher();
// Define the EventListener. Fill the [[function]] by the name of the function you have in
// the API file.
dispatcher.addEventListener([[function]], onEvent);
// To create the API, you need the ApiUserCredentials and the dispatcher.
cred = new ApiUserCredentials([[host]], [[basePath]] , [[apiToken]]);
// You can add more parameters. Go see the function in ApiUserCredential.as in io/swagger/common
// Create the API
myAPI = new [[API]](cred, dispatcher); // change [[API]] to be the real name
// Now, you can use the API
myAPI.[[function]]([[PARAMETERS]]);
// Change [[function]] and add your parameters. Then, you will see the response in the console.
}
]]>
</fx:Script>

View File

@ -34,7 +34,7 @@ public class {{classname}} extends SwaggerApi {
/* /*
* Returns {{#returnType}}{{{returnType}}} {{/returnType}}{{^returnType}}void {{/returnType}} * Returns {{#returnType}}{{{returnType}}} {{/returnType}}{{^returnType}}void {{/returnType}}
*/ */
public function {{nickname}} ({{#allParams}}{{paramName}}: {{{dataType}}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}{{#hasMore}}, {{/hasMore}}{{/allParams}}): String { public function {{nickname}} ({{#allParams}}{{paramName}}: {{{dataType}}}{{#hasMore}}, {{/hasMore}}{{/allParams}}): String {
// create path and map variables // create path and map variables
var path: String = "{{path}}".replace(/{format}/g,"xml"){{#pathParams}}.replace("{" + "{{paramName}}" + "}", getApiInvoker().escapeString({{{paramName}}})){{/pathParams}}; var path: String = "{{path}}".replace(/{format}/g,"xml"){{#pathParams}}.replace("{" + "{{paramName}}" + "}", getApiInvoker().escapeString({{{paramName}}})){{/pathParams}};