diff --git a/bin/android-java-petstore.sh b/bin/android-java-petstore.sh new file mode 100755 index 0000000000..85e3be72d2 --- /dev/null +++ b/bin/android-java-petstore.sh @@ -0,0 +1,35 @@ +#!/bin/sh + +SCRIPT="$0" + +while [ -h "$SCRIPT" ] ; do + ls=`ls -ld "$SCRIPT"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + SCRIPT="$link" + else + SCRIPT=`dirname "$SCRIPT"`/"$link" + fi +done + +if [ ! -d "${APP_DIR}" ]; then + APP_DIR=`dirname "$SCRIPT"`/.. + APP_DIR=`cd "${APP_DIR}"; pwd` +fi + +cd $APP_DIR + + +# if you've executed sbt assembly previously it will use that instead. +export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties" +ags="$@ samples/client/petstore/android-java/AndroidJavaPetstoreCodegen.scala http://petstore.swagger.wordnik.com/api/api-docs special-key" + +if [ -f $APP_DIR/target/scala-2.9.1/swagger-codegen.jar ]; then + scala -cp target/scala-2.9.1/swagger-codegen.jar $ags +elif [[ -f $APP_DIR/target/scala-2.10/swagger-codegen.jar ]]; then + scala -cp target/scala-2.10/swagger-codegen.jar $ags +else + ./sbt assembly + scala -cp target/swagger-codegen.jar $ags +fi + diff --git a/build.sbt b/build.sbt index 7eac80918d..172e5c891a 100644 --- a/build.sbt +++ b/build.sbt @@ -5,7 +5,7 @@ organization := "com.wordnik" name := "swagger-codegen" -version := "2.0.9-SNAPSHOT" +version := "2.0.10-SNAPSHOT" scalaVersion := "2.9.1" @@ -55,13 +55,6 @@ publishTo <<= (version) { version: String => Some("Sonatype Nexus Releases" at "https://oss.sonatype.org/service/local/staging/deploy/maven2") } -publishTo <<= (version) { version: String => - val artifactory = "https://ci.aws.wordnik.com/artifactory/m2-" - if (version.trim.endsWith("SNAPSHOT")) - Some("snapshots" at artifactory + "snapshots") - else - Some("releases" at artifactory + "releases") -} //publishTo := Some(Resolver.file("file", new File(Path.userHome.absolutePath+"/.m2/repository"))) diff --git a/samples/client/petstore/android-java/AndroidJavaPetstoreCodegen.scala b/samples/client/petstore/android-java/AndroidJavaPetstoreCodegen.scala new file mode 100644 index 0000000000..2be7f229b8 --- /dev/null +++ b/samples/client/petstore/android-java/AndroidJavaPetstoreCodegen.scala @@ -0,0 +1,49 @@ +/** + * Copyright 2013 Wordnik, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import com.wordnik.swagger.codegen.BasicJavaGenerator + +object AndroidJavaPetstoreCodegen extends BasicJavaGenerator { + def main(args: Array[String]) = generateClient(args) + + // location of templates + override def templateDir = "android-java" + + // where to write generated code + override def destinationDir = "samples/client/petstore/android-java/src/main/java" + + // package for api invoker, error files + override def invokerPackage = Some("com.wordnik.client") + + // package for models + override def modelPackage = Some("com.wordnik.petstore.model") + + // package for api classes + override def apiPackage = Some("com.wordnik.petstore.api") + + additionalParams ++= Map( + "artifactId" -> "swagger-petstore", + "artifactVersion" -> "1.0.0", + "groupId" -> "com.wordnik") + + // supporting classes + override def supportingFiles = List( + ("apiInvoker.mustache", destinationDir + java.io.File.separator + invokerPackage.get.replace(".", java.io.File.separator) + java.io.File.separator, "ApiInvoker.java"), + ("jsonUtil.mustache", destinationDir + java.io.File.separator + invokerPackage.get.replace(".", java.io.File.separator) + java.io.File.separator, "JsonUtil.java"), + ("apiException.mustache", destinationDir + java.io.File.separator + invokerPackage.get.replace(".", java.io.File.separator) + java.io.File.separator, "ApiException.java"), + ("pom.mustache", "samples/client/petstore/android-java", "pom.xml") + ) +} diff --git a/samples/client/petstore/android-java/pom.xml b/samples/client/petstore/android-java/pom.xml new file mode 100644 index 0000000000..c1ec3e80b8 --- /dev/null +++ b/samples/client/petstore/android-java/pom.xml @@ -0,0 +1,214 @@ + + 4.0.0 + com.wordnik + swagger-petstore + jar + swagger-petstore + 1.0.0 + + scm:git:git@github.com:wordnik/swagger-mustache.git + scm:git:git@github.com:wordnik/swagger-codegen.git + https://github.com/wordnik/swagger-codegen + + + 2.2.0 + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.12 + + + + loggerPath + conf/log4j.properties + + + -Xms512m -Xmx1500m + methods + pertest + + + + maven-dependency-plugin + + + package + + copy-dependencies + + + ${project.build.directory}/lib + + + + + + + + org.apache.maven.plugins + maven-jar-plugin + 2.2 + + + + jar + test-jar + + + + + + + + + org.codehaus.mojo + build-helper-maven-plugin + + + add_sources + generate-sources + + add-source + + + + src/main/java + + + + + add_test_sources + generate-test-sources + + add-test-source + + + + src/test/java + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + 1.6 + 1.6 + + + + net.alchim31.maven + scala-maven-plugin + ${scala-maven-plugin-version} + + + + compile + testCompile + + + + + + incremental + + + -Xmx384m + + + -target:jvm-1.5 + -deprecation + + + + run-scalatest + org.scalatest.tools.Runner + + -p + ${project.build.testOutputDirectory} + + + -Xmx512m + + + + + + + + + + + net.alchim31.maven + scala-maven-plugin + ${scala-maven-plugin-version} + + + + + + com.fasterxml.jackson.core + jackson-core + ${jackson-version} + compile + + + com.fasterxml.jackson.core + jackson-annotations + ${jackson-version} + compile + + + com.fasterxml.jackson.core + jackson-databind + ${jackson-version} + compile + + + org.apache.httpcomponents + httpclient + ${httpclient-version} + compile + + + + + org.scala-lang + scala-library + ${scala-version} + test + + + org.scalatest + scalatest_2.9.1 + ${scala-test-version} + test + + + junit + junit + ${junit-version} + test + + + + + 2.1.4 + 2.9.1-1 + 4.8.1 + 1.0.0 + 4.8.1 + 1.6.1 + 4.2.3 + 3.1.5 + + + diff --git a/samples/client/petstore/android-java/src/main/java/com/wordnik/client/ApiException.java b/samples/client/petstore/android-java/src/main/java/com/wordnik/client/ApiException.java new file mode 100644 index 0000000000..a0131b5cf8 --- /dev/null +++ b/samples/client/petstore/android-java/src/main/java/com/wordnik/client/ApiException.java @@ -0,0 +1,29 @@ +package com.wordnik.client; + +public class ApiException extends Exception { + int code = 0; + String message = null; + + public ApiException() {} + + public ApiException(int code, String message) { + this.code = code; + this.message = message; + } + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/samples/client/petstore/android-java/src/main/java/com/wordnik/client/ApiInvoker.java b/samples/client/petstore/android-java/src/main/java/com/wordnik/client/ApiInvoker.java new file mode 100644 index 0000000000..86a7dccff6 --- /dev/null +++ b/samples/client/petstore/android-java/src/main/java/com/wordnik/client/ApiInvoker.java @@ -0,0 +1,174 @@ +package com.wordnik.client; + + +import com.fasterxml.jackson.core.JsonGenerator.Feature; +import com.fasterxml.jackson.databind.*; +import com.fasterxml.jackson.annotation.*; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; + +import org.apache.http.*; +import org.apache.http.client.*; +import org.apache.http.client.methods.*; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.*; +import org.apache.http.util.EntityUtils; + +import java.io.File; +import java.net.URLEncoder; + +import java.util.Map; +import java.util.HashMap; +import java.util.List; +import java.io.IOException; +import java.io.UnsupportedEncodingException; + +public class ApiInvoker { + private static ApiInvoker INSTANCE = new ApiInvoker(); + private Map defaultHeaderMap = new HashMap(); + + private HttpClient client = null; + public static ApiInvoker getInstance() { + return INSTANCE; + } + + public void addDefaultHeader(String key, String value) { + defaultHeaderMap.put(key, value); + } + + public String escapeString(String str) { + return str; + } + + public static Object deserialize(String json, String containerType, Class cls) throws ApiException { + try{ + if("List".equals(containerType)) { + JavaType typeInfo = JsonUtil.getJsonMapper().getTypeFactory().constructCollectionType(List.class, cls); + List response = (List) JsonUtil.getJsonMapper().readValue(json, typeInfo); + return response; + } + else if(String.class.equals(cls)) { + if(json != null && json.startsWith("\"") && json.endsWith("\"") && json.length() > 1) + return json.substring(1, json.length() - 2); + else + return json; + } + else { + return JsonUtil.getJsonMapper().readValue(json, cls); + } + } + catch (IOException e) { + throw new ApiException(500, e.getMessage()); + } + } + + public static String serialize(Object obj) throws ApiException { + try { + if (obj != null) + return JsonUtil.getJsonMapper().writeValueAsString(obj); + else + return null; + } + catch (Exception e) { + throw new ApiException(500, e.getMessage()); + } + } + + public String invokeAPI(String host, String path, String method, Map queryParams, Object body, Map headerParams, String contentType) throws ApiException { + HttpClient client = getClient(host); + + StringBuilder b = new StringBuilder(); + for(String key : queryParams.keySet()) { + String value = queryParams.get(key); + if (value != null){ + if(b.toString().length() == 0) + b.append("?"); + else + b.append("&"); + b.append(escapeString(key)).append("=").append(escapeString(value)); + } + } + String url = host + path + b.toString(); + + HashMap headers = new HashMap(); + + for(String key : headerParams.keySet()) { + headers.put(key, headerParams.get(key)); + } + + for(String key : defaultHeaderMap.keySet()) { + if(!headerParams.containsKey(key)) { + headers.put(key, defaultHeaderMap.get(key)); + } + } + headers.put("Accept", "application/json"); + + HttpResponse response = null; + try{ + if("GET".equals(method)) { + HttpGet get = new HttpGet(url); + get.addHeader("Accept", "application/json"); + for(String key : headers.keySet()) { + get.setHeader(key, headers.get(key)); + } + response = client.execute(get); + } + else if ("POST".equals(method)) { + HttpPost post = new HttpPost(url); + post.setHeader("Content-Type", contentType); + post.setEntity(new StringEntity(serialize(body), "UTF-8")); + for(String key : headers.keySet()) { + post.setHeader(key, headers.get(key)); + } + response = client.execute(post); + } + else if ("PUT".equals(method)) { + HttpPut put = new HttpPut(url); + if(body != null) { + put.setHeader("Content-Type", contentType); + put.setEntity(new StringEntity(serialize(body), "UTF-8")); + } + for(String key : headers.keySet()) { + put.setHeader(key, headers.get(key)); + } + response = client.execute(put); + } + else if ("DELETE".equals(method)) { + HttpDelete delete = new HttpDelete(url); + for(String key : headers.keySet()) { + delete.setHeader(key, headers.get(key)); + } + response = client.execute(delete); + } + + int code = response.getStatusLine().getStatusCode(); + String responseString = null; + if(code == 204) + responseString = ""; + else if(code >= 200 && code < 300) { + if(response.getEntity() != null) { + HttpEntity resEntity = response.getEntity(); + responseString = EntityUtils.toString(resEntity); + } + } + else { + if(response.getEntity() != null) { + HttpEntity resEntity = response.getEntity(); + responseString = EntityUtils.toString(resEntity); + } + else + responseString = "no data"; + throw new ApiException(code, responseString); + } + return responseString; + } + catch(IOException e) { + throw new ApiException(500, e.getMessage()); + } + } + + private HttpClient getClient(String host) { + if(client == null) + client = new DefaultHttpClient(); + return client; + } +} diff --git a/samples/client/petstore/android-java/src/main/java/com/wordnik/client/JsonUtil.java b/samples/client/petstore/android-java/src/main/java/com/wordnik/client/JsonUtil.java new file mode 100644 index 0000000000..3d96fa3ac7 --- /dev/null +++ b/samples/client/petstore/android-java/src/main/java/com/wordnik/client/JsonUtil.java @@ -0,0 +1,21 @@ +package com.wordnik.client; + +import com.fasterxml.jackson.annotation.*; +import com.fasterxml.jackson.databind.*; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.fasterxml.jackson.core.JsonGenerator.Feature; + +public class JsonUtil { + public static ObjectMapper mapper; + + static { + mapper = new ObjectMapper(); + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); + } + + public static ObjectMapper getJsonMapper() { + return mapper; + } +} + diff --git a/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/api/PetApi.java b/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/api/PetApi.java new file mode 100644 index 0000000000..c620cd4259 --- /dev/null +++ b/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/api/PetApi.java @@ -0,0 +1,309 @@ +package com.wordnik.petstore.api; + +import com.wordnik.client.ApiException; +import com.wordnik.client.ApiInvoker; +import java.io.File; +import com.wordnik.petstore.model.Pet; +import java.util.*; + +public class PetApi { + String basePath = "http://petstore.swagger.wordnik.com/api"; + ApiInvoker apiInvoker = ApiInvoker.getInstance(); + + public void addHeader(String key, String value) { + getInvoker().addDefaultHeader(key, value); + } + + public ApiInvoker getInvoker() { + return apiInvoker; + } + + public void setBasePath(String basePath) { + this.basePath = basePath; + } + + public String getBasePath() { + return basePath; + } + + public Pet getPetById (Long petId) throws ApiException { + // create path and map variables + String path = "/pet/{petId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "petId" + "\\}", apiInvoker.escapeString(petId.toString())); + + // query params + Map queryParams = new HashMap(); + Map headerParams = new HashMap(); + + // verify required params are set + if(petId == null ) { + throw new ApiException(400, "missing required params"); + } + String contentType = "application/json"; + + try { + String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, contentType); + if(response != null){ + return (Pet) ApiInvoker.deserialize(response, "", Pet.class); + } + else { + return null; + } + } catch (ApiException ex) { + if(ex.getCode() == 404) { + return null; + } + else { + throw ex; + } + } + } + public void deletePet (String petId) throws ApiException { + // create path and map variables + String path = "/pet/{petId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "petId" + "\\}", apiInvoker.escapeString(petId.toString())); + + // query params + Map queryParams = new HashMap(); + Map headerParams = new HashMap(); + + // verify required params are set + if(petId == null ) { + throw new ApiException(400, "missing required params"); + } + String contentType = "application/json"; + + try { + String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, null, headerParams, contentType); + if(response != null){ + return ; + } + else { + return ; + } + } catch (ApiException ex) { + if(ex.getCode() == 404) { + return ; + } + else { + throw ex; + } + } + } + public List partialUpdate (String petId, Pet body) throws ApiException { + // create path and map variables + String path = "/pet/{petId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "petId" + "\\}", apiInvoker.escapeString(petId.toString())); + + // query params + Map queryParams = new HashMap(); + Map headerParams = new HashMap(); + + // verify required params are set + if(petId == null || body == null ) { + throw new ApiException(400, "missing required params"); + } + String contentType = "application/json"; + + try { + String response = apiInvoker.invokeAPI(basePath, path, "PATCH", queryParams, body, headerParams, contentType); + if(response != null){ + return (List) ApiInvoker.deserialize(response, "Array", Pet.class); + } + else { + return null; + } + } catch (ApiException ex) { + if(ex.getCode() == 404) { + return null; + } + else { + throw ex; + } + } + } + public void updatePetWithForm (String petId, String name, String status) throws ApiException { + // create path and map variables + String path = "/pet/{petId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "petId" + "\\}", apiInvoker.escapeString(petId.toString())); + + // query params + Map queryParams = new HashMap(); + Map headerParams = new HashMap(); + + // verify required params are set + if(petId == null ) { + throw new ApiException(400, "missing required params"); + } + String contentType = "application/json"; + + try { + String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, null, headerParams, contentType); + if(response != null){ + return ; + } + else { + return ; + } + } catch (ApiException ex) { + if(ex.getCode() == 404) { + return ; + } + else { + throw ex; + } + } + } + public void uploadFile (String additionalMetadata, File body) throws ApiException { + // create path and map variables + String path = "/pet/uploadImage".replaceAll("\\{format\\}","json"); + + // query params + Map queryParams = new HashMap(); + Map headerParams = new HashMap(); + + String contentType = "application/json"; + + try { + String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams, contentType); + if(response != null){ + return ; + } + else { + return ; + } + } catch (ApiException ex) { + if(ex.getCode() == 404) { + return ; + } + else { + throw ex; + } + } + } + public void addPet (Pet body) throws ApiException { + // create path and map variables + String path = "/pet".replaceAll("\\{format\\}","json"); + + // query params + Map queryParams = new HashMap(); + Map headerParams = new HashMap(); + + // verify required params are set + if(body == null ) { + throw new ApiException(400, "missing required params"); + } + String contentType = "application/json"; + + try { + String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams, contentType); + if(response != null){ + return ; + } + else { + return ; + } + } catch (ApiException ex) { + if(ex.getCode() == 404) { + return ; + } + else { + throw ex; + } + } + } + public void updatePet (Pet body) throws ApiException { + // create path and map variables + String path = "/pet".replaceAll("\\{format\\}","json"); + + // query params + Map queryParams = new HashMap(); + Map headerParams = new HashMap(); + + // verify required params are set + if(body == null ) { + throw new ApiException(400, "missing required params"); + } + String contentType = "application/json"; + + try { + String response = apiInvoker.invokeAPI(basePath, path, "PUT", queryParams, body, headerParams, contentType); + if(response != null){ + return ; + } + else { + return ; + } + } catch (ApiException ex) { + if(ex.getCode() == 404) { + return ; + } + else { + throw ex; + } + } + } + public List findPetsByStatus (String status) throws ApiException { + // create path and map variables + String path = "/pet/findByStatus".replaceAll("\\{format\\}","json"); + + // query params + Map queryParams = new HashMap(); + Map headerParams = new HashMap(); + + // verify required params are set + if(status == null ) { + throw new ApiException(400, "missing required params"); + } + if(!"null".equals(String.valueOf(status))) + queryParams.put("status", String.valueOf(status)); + String contentType = "application/json"; + + try { + String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, contentType); + if(response != null){ + return (List) ApiInvoker.deserialize(response, "Array", Pet.class); + } + else { + return null; + } + } catch (ApiException ex) { + if(ex.getCode() == 404) { + return null; + } + else { + throw ex; + } + } + } + public List findPetsByTags (String tags) throws ApiException { + // create path and map variables + String path = "/pet/findByTags".replaceAll("\\{format\\}","json"); + + // query params + Map queryParams = new HashMap(); + Map headerParams = new HashMap(); + + // verify required params are set + if(tags == null ) { + throw new ApiException(400, "missing required params"); + } + if(!"null".equals(String.valueOf(tags))) + queryParams.put("tags", String.valueOf(tags)); + String contentType = "application/json"; + + try { + String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, contentType); + if(response != null){ + return (List) ApiInvoker.deserialize(response, "Array", Pet.class); + } + else { + return null; + } + } catch (ApiException ex) { + if(ex.getCode() == 404) { + return null; + } + else { + throw ex; + } + } + } + } + diff --git a/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/api/StoreApi.java b/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/api/StoreApi.java new file mode 100644 index 0000000000..ea2fd61381 --- /dev/null +++ b/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/api/StoreApi.java @@ -0,0 +1,122 @@ +package com.wordnik.petstore.api; + +import com.wordnik.client.ApiException; +import com.wordnik.client.ApiInvoker; +import com.wordnik.petstore.model.Order; +import java.util.*; + +public class StoreApi { + String basePath = "http://petstore.swagger.wordnik.com/api"; + ApiInvoker apiInvoker = ApiInvoker.getInstance(); + + public void addHeader(String key, String value) { + getInvoker().addDefaultHeader(key, value); + } + + public ApiInvoker getInvoker() { + return apiInvoker; + } + + public void setBasePath(String basePath) { + this.basePath = basePath; + } + + public String getBasePath() { + return basePath; + } + + public Order getOrderById (String orderId) throws ApiException { + // create path and map variables + String path = "/store/order/{orderId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "orderId" + "\\}", apiInvoker.escapeString(orderId.toString())); + + // query params + Map queryParams = new HashMap(); + Map headerParams = new HashMap(); + + // verify required params are set + if(orderId == null ) { + throw new ApiException(400, "missing required params"); + } + String contentType = "application/json"; + + try { + String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, contentType); + if(response != null){ + return (Order) ApiInvoker.deserialize(response, "", Order.class); + } + else { + return null; + } + } catch (ApiException ex) { + if(ex.getCode() == 404) { + return null; + } + else { + throw ex; + } + } + } + public void deleteOrder (String orderId) throws ApiException { + // create path and map variables + String path = "/store/order/{orderId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "orderId" + "\\}", apiInvoker.escapeString(orderId.toString())); + + // query params + Map queryParams = new HashMap(); + Map headerParams = new HashMap(); + + // verify required params are set + if(orderId == null ) { + throw new ApiException(400, "missing required params"); + } + String contentType = "application/json"; + + try { + String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, null, headerParams, contentType); + if(response != null){ + return ; + } + else { + return ; + } + } catch (ApiException ex) { + if(ex.getCode() == 404) { + return ; + } + else { + throw ex; + } + } + } + public void placeOrder (Order body) throws ApiException { + // create path and map variables + String path = "/store/order".replaceAll("\\{format\\}","json"); + + // query params + Map queryParams = new HashMap(); + Map headerParams = new HashMap(); + + // verify required params are set + if(body == null ) { + throw new ApiException(400, "missing required params"); + } + String contentType = "application/json"; + + try { + String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams, contentType); + if(response != null){ + return ; + } + else { + return ; + } + } catch (ApiException ex) { + if(ex.getCode() == 404) { + return ; + } + else { + throw ex; + } + } + } + } + diff --git a/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/api/UserApi.java b/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/api/UserApi.java new file mode 100644 index 0000000000..505f1245ce --- /dev/null +++ b/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/api/UserApi.java @@ -0,0 +1,277 @@ +package com.wordnik.petstore.api; + +import com.wordnik.client.ApiException; +import com.wordnik.client.ApiInvoker; +import com.wordnik.petstore.model.User; +import java.util.*; + +public class UserApi { + String basePath = "http://petstore.swagger.wordnik.com/api"; + ApiInvoker apiInvoker = ApiInvoker.getInstance(); + + public void addHeader(String key, String value) { + getInvoker().addDefaultHeader(key, value); + } + + public ApiInvoker getInvoker() { + return apiInvoker; + } + + public void setBasePath(String basePath) { + this.basePath = basePath; + } + + public String getBasePath() { + return basePath; + } + + public void createUser (User body) throws ApiException { + // create path and map variables + String path = "/user".replaceAll("\\{format\\}","json"); + + // query params + Map queryParams = new HashMap(); + Map headerParams = new HashMap(); + + // verify required params are set + if(body == null ) { + throw new ApiException(400, "missing required params"); + } + String contentType = "application/json"; + + try { + String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams, contentType); + if(response != null){ + return ; + } + else { + return ; + } + } catch (ApiException ex) { + if(ex.getCode() == 404) { + return ; + } + else { + throw ex; + } + } + } + public void createUsersWithArrayInput (List body) throws ApiException { + // create path and map variables + String path = "/user/createWithArray".replaceAll("\\{format\\}","json"); + + // query params + Map queryParams = new HashMap(); + Map headerParams = new HashMap(); + + // verify required params are set + if(body == null ) { + throw new ApiException(400, "missing required params"); + } + String contentType = "application/json"; + + try { + String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams, contentType); + if(response != null){ + return ; + } + else { + return ; + } + } catch (ApiException ex) { + if(ex.getCode() == 404) { + return ; + } + else { + throw ex; + } + } + } + public void createUsersWithListInput (List body) throws ApiException { + // create path and map variables + String path = "/user/createWithList".replaceAll("\\{format\\}","json"); + + // query params + Map queryParams = new HashMap(); + Map headerParams = new HashMap(); + + // verify required params are set + if(body == null ) { + throw new ApiException(400, "missing required params"); + } + String contentType = "application/json"; + + try { + String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams, contentType); + if(response != null){ + return ; + } + else { + return ; + } + } catch (ApiException ex) { + if(ex.getCode() == 404) { + return ; + } + else { + throw ex; + } + } + } + public void updateUser (String username, User body) throws ApiException { + // create path and map variables + String path = "/user/{username}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "username" + "\\}", apiInvoker.escapeString(username.toString())); + + // query params + Map queryParams = new HashMap(); + Map headerParams = new HashMap(); + + // verify required params are set + if(username == null || body == null ) { + throw new ApiException(400, "missing required params"); + } + String contentType = "application/json"; + + try { + String response = apiInvoker.invokeAPI(basePath, path, "PUT", queryParams, body, headerParams, contentType); + if(response != null){ + return ; + } + else { + return ; + } + } catch (ApiException ex) { + if(ex.getCode() == 404) { + return ; + } + else { + throw ex; + } + } + } + public void deleteUser (String username) throws ApiException { + // create path and map variables + String path = "/user/{username}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "username" + "\\}", apiInvoker.escapeString(username.toString())); + + // query params + Map queryParams = new HashMap(); + Map headerParams = new HashMap(); + + // verify required params are set + if(username == null ) { + throw new ApiException(400, "missing required params"); + } + String contentType = "application/json"; + + try { + String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, null, headerParams, contentType); + if(response != null){ + return ; + } + else { + return ; + } + } catch (ApiException ex) { + if(ex.getCode() == 404) { + return ; + } + else { + throw ex; + } + } + } + public User getUserByName (String username) throws ApiException { + // create path and map variables + String path = "/user/{username}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "username" + "\\}", apiInvoker.escapeString(username.toString())); + + // query params + Map queryParams = new HashMap(); + Map headerParams = new HashMap(); + + // verify required params are set + if(username == null ) { + throw new ApiException(400, "missing required params"); + } + String contentType = "application/json"; + + try { + String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, contentType); + if(response != null){ + return (User) ApiInvoker.deserialize(response, "", User.class); + } + else { + return null; + } + } catch (ApiException ex) { + if(ex.getCode() == 404) { + return null; + } + else { + throw ex; + } + } + } + public String loginUser (String username, String password) throws ApiException { + // create path and map variables + String path = "/user/login".replaceAll("\\{format\\}","json"); + + // query params + Map queryParams = new HashMap(); + Map headerParams = new HashMap(); + + // verify required params are set + if(username == null || password == null ) { + throw new ApiException(400, "missing required params"); + } + if(!"null".equals(String.valueOf(username))) + queryParams.put("username", String.valueOf(username)); + if(!"null".equals(String.valueOf(password))) + queryParams.put("password", String.valueOf(password)); + String contentType = "application/json"; + + try { + String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, contentType); + if(response != null){ + return (String) ApiInvoker.deserialize(response, "", String.class); + } + else { + return null; + } + } catch (ApiException ex) { + if(ex.getCode() == 404) { + return null; + } + else { + throw ex; + } + } + } + public void logoutUser () throws ApiException { + // create path and map variables + String path = "/user/logout".replaceAll("\\{format\\}","json"); + + // query params + Map queryParams = new HashMap(); + Map headerParams = new HashMap(); + + String contentType = "application/json"; + + try { + String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, contentType); + if(response != null){ + return ; + } + else { + return ; + } + } catch (ApiException ex) { + if(ex.getCode() == 404) { + return ; + } + else { + throw ex; + } + } + } + } + diff --git a/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/model/Category.java b/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/model/Category.java new file mode 100644 index 0000000000..95cc380d09 --- /dev/null +++ b/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/model/Category.java @@ -0,0 +1,36 @@ +package com.wordnik.petstore.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class Category { + /* Category unique identifier */ + @JsonProperty("id") + private Long id = null; + /* Name of the category */ + @JsonProperty("name") + private String name = null; + public Long getId() { + return id; + } + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class Category {\n"); + sb.append(" id: ").append(id).append("\n"); + sb.append(" name: ").append(name).append("\n"); + sb.append("}\n"); + return sb.toString(); + } +} + diff --git a/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/model/Order.java b/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/model/Order.java new file mode 100644 index 0000000000..1466954b03 --- /dev/null +++ b/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/model/Order.java @@ -0,0 +1,70 @@ +package com.wordnik.petstore.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.Date; +public class Order { + /* Unique identifier for the order */ + @JsonProperty("id") + private Long id = null; + /* ID of pet being ordered */ + @JsonProperty("petId") + private Long petId = null; + /* Number of pets ordered */ + @JsonProperty("quantity") + private Integer quantity = null; + /* Status of the order */ + @JsonProperty("status") + private String status = null; + /* Date shipped, only if it has been */ + @JsonProperty("shipDate") + private Date shipDate = null; + public Long getId() { + return id; + } + public void setId(Long id) { + this.id = id; + } + + public Long getPetId() { + return petId; + } + public void setPetId(Long petId) { + this.petId = petId; + } + + public Integer getQuantity() { + return quantity; + } + public void setQuantity(Integer quantity) { + this.quantity = quantity; + } + + public String getStatus() { + return status; + } + public void setStatus(String status) { + this.status = status; + } + + public Date getShipDate() { + return shipDate; + } + public void setShipDate(Date shipDate) { + this.shipDate = shipDate; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class Order {\n"); + sb.append(" id: ").append(id).append("\n"); + sb.append(" petId: ").append(petId).append("\n"); + sb.append(" quantity: ").append(quantity).append("\n"); + sb.append(" status: ").append(status).append("\n"); + sb.append(" shipDate: ").append(shipDate).append("\n"); + sb.append("}\n"); + return sb.toString(); + } +} + diff --git a/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/model/Pet.java b/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/model/Pet.java new file mode 100644 index 0000000000..8284abdc20 --- /dev/null +++ b/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/model/Pet.java @@ -0,0 +1,83 @@ +package com.wordnik.petstore.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.*; +import com.wordnik.petstore.model.Category; +import com.wordnik.petstore.model.Tag; +public class Pet { + /* Unique identifier for the Pet */ + @JsonProperty("id") + private Long id = null; + /* Category the pet is in */ + @JsonProperty("category") + private Category category = null; + /* Friendly name of the pet */ + @JsonProperty("name") + private String name = null; + /* Image URLs */ + @JsonProperty("photoUrls") + private List photoUrls = new ArrayList(); + /* Tags assigned to this pet */ + @JsonProperty("tags") + private List tags = new ArrayList(); + /* pet status in the store */ + @JsonProperty("status") + private String status = null; + public Long getId() { + return id; + } + public void setId(Long id) { + this.id = id; + } + + public Category getCategory() { + return category; + } + public void setCategory(Category category) { + this.category = category; + } + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + + public List getPhotoUrls() { + return photoUrls; + } + public void setPhotoUrls(List photoUrls) { + this.photoUrls = photoUrls; + } + + public List getTags() { + return tags; + } + public void setTags(List tags) { + this.tags = tags; + } + + public String getStatus() { + return status; + } + public void setStatus(String status) { + this.status = status; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class Pet {\n"); + sb.append(" id: ").append(id).append("\n"); + sb.append(" category: ").append(category).append("\n"); + sb.append(" name: ").append(name).append("\n"); + sb.append(" photoUrls: ").append(photoUrls).append("\n"); + sb.append(" tags: ").append(tags).append("\n"); + sb.append(" status: ").append(status).append("\n"); + sb.append("}\n"); + return sb.toString(); + } +} + diff --git a/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/model/Tag.java b/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/model/Tag.java new file mode 100644 index 0000000000..e092d8fa6b --- /dev/null +++ b/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/model/Tag.java @@ -0,0 +1,36 @@ +package com.wordnik.petstore.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class Tag { + /* Unique identifier for the tag */ + @JsonProperty("id") + private Long id = null; + /* Friendly name for the tag */ + @JsonProperty("name") + private String name = null; + public Long getId() { + return id; + } + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class Tag {\n"); + sb.append(" id: ").append(id).append("\n"); + sb.append(" name: ").append(name).append("\n"); + sb.append("}\n"); + return sb.toString(); + } +} + diff --git a/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/model/User.java b/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/model/User.java new file mode 100644 index 0000000000..6213bb6b7c --- /dev/null +++ b/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/model/User.java @@ -0,0 +1,102 @@ +package com.wordnik.petstore.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class User { + /* Unique identifier for the user */ + @JsonProperty("id") + private Long id = null; + /* Unique username */ + @JsonProperty("username") + private String username = null; + /* First name of the user */ + @JsonProperty("firstName") + private String firstName = null; + /* Last name of the user */ + @JsonProperty("lastName") + private String lastName = null; + /* Email address of the user */ + @JsonProperty("email") + private String email = null; + /* Password name of the user */ + @JsonProperty("password") + private String password = null; + /* Phone number of the user */ + @JsonProperty("phone") + private String phone = null; + /* User Status */ + @JsonProperty("userStatus") + private Integer userStatus = null; + public Long getId() { + return id; + } + public void setId(Long id) { + this.id = id; + } + + public String getUsername() { + return username; + } + public void setUsername(String username) { + this.username = username; + } + + public String getFirstName() { + return firstName; + } + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public String getEmail() { + return email; + } + public void setEmail(String email) { + this.email = email; + } + + public String getPassword() { + return password; + } + public void setPassword(String password) { + this.password = password; + } + + public String getPhone() { + return phone; + } + public void setPhone(String phone) { + this.phone = phone; + } + + public Integer getUserStatus() { + return userStatus; + } + public void setUserStatus(Integer userStatus) { + this.userStatus = userStatus; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class User {\n"); + sb.append(" id: ").append(id).append("\n"); + sb.append(" username: ").append(username).append("\n"); + sb.append(" firstName: ").append(firstName).append("\n"); + sb.append(" lastName: ").append(lastName).append("\n"); + sb.append(" email: ").append(email).append("\n"); + sb.append(" password: ").append(password).append("\n"); + sb.append(" phone: ").append(phone).append("\n"); + sb.append(" userStatus: ").append(userStatus).append("\n"); + sb.append("}\n"); + return sb.toString(); + } +} + diff --git a/scratch.txt b/scratch.txt deleted file mode 100644 index 81b8bfc277..0000000000 --- a/scratch.txt +++ /dev/null @@ -1,8 +0,0 @@ -import com.wordnik.swagger.client._ -import com.wordnik.samples.petstore._ -import akka.dispatch._ -import akka.util.duration._ - -val cfg = SwaggerConfig("http://petstore.swagger.wordnik.com/api") -val client = new Petstore(cfg) -try { Await.result(client.pet.findPetsByStatus(), 3 seconds) } finally { client.close() } diff --git a/src/main/resources/android-java/api.mustache b/src/main/resources/android-java/api.mustache new file mode 100644 index 0000000000..925e9b9d71 --- /dev/null +++ b/src/main/resources/android-java/api.mustache @@ -0,0 +1,75 @@ +package {{package}}; + +import {{invokerPackage}}.ApiException; +import {{invokerPackage}}.ApiInvoker; +{{#imports}}import {{import}}; +{{/imports}} + +import java.util.*; + +{{#operations}} +public class {{classname}} { + String basePath = "{{basePath}}"; + ApiInvoker apiInvoker = ApiInvoker.getInstance(); + + public void addHeader(String key, String value) { + getInvoker().addDefaultHeader(key, value); + } + + public ApiInvoker getInvoker() { + return apiInvoker; + } + + public void setBasePath(String basePath) { + this.basePath = basePath; + } + + public String getBasePath() { + return basePath; + } + + {{#operation}} + public {{#returnType}}{{{returnType}}} {{/returnType}}{{^returnType}}void {{/returnType}} {{nickname}} ({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) throws ApiException { + // create path and map variables + String path = "{{path}}".replaceAll("\\{format\\}","json"){{#pathParams}}.replaceAll("\\{" + "{{paramName}}" + "\\}", apiInvoker.escapeString({{{paramName}}}.toString())){{/pathParams}}; + + // query params + Map queryParams = new HashMap(); + Map headerParams = new HashMap(); + + {{#requiredParamCount}} + // verify required params are set + if({{/requiredParamCount}}{{#requiredParams}} {{paramName}} == null {{#hasMore}}|| {{/hasMore}}{{/requiredParams}}{{#requiredParamCount}}) { + throw new ApiException(400, "missing required params"); + } + {{/requiredParamCount}} + + {{#queryParams}}if(!"null".equals(String.valueOf({{paramName}}))) + queryParams.put("{{paramName}}", String.valueOf({{paramName}})); + {{/queryParams}} + + {{#headerParams}}headerParams.put("{{paramName}}", {{paramName}}); + {{/headerParams}} + + String contentType = "application/json"; + + try { + String response = apiInvoker.invokeAPI(basePath, path, "{{httpMethod}}", queryParams, {{#bodyParam}}{{bodyParam}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}}, headerParams, contentType); + if(response != null){ + return {{#returnType}}({{{returnType}}}) ApiInvoker.deserialize(response, "{{returnContainer}}", {{returnBaseType}}.class){{/returnType}}; + } + else { + return {{#returnType}}null{{/returnType}}; + } + } catch (ApiException ex) { + if(ex.getCode() == 404) { + return {{#returnType}} null{{/returnType}}; + } + else { + throw ex; + } + } + } + {{/operation}} +} +{{/operations}} \ No newline at end of file diff --git a/src/main/resources/android-java/apiException.mustache b/src/main/resources/android-java/apiException.mustache new file mode 100644 index 0000000000..a6bcba75b7 --- /dev/null +++ b/src/main/resources/android-java/apiException.mustache @@ -0,0 +1,29 @@ +package {{invokerPackage}}; + +public class ApiException extends Exception { + int code = 0; + String message = null; + + public ApiException() {} + + public ApiException(int code, String message) { + this.code = code; + this.message = message; + } + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} \ No newline at end of file diff --git a/src/main/resources/android-java/apiInvoker.mustache b/src/main/resources/android-java/apiInvoker.mustache new file mode 100644 index 0000000000..453adb690d --- /dev/null +++ b/src/main/resources/android-java/apiInvoker.mustache @@ -0,0 +1,174 @@ +package {{invokerPackage}}; + + +import com.fasterxml.jackson.core.JsonGenerator.Feature; +import com.fasterxml.jackson.databind.*; +import com.fasterxml.jackson.annotation.*; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; + +import org.apache.http.*; +import org.apache.http.client.*; +import org.apache.http.client.methods.*; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.*; +import org.apache.http.util.EntityUtils; + +import java.io.File; +import java.net.URLEncoder; + +import java.util.Map; +import java.util.HashMap; +import java.util.List; +import java.io.IOException; +import java.io.UnsupportedEncodingException; + +public class ApiInvoker { + private static ApiInvoker INSTANCE = new ApiInvoker(); + private Map defaultHeaderMap = new HashMap(); + + private HttpClient client = null; + public static ApiInvoker getInstance() { + return INSTANCE; + } + + public void addDefaultHeader(String key, String value) { + defaultHeaderMap.put(key, value); + } + + public String escapeString(String str) { + return str; + } + + public static Object deserialize(String json, String containerType, Class cls) throws ApiException { + try{ + if("List".equals(containerType)) { + JavaType typeInfo = JsonUtil.getJsonMapper().getTypeFactory().constructCollectionType(List.class, cls); + List response = (List) JsonUtil.getJsonMapper().readValue(json, typeInfo); + return response; + } + else if(String.class.equals(cls)) { + if(json != null && json.startsWith("\"") && json.endsWith("\"") && json.length() > 1) + return json.substring(1, json.length() - 2); + else + return json; + } + else { + return JsonUtil.getJsonMapper().readValue(json, cls); + } + } + catch (IOException e) { + throw new ApiException(500, e.getMessage()); + } + } + + public static String serialize(Object obj) throws ApiException { + try { + if (obj != null) + return JsonUtil.getJsonMapper().writeValueAsString(obj); + else + return null; + } + catch (Exception e) { + throw new ApiException(500, e.getMessage()); + } + } + + public String invokeAPI(String host, String path, String method, Map queryParams, Object body, Map headerParams, String contentType) throws ApiException { + HttpClient client = getClient(host); + + StringBuilder b = new StringBuilder(); + for(String key : queryParams.keySet()) { + String value = queryParams.get(key); + if (value != null){ + if(b.toString().length() == 0) + b.append("?"); + else + b.append("&"); + b.append(escapeString(key)).append("=").append(escapeString(value)); + } + } + String url = host + path + b.toString(); + + HashMap headers = new HashMap(); + + for(String key : headerParams.keySet()) { + headers.put(key, headerParams.get(key)); + } + + for(String key : defaultHeaderMap.keySet()) { + if(!headerParams.containsKey(key)) { + headers.put(key, defaultHeaderMap.get(key)); + } + } + headers.put("Accept", "application/json"); + + HttpResponse response = null; + try{ + if("GET".equals(method)) { + HttpGet get = new HttpGet(url); + get.addHeader("Accept", "application/json"); + for(String key : headers.keySet()) { + get.setHeader(key, headers.get(key)); + } + response = client.execute(get); + } + else if ("POST".equals(method)) { + HttpPost post = new HttpPost(url); + post.setHeader("Content-Type", contentType); + post.setEntity(new StringEntity(serialize(body), "UTF-8")); + for(String key : headers.keySet()) { + post.setHeader(key, headers.get(key)); + } + response = client.execute(post); + } + else if ("PUT".equals(method)) { + HttpPut put = new HttpPut(url); + if(body != null) { + put.setHeader("Content-Type", contentType); + put.setEntity(new StringEntity(serialize(body), "UTF-8")); + } + for(String key : headers.keySet()) { + put.setHeader(key, headers.get(key)); + } + response = client.execute(put); + } + else if ("DELETE".equals(method)) { + HttpDelete delete = new HttpDelete(url); + for(String key : headers.keySet()) { + delete.setHeader(key, headers.get(key)); + } + response = client.execute(delete); + } + + int code = response.getStatusLine().getStatusCode(); + String responseString = null; + if(code == 204) + responseString = ""; + else if(code >= 200 && code < 300) { + if(response.getEntity() != null) { + HttpEntity resEntity = response.getEntity(); + responseString = EntityUtils.toString(resEntity); + } + } + else { + if(response.getEntity() != null) { + HttpEntity resEntity = response.getEntity(); + responseString = EntityUtils.toString(resEntity); + } + else + responseString = "no data"; + throw new ApiException(code, responseString); + } + return responseString; + } + catch(IOException e) { + throw new ApiException(500, e.getMessage()); + } + } + + private HttpClient getClient(String host) { + if(client == null) + client = new DefaultHttpClient(); + return client; + } +} \ No newline at end of file diff --git a/src/main/resources/android-java/jsonUtil.mustache b/src/main/resources/android-java/jsonUtil.mustache new file mode 100644 index 0000000000..ef00838c17 --- /dev/null +++ b/src/main/resources/android-java/jsonUtil.mustache @@ -0,0 +1,20 @@ +package {{invokerPackage}}; + +import com.fasterxml.jackson.annotation.*; +import com.fasterxml.jackson.databind.*; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.fasterxml.jackson.core.JsonGenerator.Feature; + +public class JsonUtil { + public static ObjectMapper mapper; + + static { + mapper = new ObjectMapper(); + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); + } + + public static ObjectMapper getJsonMapper() { + return mapper; + } +} diff --git a/src/main/resources/android-java/model.mustache b/src/main/resources/android-java/model.mustache new file mode 100644 index 0000000000..afa4a49575 --- /dev/null +++ b/src/main/resources/android-java/model.mustache @@ -0,0 +1,40 @@ +package {{package}}; + +import com.fasterxml.jackson.annotation.JsonProperty; + +{{#imports}}import {{import}}; +{{/imports}} +{{#models}} +{{#model}} +public class {{classname}} { + {{#vars}} + + {{#description}}/* {{{description}}} */ + {{/description}} + @JsonProperty("{{name}}") + private {{{datatype}}} {{name}} = {{{defaultValue}}}; + {{/vars}} + + {{#vars}} + public {{{datatype}}} {{getter}}() { + return {{name}}; + } + public void {{setter}}({{{datatype}}} {{name}}) { + this.{{name}} = {{name}}; + } + + {{/vars}} + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class {{classname}} {\n"); + {{#vars}} + sb.append(" {{name}}: ").append({{name}}).append("\n"); + {{/vars}} + sb.append("}\n"); + return sb.toString(); + } +} +{{/model}} +{{/models}} \ No newline at end of file diff --git a/src/main/resources/android-java/pom.mustache b/src/main/resources/android-java/pom.mustache new file mode 100644 index 0000000000..5ec4fd76a8 --- /dev/null +++ b/src/main/resources/android-java/pom.mustache @@ -0,0 +1,213 @@ + + 4.0.0 + com.wordnik + {{artifactId}} + jar + {{artifactId}} + 1.0.0 + + scm:git:git@github.com:wordnik/swagger-mustache.git + scm:git:git@github.com:wordnik/swagger-codegen.git + https://github.com/wordnik/swagger-codegen + + + 2.2.0 + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.12 + + + + loggerPath + conf/log4j.properties + + + -Xms512m -Xmx1500m + methods + pertest + + + + maven-dependency-plugin + + + package + + copy-dependencies + + + ${project.build.directory}/lib + + + + + + + + org.apache.maven.plugins + maven-jar-plugin + 2.2 + + + + jar + test-jar + + + + + + + + + org.codehaus.mojo + build-helper-maven-plugin + + + add_sources + generate-sources + + add-source + + + + src/main/java + + + + + add_test_sources + generate-test-sources + + add-test-source + + + + src/test/java + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + 1.6 + 1.6 + + + + net.alchim31.maven + scala-maven-plugin + ${scala-maven-plugin-version} + + + + compile + testCompile + + + + + + incremental + + + -Xmx384m + + + -target:jvm-1.5 + -deprecation + + + + run-scalatest + org.scalatest.tools.Runner + + -p + ${project.build.testOutputDirectory} + + + -Xmx512m + + + + + + + + + + + net.alchim31.maven + scala-maven-plugin + ${scala-maven-plugin-version} + + + + + + com.fasterxml.jackson.core + jackson-core + ${jackson-version} + compile + + + com.fasterxml.jackson.core + jackson-annotations + ${jackson-version} + compile + + + com.fasterxml.jackson.core + jackson-databind + ${jackson-version} + compile + + + org.apache.httpcomponents + httpclient + ${httpclient-version} + compile + + + + + org.scala-lang + scala-library + ${scala-version} + test + + + org.scalatest + scalatest_2.9.1 + ${scala-test-version} + test + + + junit + junit + ${junit-version} + test + + + + + 2.1.4 + 2.9.1-1 + 4.8.1 + 1.0.0 + 4.8.1 + 1.6.1 + 4.2.3 + 3.1.5 + + diff --git a/src/main/scala/com/wordnik/swagger/codegen/BasicAndroidJavaClient.scala b/src/main/scala/com/wordnik/swagger/codegen/BasicAndroidJavaClient.scala new file mode 100644 index 0000000000..e34f3ae535 --- /dev/null +++ b/src/main/scala/com/wordnik/swagger/codegen/BasicAndroidJavaClient.scala @@ -0,0 +1,71 @@ +/** + * Copyright 2012 Wordnik, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import com.wordnik.swagger.codegen.BasicJavaGenerator + +import com.wordnik.swagger.model._ + +object BasicAndroidJavaClient extends BasicJavaGenerator { + def main(args: Array[String]) = generateClient(args) + + override def templateDir = "src/main/resources/android-java" + + override def typeMapping = super.typeMapping ++ Map( + "file" -> "File") + + override def importMapping = super.importMapping ++ Map( + "Set" -> "java.util.Set") + + override def defaultIncludes = Set( + "Integer", + "String", + "Long", + "Short", + "Char", + "Byte", + "Float", + "Double", + "Boolean", + "AnyRef", + "Any") + + additionalParams ++= Map( + "artifactId" -> "swagger-android-client", + "artifactVersion" -> "1.0.0", + "groupId" -> "com.wordnik" + ) + + // package for api invoker, error files + override def invokerPackage = Some("com.wordnik.client") + + // where to write generated code + override def destinationDir = "generated-code/android-java/src/main/java" + + // package for models + override def modelPackage = Some("com.wordnik.client.model") + + // package for api classes + override def apiPackage = Some("com.wordnik.client.api") + + + // supporting classes + override def supportingFiles = List( + ("apiInvoker.mustache", destinationDir + java.io.File.separator + invokerPackage.get.replace(".", java.io.File.separator) + java.io.File.separator, "ApiInvoker.java"), + ("jsonUtil.mustache", destinationDir + java.io.File.separator + invokerPackage.get.replace(".", java.io.File.separator) + java.io.File.separator, "JsonUtil.java"), + ("ApiException.mustache", destinationDir + java.io.File.separator + invokerPackage.get.replace(".", java.io.File.separator) + java.io.File.separator, "ApiException.java"), + ("pom.mustache", "android-java", "pom.xml") + ) +} \ No newline at end of file