From 16d9191c9d0b48db207d16f442640322b5eb2b88 Mon Sep 17 00:00:00 2001 From: clasnake Date: Fri, 17 Feb 2017 00:01:35 +0800 Subject: [PATCH 1/6] Add async support to scala client. --- .../codegen/languages/ScalaClientCodegen.java | 4 + .../main/resources/asyncscala/client.mustache | 1 + .../src/main/resources/scala/api.mustache | 159 +++--- .../main/resources/scala/build.sbt.mustache | 3 +- .../src/main/resources/scala/client.mustache | 22 + samples/client/petstore/scala/build.sbt | 3 +- .../scala/io/swagger/client/AsyncClient.scala | 20 + .../scala/io/swagger/client/api/PetApi.scala | 528 ++++++++++-------- .../io/swagger/client/api/StoreApi.scala | 259 +++++---- .../scala/io/swagger/client/api/UserApi.scala | 518 +++++++++-------- 10 files changed, 868 insertions(+), 649 deletions(-) create mode 100644 modules/swagger-codegen/src/main/resources/scala/client.mustache create mode 100644 samples/client/petstore/scala/src/main/scala/io/swagger/client/AsyncClient.scala diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/ScalaClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/ScalaClientCodegen.java index 2044b80fc80..e3c3873e8be 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/ScalaClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/ScalaClientCodegen.java @@ -20,6 +20,7 @@ public class ScalaClientCodegen extends AbstractScalaCodegen implements CodegenC protected String groupId = "io.swagger"; protected String artifactId = "swagger-scala-client"; protected String artifactVersion = "1.0.0"; + protected String clientName = "AsyncClient"; public ScalaClientCodegen() { super(); @@ -51,10 +52,13 @@ public ScalaClientCodegen() { additionalProperties.put("asyncHttpClient", asyncHttpClient); additionalProperties.put("authScheme", authScheme); additionalProperties.put("authPreemptive", authPreemptive); + additionalProperties.put("clientName", clientName); supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml")); supportingFiles.add(new SupportingFile("apiInvoker.mustache", (sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), "ApiInvoker.scala")); + supportingFiles.add(new SupportingFile("client.mustache", + (sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), clientName + ".scala")); supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh")); supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore")); // gradle settings diff --git a/modules/swagger-codegen/src/main/resources/asyncscala/client.mustache b/modules/swagger-codegen/src/main/resources/asyncscala/client.mustache index d48d031860e..87d830e3e6a 100644 --- a/modules/swagger-codegen/src/main/resources/asyncscala/client.mustache +++ b/modules/swagger-codegen/src/main/resources/asyncscala/client.mustache @@ -23,3 +23,4 @@ class {{clientName}}(config: SwaggerConfig) extends Closeable { client.close() } } + diff --git a/modules/swagger-codegen/src/main/resources/scala/api.mustache b/modules/swagger-codegen/src/main/resources/scala/api.mustache index 177fc96cd13..c85a78be27b 100644 --- a/modules/swagger-codegen/src/main/resources/scala/api.mustache +++ b/modules/swagger-codegen/src/main/resources/scala/api.mustache @@ -5,6 +5,7 @@ package {{package}} {{/imports}} import {{invokerPackage}}.ApiInvoker import {{invokerPackage}}.ApiException +import {{invokerPackage}}.AsyncClient import com.sun.jersey.multipart.FormDataMultiPart import com.sun.jersey.multipart.file.FileDataBodyPart @@ -16,13 +17,39 @@ import java.util.Date import scala.collection.mutable.HashMap +import com.wordnik.swagger.client._ +import scala.concurrent.Future +import collection.mutable + +import java.net.URI + +import com.wordnik.swagger.client.ClientResponseReaders.Json4sFormatsReader._ +import com.wordnik.swagger.client.RequestWriters.Json4sFormatsWriter._ + +import scala.concurrent.ExecutionContext.Implicits.global +import scala.concurrent._ +import scala.concurrent.duration._ +import scala.util.{Failure, Success, Try} + {{#operations}} class {{classname}}(val defBasePath: String = "{{basePath}}", defApiInvoker: ApiInvoker = ApiInvoker) { + + implicit val formats = org.json4s.DefaultFormats + implicit val reader = JsonFormatsReader + implicit val writer = JsonFormatsWriter + var basePath = defBasePath var apiInvoker = defApiInvoker - def addHeader(key: String, value: String) = apiInvoker.defaultHeaders += key -> value + def addHeader(key: String, value: String) = apiInvoker.defaultHeaders += key -> value + + private[this] val client = transportClient + + protected def transportClient: TransportClient = new RestClient(config) + + val config = SwaggerConfig.forUrl(new URI(defBasePath)) + val helper = new {{classname}}AsyncHelper(client, config) {{#operation}} /** @@ -32,97 +59,83 @@ class {{classname}}(val defBasePath: String = "{{basePath}}", {{/allParams}} * @return {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}} */ def {{operationId}}({{#allParams}}{{paramName}}: {{#required}}{{dataType}}{{#defaultValue}} /* = {{{defaultValue}}}*/{{/defaultValue}}{{/required}}{{^required}}Option[{{dataType}}]{{#defaultValue}} /* = {{{defaultValue}}}*/{{/defaultValue}}{{^defaultValue}} = None{{/defaultValue}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}){{#returnType}}: Option[{{returnType}}]{{/returnType}} = { - // create path and map variables - val path = "{{path}}".replaceAll("\\{format\\}", "json"){{#pathParams}}.replaceAll("\\{" + "{{baseName}}" + "\\}",apiInvoker.escape({{paramName}})){{/pathParams}} + val await = Try(Await.result({{operationId}}Async({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}), Duration.Inf)) + await match { + case Success(i) => Some(await.get) + case Failure(t) => None + } - val contentTypes = List({{#consumes}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/consumes}}{{^consumes}}"application/json"{{/consumes}}) - val contentType = contentTypes(0) + } - val queryParams = new HashMap[String, String] - val headerParams = new HashMap[String, String] - val formParams = new HashMap[String, String] + /** + * {{summary}} asynchronously + * {{notes}} + {{#allParams}} * @param {{paramName}} {{description}} {{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}} + {{/allParams}} * @return Future({{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}) + */ + def {{operationId}}Async({{#allParams}}{{paramName}}: {{#required}}{{dataType}}{{#defaultValue}} /* = {{{defaultValue}}}*/{{/defaultValue}}{{/required}}{{^required}}Option[{{dataType}}]{{#defaultValue}} /* = {{{defaultValue}}}*/{{/defaultValue}}{{^defaultValue}} = None{{/defaultValue}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}){{#returnType}}: Future[{{returnType}}]{{/returnType}} = { + helper.{{operationId}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) + } - {{#allParams}} - {{#required}} - {{^isPrimitiveType}} - if ({{paramName}} == null) throw new Exception("Missing required parameter '{{paramName}}' when calling {{classname}}->{{operationId}}") - {{/isPrimitiveType}} - {{#isString}} - if ({{paramName}} == null) throw new Exception("Missing required parameter '{{paramName}}' when calling {{classname}}->{{operationId}}") + {{/operation}} +} - {{/isString}} - {{/required}} - {{/allParams}} - {{#queryParams}} - {{#required}} - queryParams += "{{baseName}}" -> {{paramName}}.toString - {{/required}} - {{^required}} - {{paramName}}.map(paramVal => queryParams += "{{baseName}}" -> paramVal.toString) - {{/required}} - {{/queryParams}} - - {{#headerParams}} - {{#required}} - headerParams += "{{baseName}}" -> {{paramName}} - {{/required}} - {{^required}} - {{paramName}}.map(paramVal => headerParams += "{{baseName}}" -> paramVal) - {{/required}} - {{/headerParams}} +class {{classname}}AsyncHelper(client: TransportClient, config: SwaggerConfig) extends ApiClient(client, config) { - var postBody: AnyRef = {{#bodyParam}}{{#required}}{{paramName}}{{/required}}{{^required}}{{paramName}}.map(paramVal => paramVal){{/required}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}} +{{#operation}} + def {{operationId}}({{#allParams}}{{^required}}{{paramName}}: Option[{{dataType}}] = {{#defaultValue}}Some({{defaultValue}}){{/defaultValue}}{{^defaultValue}}None{{/defaultValue}}{{#hasMore}},{{/hasMore}} + {{/required}}{{#required}}{{paramName}}: {{dataType}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}{{#hasMore}}, + {{/hasMore}}{{/required}}{{/allParams}})(implicit reader: ClientResponseReader[{{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}Unit{{/returnType}}]{{#bodyParams}}, writer: RequestWriter[{{dataType}}]{{/bodyParams}}){{#returnType}}: Future[{{returnType}}]{{/returnType}}{{^returnType}}: Future[Unit]{{/returnType}} = { + // create path and map variables + val path = (addFmt("{{path}}"){{#pathParams}} + replaceAll ("\\{" + "{{baseName}}" + "\\}",{{paramName}}.toString){{/pathParams}}) - if (contentType.startsWith("multipart/form-data")) { - val mp = new FormDataMultiPart - {{#formParams}} - {{#notFile}} + // query params + val queryParams = new mutable.HashMap[String, String] + val headerParams = new mutable.HashMap[String, String] + + {{#allParams}} {{#required}} - mp.field("{{baseName}}", {{paramName}}.toString, MediaType.MULTIPART_FORM_DATA_TYPE) + {{^isPrimitiveType}} + if ({{paramName}} == null) throw new Exception("Missing required parameter '{{paramName}}' when calling {{classname}}->{{operationId}}") + {{/isPrimitiveType}} + {{#isString}} + if ({{paramName}} == null) throw new Exception("Missing required parameter '{{paramName}}' when calling {{classname}}->{{operationId}}") + + {{/isString}} {{/required}} + {{/allParams}} + {{#queryParams}} {{^required}} - {{paramName}}.map(paramVal => mp.field("{{baseName}}", paramVal.toString, MediaType.MULTIPART_FORM_DATA_TYPE)) + {{paramName}} match { + case Some(param) => queryParams += "{{baseName}}" -> param.toString + case _ => queryParams + } {{/required}} - {{/notFile}} - {{#isFile}} {{#required}} - mp.field("{{baseName}}", file.getName) - mp.bodyPart(new FileDataBodyPart("{{baseName}}", {{paramName}}, MediaType.MULTIPART_FORM_DATA_TYPE)) + queryParams += "{{baseName}}" -> {{paramName}}.toString {{/required}} + {{/queryParams}} + {{#headerParams}} {{^required}} - file.map(fileVal => mp.field("{{baseName}}", fileVal.getName)) - {{paramName}}.map(paramVal => mp.bodyPart(new FileDataBodyPart("{{baseName}}", paramVal, MediaType.MULTIPART_FORM_DATA_TYPE))) + {{paramName}} match { + case Some(param) => headerParams += "{{baseName}}" -> param.toString + case _ => headerParams + } {{/required}} - {{/isFile}} - {{/formParams}} - postBody = mp - } else { - {{#formParams}} - {{#notFile}} {{#required}} - formParams += "{{baseName}}" -> {{paramName}}.toString - {{/required}} - {{^required}} - {{paramName}}.map(paramVal => formParams += "{{baseName}}" -> paramVal.toString) + headerParams += "{{baseName}}" -> {{paramName}}.toString {{/required}} - {{/notFile}} - {{/formParams}} - } + {{/headerParams}} - try { - apiInvoker.invokeApi(basePath, path, "{{httpMethod}}", queryParams.toMap, formParams.toMap, postBody, headerParams.toMap, contentType) match { - case s: String => - {{#returnType}} Some(apiInvoker.deserialize(s, "{{returnContainer}}", classOf[{{returnBaseType}}]).asInstanceOf[{{returnType}}]) - {{/returnType}} - case _ => None - } - } catch { - case ex: ApiException if ex.code == 404 => None - case ex: ApiException => throw ex + val resFuture = client.submit("{{httpMethod}}", path, queryParams.toMap, headerParams.toMap, {{#bodyParam}}writer.write({{paramName}}){{/bodyParam}}{{^bodyParam}}"{{emptyBodyParam}}"{{/bodyParam}}) + resFuture flatMap { resp => + process(reader.read(resp)) + } } - } - {{/operation}} +{{/operation}} + } {{/operations}} diff --git a/modules/swagger-codegen/src/main/resources/scala/build.sbt.mustache b/modules/swagger-codegen/src/main/resources/scala/build.sbt.mustache index 6a198acb832..daf4b6ae94d 100644 --- a/modules/swagger-codegen/src/main/resources/scala/build.sbt.mustache +++ b/modules/swagger-codegen/src/main/resources/scala/build.sbt.mustache @@ -15,7 +15,8 @@ lazy val root = (project in file(".")). "joda-time" % "joda-time" % "2.2", "org.joda" % "joda-convert" % "1.2", "org.scalatest" %% "scalatest" % "2.2.4" % "test", - "junit" % "junit" % "4.8.1" % "test" + "junit" % "junit" % "4.8.1" % "test", + "com.wordnik.swagger" %% "swagger-async-httpclient" % "0.3.5" ), resolvers ++= Seq( diff --git a/modules/swagger-codegen/src/main/resources/scala/client.mustache b/modules/swagger-codegen/src/main/resources/scala/client.mustache new file mode 100644 index 00000000000..8098b73c6bb --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/scala/client.mustache @@ -0,0 +1,22 @@ +package {{invokerPackage}} + +{{#imports}}import {{import}} +{{/imports}} +import {{apiPackage}}._ + +import com.wordnik.swagger.client._ + +import java.io.Closeable + +class {{clientName}}(config: SwaggerConfig) extends Closeable { + val locator = config.locator + val name = config.name + + private[this] val client = transportClient + + protected def transportClient: TransportClient = new RestClient(config) + + def close() { + client.close() + } +} diff --git a/samples/client/petstore/scala/build.sbt b/samples/client/petstore/scala/build.sbt index 063b2b0d490..005d7aeca1b 100644 --- a/samples/client/petstore/scala/build.sbt +++ b/samples/client/petstore/scala/build.sbt @@ -15,7 +15,8 @@ lazy val root = (project in file(".")). "joda-time" % "joda-time" % "2.2", "org.joda" % "joda-convert" % "1.2", "org.scalatest" %% "scalatest" % "2.2.4" % "test", - "junit" % "junit" % "4.8.1" % "test" + "junit" % "junit" % "4.8.1" % "test", + "com.wordnik.swagger" %% "swagger-async-httpclient" % "0.3.5" ), resolvers ++= Seq( diff --git a/samples/client/petstore/scala/src/main/scala/io/swagger/client/AsyncClient.scala b/samples/client/petstore/scala/src/main/scala/io/swagger/client/AsyncClient.scala new file mode 100644 index 00000000000..c518277f577 --- /dev/null +++ b/samples/client/petstore/scala/src/main/scala/io/swagger/client/AsyncClient.scala @@ -0,0 +1,20 @@ +package io.swagger.client + +import io.swagger.client.api._ + +import com.wordnik.swagger.client._ + +import java.io.Closeable + +class AsyncClient(config: SwaggerConfig) extends Closeable { + val locator = config.locator + val name = config.name + + private[this] val client = transportClient + + protected def transportClient: TransportClient = new RestClient(config) + + def close() { + client.close() + } +} diff --git a/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/PetApi.scala b/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/PetApi.scala index 3bcadab73c9..ef7401ad51e 100644 --- a/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/PetApi.scala +++ b/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/PetApi.scala @@ -17,6 +17,7 @@ import java.io.File import io.swagger.client.model.Pet import io.swagger.client.ApiInvoker import io.swagger.client.ApiException +import io.swagger.client.AsyncClient import com.sun.jersey.multipart.FormDataMultiPart import com.sun.jersey.multipart.file.FileDataBodyPart @@ -28,12 +29,38 @@ import java.util.Date import scala.collection.mutable.HashMap +import com.wordnik.swagger.client._ +import scala.concurrent.Future +import collection.mutable + +import java.net.URI + +import com.wordnik.swagger.client.ClientResponseReaders.Json4sFormatsReader._ +import com.wordnik.swagger.client.RequestWriters.Json4sFormatsWriter._ + +import scala.concurrent.ExecutionContext.Implicits.global +import scala.concurrent._ +import scala.concurrent.duration._ +import scala.util.{Failure, Success, Try} + class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2", defApiInvoker: ApiInvoker = ApiInvoker) { + + implicit val formats = org.json4s.DefaultFormats + implicit val reader = JsonFormatsReader + implicit val writer = JsonFormatsWriter + var basePath = defBasePath var apiInvoker = defApiInvoker - def addHeader(key: String, value: String) = apiInvoker.defaultHeaders += key -> value + def addHeader(key: String, value: String) = apiInvoker.defaultHeaders += key -> value + + private[this] val client = transportClient + + protected def transportClient: TransportClient = new RestClient(config) + + val config = SwaggerConfig.forUrl(new URI(defBasePath)) + val helper = new PetApiAsyncHelper(client, config) /** * Add a new pet to the store @@ -42,39 +69,25 @@ class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2", * @return void */ def addPet(body: Pet) = { - // create path and map variables - val path = "/pet".replaceAll("\\{format\\}", "json") - - val contentTypes = List("application/json", "application/xml") - val contentType = contentTypes(0) - - val queryParams = new HashMap[String, String] - val headerParams = new HashMap[String, String] - val formParams = new HashMap[String, String] - - if (body == null) throw new Exception("Missing required parameter 'body' when calling PetApi->addPet") - - - - var postBody: AnyRef = body - - if (contentType.startsWith("multipart/form-data")) { - val mp = new FormDataMultiPart - postBody = mp - } else { + val await = Try(Await.result(addPetAsync(body), Duration.Inf)) + await match { + case Success(i) => Some(await.get) + case Failure(t) => None } - try { - apiInvoker.invokeApi(basePath, path, "POST", queryParams.toMap, formParams.toMap, postBody, headerParams.toMap, contentType) match { - case s: String => - case _ => None - } - } catch { - case ex: ApiException if ex.code == 404 => None - case ex: ApiException => throw ex - } } + /** + * Add a new pet to the store asynchronously + * + * @param body Pet object that needs to be added to the store + * @return Future(void) + */ + def addPetAsync(body: Pet) = { + helper.addPet(body) + } + + /** * Deletes a pet * @@ -83,38 +96,26 @@ class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2", * @return void */ def deletePet(petId: Long, apiKey: Option[String] = None) = { - // create path and map variables - val path = "/pet/{petId}".replaceAll("\\{format\\}", "json").replaceAll("\\{" + "petId" + "\\}",apiInvoker.escape(petId)) - - val contentTypes = List("application/json") - val contentType = contentTypes(0) - - val queryParams = new HashMap[String, String] - val headerParams = new HashMap[String, String] - val formParams = new HashMap[String, String] - - - apiKey.map(paramVal => headerParams += "api_key" -> paramVal) - - var postBody: AnyRef = null - - if (contentType.startsWith("multipart/form-data")) { - val mp = new FormDataMultiPart - postBody = mp - } else { + val await = Try(Await.result(deletePetAsync(petId, apiKey), Duration.Inf)) + await match { + case Success(i) => Some(await.get) + case Failure(t) => None } - try { - apiInvoker.invokeApi(basePath, path, "DELETE", queryParams.toMap, formParams.toMap, postBody, headerParams.toMap, contentType) match { - case s: String => - case _ => None - } - } catch { - case ex: ApiException if ex.code == 404 => None - case ex: ApiException => throw ex - } } + /** + * Deletes a pet asynchronously + * + * @param petId Pet id to delete + * @param apiKey (optional) + * @return Future(void) + */ + def deletePetAsync(petId: Long, apiKey: Option[String] = None) = { + helper.deletePet(petId, apiKey) + } + + /** * Finds Pets by status * Multiple status values can be provided with comma separated strings @@ -122,41 +123,25 @@ class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2", * @return List[Pet] */ def findPetsByStatus(status: List[String]): Option[List[Pet]] = { - // create path and map variables - val path = "/pet/findByStatus".replaceAll("\\{format\\}", "json") - - val contentTypes = List("application/json") - val contentType = contentTypes(0) - - val queryParams = new HashMap[String, String] - val headerParams = new HashMap[String, String] - val formParams = new HashMap[String, String] - - if (status == null) throw new Exception("Missing required parameter 'status' when calling PetApi->findPetsByStatus") - - queryParams += "status" -> status.toString - - - var postBody: AnyRef = null - - if (contentType.startsWith("multipart/form-data")) { - val mp = new FormDataMultiPart - postBody = mp - } else { + val await = Try(Await.result(findPetsByStatusAsync(status), Duration.Inf)) + await match { + case Success(i) => Some(await.get) + case Failure(t) => None } - try { - apiInvoker.invokeApi(basePath, path, "GET", queryParams.toMap, formParams.toMap, postBody, headerParams.toMap, contentType) match { - case s: String => - Some(apiInvoker.deserialize(s, "array", classOf[Pet]).asInstanceOf[List[Pet]]) - case _ => None - } - } catch { - case ex: ApiException if ex.code == 404 => None - case ex: ApiException => throw ex - } } + /** + * Finds Pets by status asynchronously + * Multiple status values can be provided with comma separated strings + * @param status Status values that need to be considered for filter + * @return Future(List[Pet]) + */ + def findPetsByStatusAsync(status: List[String]): Future[List[Pet]] = { + helper.findPetsByStatus(status) + } + + /** * Finds Pets by tags * Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. @@ -164,41 +149,25 @@ class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2", * @return List[Pet] */ def findPetsByTags(tags: List[String]): Option[List[Pet]] = { - // create path and map variables - val path = "/pet/findByTags".replaceAll("\\{format\\}", "json") - - val contentTypes = List("application/json") - val contentType = contentTypes(0) - - val queryParams = new HashMap[String, String] - val headerParams = new HashMap[String, String] - val formParams = new HashMap[String, String] - - if (tags == null) throw new Exception("Missing required parameter 'tags' when calling PetApi->findPetsByTags") - - queryParams += "tags" -> tags.toString - - - var postBody: AnyRef = null - - if (contentType.startsWith("multipart/form-data")) { - val mp = new FormDataMultiPart - postBody = mp - } else { + val await = Try(Await.result(findPetsByTagsAsync(tags), Duration.Inf)) + await match { + case Success(i) => Some(await.get) + case Failure(t) => None } - try { - apiInvoker.invokeApi(basePath, path, "GET", queryParams.toMap, formParams.toMap, postBody, headerParams.toMap, contentType) match { - case s: String => - Some(apiInvoker.deserialize(s, "array", classOf[Pet]).asInstanceOf[List[Pet]]) - case _ => None - } - } catch { - case ex: ApiException if ex.code == 404 => None - case ex: ApiException => throw ex - } } + /** + * Finds Pets by tags asynchronously + * Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. + * @param tags Tags to filter by + * @return Future(List[Pet]) + */ + def findPetsByTagsAsync(tags: List[String]): Future[List[Pet]] = { + helper.findPetsByTags(tags) + } + + /** * Find pet by ID * Returns a single pet @@ -206,38 +175,25 @@ class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2", * @return Pet */ def getPetById(petId: Long): Option[Pet] = { - // create path and map variables - val path = "/pet/{petId}".replaceAll("\\{format\\}", "json").replaceAll("\\{" + "petId" + "\\}",apiInvoker.escape(petId)) - - val contentTypes = List("application/json") - val contentType = contentTypes(0) - - val queryParams = new HashMap[String, String] - val headerParams = new HashMap[String, String] - val formParams = new HashMap[String, String] - - - - var postBody: AnyRef = null - - if (contentType.startsWith("multipart/form-data")) { - val mp = new FormDataMultiPart - postBody = mp - } else { + val await = Try(Await.result(getPetByIdAsync(petId), Duration.Inf)) + await match { + case Success(i) => Some(await.get) + case Failure(t) => None } - try { - apiInvoker.invokeApi(basePath, path, "GET", queryParams.toMap, formParams.toMap, postBody, headerParams.toMap, contentType) match { - case s: String => - Some(apiInvoker.deserialize(s, "", classOf[Pet]).asInstanceOf[Pet]) - case _ => None - } - } catch { - case ex: ApiException if ex.code == 404 => None - case ex: ApiException => throw ex - } } + /** + * Find pet by ID asynchronously + * Returns a single pet + * @param petId ID of pet to return + * @return Future(Pet) + */ + def getPetByIdAsync(petId: Long): Future[Pet] = { + helper.getPetById(petId) + } + + /** * Update an existing pet * @@ -245,39 +201,25 @@ class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2", * @return void */ def updatePet(body: Pet) = { - // create path and map variables - val path = "/pet".replaceAll("\\{format\\}", "json") - - val contentTypes = List("application/json", "application/xml") - val contentType = contentTypes(0) - - val queryParams = new HashMap[String, String] - val headerParams = new HashMap[String, String] - val formParams = new HashMap[String, String] - - if (body == null) throw new Exception("Missing required parameter 'body' when calling PetApi->updatePet") - - - - var postBody: AnyRef = body - - if (contentType.startsWith("multipart/form-data")) { - val mp = new FormDataMultiPart - postBody = mp - } else { + val await = Try(Await.result(updatePetAsync(body), Duration.Inf)) + await match { + case Success(i) => Some(await.get) + case Failure(t) => None } - try { - apiInvoker.invokeApi(basePath, path, "PUT", queryParams.toMap, formParams.toMap, postBody, headerParams.toMap, contentType) match { - case s: String => - case _ => None - } - } catch { - case ex: ApiException if ex.code == 404 => None - case ex: ApiException => throw ex - } } + /** + * Update an existing pet asynchronously + * + * @param body Pet object that needs to be added to the store + * @return Future(void) + */ + def updatePetAsync(body: Pet) = { + helper.updatePet(body) + } + + /** * Updates a pet in the store with form data * @@ -287,41 +229,27 @@ class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2", * @return void */ def updatePetWithForm(petId: Long, name: Option[String] = None, status: Option[String] = None) = { - // create path and map variables - val path = "/pet/{petId}".replaceAll("\\{format\\}", "json").replaceAll("\\{" + "petId" + "\\}",apiInvoker.escape(petId)) - - val contentTypes = List("application/x-www-form-urlencoded") - val contentType = contentTypes(0) - - val queryParams = new HashMap[String, String] - val headerParams = new HashMap[String, String] - val formParams = new HashMap[String, String] - - - - var postBody: AnyRef = null - - if (contentType.startsWith("multipart/form-data")) { - val mp = new FormDataMultiPart - name.map(paramVal => mp.field("name", paramVal.toString, MediaType.MULTIPART_FORM_DATA_TYPE)) - status.map(paramVal => mp.field("status", paramVal.toString, MediaType.MULTIPART_FORM_DATA_TYPE)) - postBody = mp - } else { - name.map(paramVal => formParams += "name" -> paramVal.toString) - status.map(paramVal => formParams += "status" -> paramVal.toString) + val await = Try(Await.result(updatePetWithFormAsync(petId, name, status), Duration.Inf)) + await match { + case Success(i) => Some(await.get) + case Failure(t) => None } - try { - apiInvoker.invokeApi(basePath, path, "POST", queryParams.toMap, formParams.toMap, postBody, headerParams.toMap, contentType) match { - case s: String => - case _ => None - } - } catch { - case ex: ApiException if ex.code == 404 => None - case ex: ApiException => throw ex - } } + /** + * Updates a pet in the store with form data asynchronously + * + * @param petId ID of pet that needs to be updated + * @param name Updated name of the pet (optional) + * @param status Updated status of the pet (optional) + * @return Future(void) + */ + def updatePetWithFormAsync(petId: Long, name: Option[String] = None, status: Option[String] = None) = { + helper.updatePetWithForm(petId, name, status) + } + + /** * uploads an image * @@ -331,40 +259,172 @@ class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2", * @return ApiResponse */ def uploadFile(petId: Long, additionalMetadata: Option[String] = None, file: Option[File] = None): Option[ApiResponse] = { + val await = Try(Await.result(uploadFileAsync(petId, additionalMetadata, file), Duration.Inf)) + await match { + case Success(i) => Some(await.get) + case Failure(t) => None + } + + } + + /** + * uploads an image asynchronously + * + * @param petId ID of pet to update + * @param additionalMetadata Additional data to pass to server (optional) + * @param file file to upload (optional) + * @return Future(ApiResponse) + */ + def uploadFileAsync(petId: Long, additionalMetadata: Option[String] = None, file: Option[File] = None): Future[ApiResponse] = { + helper.uploadFile(petId, additionalMetadata, file) + } + + +} + +class PetApiAsyncHelper(client: TransportClient, config: SwaggerConfig) extends ApiClient(client, config) { + + def addPet(body: Pet)(implicit reader: ClientResponseReader[Unit], writer: RequestWriter[Pet]): Future[Unit] = { // create path and map variables - val path = "/pet/{petId}/uploadImage".replaceAll("\\{format\\}", "json").replaceAll("\\{" + "petId" + "\\}",apiInvoker.escape(petId)) + val path = (addFmt("/pet")) + + // query params + val queryParams = new mutable.HashMap[String, String] + val headerParams = new mutable.HashMap[String, String] - val contentTypes = List("multipart/form-data") - val contentType = contentTypes(0) + if (body == null) throw new Exception("Missing required parameter 'body' when calling PetApi->addPet") - val queryParams = new HashMap[String, String] - val headerParams = new HashMap[String, String] - val formParams = new HashMap[String, String] + val resFuture = client.submit("POST", path, queryParams.toMap, headerParams.toMap, writer.write(body)) + resFuture flatMap { resp => + process(reader.read(resp)) + } + } + + def deletePet(petId: Long, + apiKey: Option[String] = None + )(implicit reader: ClientResponseReader[Unit]): Future[Unit] = { + // create path and map variables + val path = (addFmt("/pet/{petId}") + replaceAll ("\\{" + "petId" + "\\}",petId.toString)) - + // query params + val queryParams = new mutable.HashMap[String, String] + val headerParams = new mutable.HashMap[String, String] - var postBody: AnyRef = null + apiKey match { + case Some(param) => headerParams += "api_key" -> param.toString + case _ => headerParams + } - if (contentType.startsWith("multipart/form-data")) { - val mp = new FormDataMultiPart - additionalMetadata.map(paramVal => mp.field("additionalMetadata", paramVal.toString, MediaType.MULTIPART_FORM_DATA_TYPE)) - file.map(fileVal => mp.field("file", fileVal.getName)) - file.map(paramVal => mp.bodyPart(new FileDataBodyPart("file", paramVal, MediaType.MULTIPART_FORM_DATA_TYPE))) - postBody = mp - } else { - additionalMetadata.map(paramVal => formParams += "additionalMetadata" -> paramVal.toString) + val resFuture = client.submit("DELETE", path, queryParams.toMap, headerParams.toMap, "") + resFuture flatMap { resp => + process(reader.read(resp)) } + } + + def findPetsByStatus(status: List[String])(implicit reader: ClientResponseReader[List[Pet]]): Future[List[Pet]] = { + // create path and map variables + val path = (addFmt("/pet/findByStatus")) + + // query params + val queryParams = new mutable.HashMap[String, String] + val headerParams = new mutable.HashMap[String, String] - try { - apiInvoker.invokeApi(basePath, path, "POST", queryParams.toMap, formParams.toMap, postBody, headerParams.toMap, contentType) match { - case s: String => - Some(apiInvoker.deserialize(s, "", classOf[ApiResponse]).asInstanceOf[ApiResponse]) - case _ => None - } - } catch { - case ex: ApiException if ex.code == 404 => None - case ex: ApiException => throw ex + if (status == null) throw new Exception("Missing required parameter 'status' when calling PetApi->findPetsByStatus") + queryParams += "status" -> status.toString + + val resFuture = client.submit("GET", path, queryParams.toMap, headerParams.toMap, "") + resFuture flatMap { resp => + process(reader.read(resp)) } - } + } + + def findPetsByTags(tags: List[String])(implicit reader: ClientResponseReader[List[Pet]]): Future[List[Pet]] = { + // create path and map variables + val path = (addFmt("/pet/findByTags")) + + // query params + val queryParams = new mutable.HashMap[String, String] + val headerParams = new mutable.HashMap[String, String] + + if (tags == null) throw new Exception("Missing required parameter 'tags' when calling PetApi->findPetsByTags") + queryParams += "tags" -> tags.toString + + val resFuture = client.submit("GET", path, queryParams.toMap, headerParams.toMap, "") + resFuture flatMap { resp => + process(reader.read(resp)) + } + } + + def getPetById(petId: Long)(implicit reader: ClientResponseReader[Pet]): Future[Pet] = { + // create path and map variables + val path = (addFmt("/pet/{petId}") + replaceAll ("\\{" + "petId" + "\\}",petId.toString)) + + // query params + val queryParams = new mutable.HashMap[String, String] + val headerParams = new mutable.HashMap[String, String] + + + val resFuture = client.submit("GET", path, queryParams.toMap, headerParams.toMap, "") + resFuture flatMap { resp => + process(reader.read(resp)) + } + } + + def updatePet(body: Pet)(implicit reader: ClientResponseReader[Unit], writer: RequestWriter[Pet]): Future[Unit] = { + // create path and map variables + val path = (addFmt("/pet")) + + // query params + val queryParams = new mutable.HashMap[String, String] + val headerParams = new mutable.HashMap[String, String] + + if (body == null) throw new Exception("Missing required parameter 'body' when calling PetApi->updatePet") + + val resFuture = client.submit("PUT", path, queryParams.toMap, headerParams.toMap, writer.write(body)) + resFuture flatMap { resp => + process(reader.read(resp)) + } + } + + def updatePetWithForm(petId: Long, + name: Option[String] = None, + status: Option[String] = None + )(implicit reader: ClientResponseReader[Unit]): Future[Unit] = { + // create path and map variables + val path = (addFmt("/pet/{petId}") + replaceAll ("\\{" + "petId" + "\\}",petId.toString)) + + // query params + val queryParams = new mutable.HashMap[String, String] + val headerParams = new mutable.HashMap[String, String] + + + val resFuture = client.submit("POST", path, queryParams.toMap, headerParams.toMap, "") + resFuture flatMap { resp => + process(reader.read(resp)) + } + } + + def uploadFile(petId: Long, + additionalMetadata: Option[String] = None, + file: Option[File] = None + )(implicit reader: ClientResponseReader[ApiResponse]): Future[ApiResponse] = { + // create path and map variables + val path = (addFmt("/pet/{petId}/uploadImage") + replaceAll ("\\{" + "petId" + "\\}",petId.toString)) + + // query params + val queryParams = new mutable.HashMap[String, String] + val headerParams = new mutable.HashMap[String, String] + + + val resFuture = client.submit("POST", path, queryParams.toMap, headerParams.toMap, "") + resFuture flatMap { resp => + process(reader.read(resp)) + } + } + } diff --git a/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/StoreApi.scala b/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/StoreApi.scala index 3f72757f9eb..f394221cf85 100644 --- a/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/StoreApi.scala +++ b/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/StoreApi.scala @@ -15,6 +15,7 @@ package io.swagger.client.api import io.swagger.client.model.Order import io.swagger.client.ApiInvoker import io.swagger.client.ApiException +import io.swagger.client.AsyncClient import com.sun.jersey.multipart.FormDataMultiPart import com.sun.jersey.multipart.file.FileDataBodyPart @@ -26,12 +27,38 @@ import java.util.Date import scala.collection.mutable.HashMap +import com.wordnik.swagger.client._ +import scala.concurrent.Future +import collection.mutable + +import java.net.URI + +import com.wordnik.swagger.client.ClientResponseReaders.Json4sFormatsReader._ +import com.wordnik.swagger.client.RequestWriters.Json4sFormatsWriter._ + +import scala.concurrent.ExecutionContext.Implicits.global +import scala.concurrent._ +import scala.concurrent.duration._ +import scala.util.{Failure, Success, Try} + class StoreApi(val defBasePath: String = "http://petstore.swagger.io/v2", defApiInvoker: ApiInvoker = ApiInvoker) { + + implicit val formats = org.json4s.DefaultFormats + implicit val reader = JsonFormatsReader + implicit val writer = JsonFormatsWriter + var basePath = defBasePath var apiInvoker = defApiInvoker - def addHeader(key: String, value: String) = apiInvoker.defaultHeaders += key -> value + def addHeader(key: String, value: String) = apiInvoker.defaultHeaders += key -> value + + private[this] val client = transportClient + + protected def transportClient: TransportClient = new RestClient(config) + + val config = SwaggerConfig.forUrl(new URI(defBasePath)) + val helper = new StoreApiAsyncHelper(client, config) /** * Delete purchase order by ID @@ -40,77 +67,49 @@ class StoreApi(val defBasePath: String = "http://petstore.swagger.io/v2", * @return void */ def deleteOrder(orderId: String) = { - // create path and map variables - val path = "/store/order/{orderId}".replaceAll("\\{format\\}", "json").replaceAll("\\{" + "orderId" + "\\}",apiInvoker.escape(orderId)) - - val contentTypes = List("application/json") - val contentType = contentTypes(0) - - val queryParams = new HashMap[String, String] - val headerParams = new HashMap[String, String] - val formParams = new HashMap[String, String] - - if (orderId == null) throw new Exception("Missing required parameter 'orderId' when calling StoreApi->deleteOrder") - - - - var postBody: AnyRef = null - - if (contentType.startsWith("multipart/form-data")) { - val mp = new FormDataMultiPart - postBody = mp - } else { + val await = Try(Await.result(deleteOrderAsync(orderId), Duration.Inf)) + await match { + case Success(i) => Some(await.get) + case Failure(t) => None } - try { - apiInvoker.invokeApi(basePath, path, "DELETE", queryParams.toMap, formParams.toMap, postBody, headerParams.toMap, contentType) match { - case s: String => - case _ => None - } - } catch { - case ex: ApiException if ex.code == 404 => None - case ex: ApiException => throw ex - } } + /** + * Delete purchase order by ID asynchronously + * For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors + * @param orderId ID of the order that needs to be deleted + * @return Future(void) + */ + def deleteOrderAsync(orderId: String) = { + helper.deleteOrder(orderId) + } + + /** * Returns pet inventories by status * Returns a map of status codes to quantities * @return Map[String, Integer] */ def getInventory(): Option[Map[String, Integer]] = { - // create path and map variables - val path = "/store/inventory".replaceAll("\\{format\\}", "json") - - val contentTypes = List("application/json") - val contentType = contentTypes(0) - - val queryParams = new HashMap[String, String] - val headerParams = new HashMap[String, String] - val formParams = new HashMap[String, String] - - - - var postBody: AnyRef = null - - if (contentType.startsWith("multipart/form-data")) { - val mp = new FormDataMultiPart - postBody = mp - } else { + val await = Try(Await.result(getInventoryAsync(), Duration.Inf)) + await match { + case Success(i) => Some(await.get) + case Failure(t) => None } - try { - apiInvoker.invokeApi(basePath, path, "GET", queryParams.toMap, formParams.toMap, postBody, headerParams.toMap, contentType) match { - case s: String => - Some(apiInvoker.deserialize(s, "map", classOf[Integer]).asInstanceOf[Map[String, Integer]]) - case _ => None - } - } catch { - case ex: ApiException if ex.code == 404 => None - case ex: ApiException => throw ex - } } + /** + * Returns pet inventories by status asynchronously + * Returns a map of status codes to quantities + * @return Future(Map[String, Integer]) + */ + def getInventoryAsync(): Future[Map[String, Integer]] = { + helper.getInventory() + } + + /** * Find purchase order by ID * For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions @@ -118,38 +117,25 @@ class StoreApi(val defBasePath: String = "http://petstore.swagger.io/v2", * @return Order */ def getOrderById(orderId: Long): Option[Order] = { - // create path and map variables - val path = "/store/order/{orderId}".replaceAll("\\{format\\}", "json").replaceAll("\\{" + "orderId" + "\\}",apiInvoker.escape(orderId)) - - val contentTypes = List("application/json") - val contentType = contentTypes(0) - - val queryParams = new HashMap[String, String] - val headerParams = new HashMap[String, String] - val formParams = new HashMap[String, String] - - - - var postBody: AnyRef = null - - if (contentType.startsWith("multipart/form-data")) { - val mp = new FormDataMultiPart - postBody = mp - } else { + val await = Try(Await.result(getOrderByIdAsync(orderId), Duration.Inf)) + await match { + case Success(i) => Some(await.get) + case Failure(t) => None } - try { - apiInvoker.invokeApi(basePath, path, "GET", queryParams.toMap, formParams.toMap, postBody, headerParams.toMap, contentType) match { - case s: String => - Some(apiInvoker.deserialize(s, "", classOf[Order]).asInstanceOf[Order]) - case _ => None - } - } catch { - case ex: ApiException if ex.code == 404 => None - case ex: ApiException => throw ex - } } + /** + * Find purchase order by ID asynchronously + * For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions + * @param orderId ID of pet that needs to be fetched + * @return Future(Order) + */ + def getOrderByIdAsync(orderId: Long): Future[Order] = { + helper.getOrderById(orderId) + } + + /** * Place an order for a pet * @@ -157,38 +143,93 @@ class StoreApi(val defBasePath: String = "http://petstore.swagger.io/v2", * @return Order */ def placeOrder(body: Order): Option[Order] = { + val await = Try(Await.result(placeOrderAsync(body), Duration.Inf)) + await match { + case Success(i) => Some(await.get) + case Failure(t) => None + } + + } + + /** + * Place an order for a pet asynchronously + * + * @param body order placed for purchasing the pet + * @return Future(Order) + */ + def placeOrderAsync(body: Order): Future[Order] = { + helper.placeOrder(body) + } + + +} + +class StoreApiAsyncHelper(client: TransportClient, config: SwaggerConfig) extends ApiClient(client, config) { + + def deleteOrder(orderId: String)(implicit reader: ClientResponseReader[Unit]): Future[Unit] = { // create path and map variables - val path = "/store/order".replaceAll("\\{format\\}", "json") + val path = (addFmt("/store/order/{orderId}") + replaceAll ("\\{" + "orderId" + "\\}",orderId.toString)) - val contentTypes = List("application/json") - val contentType = contentTypes(0) + // query params + val queryParams = new mutable.HashMap[String, String] + val headerParams = new mutable.HashMap[String, String] - val queryParams = new HashMap[String, String] - val headerParams = new HashMap[String, String] - val formParams = new HashMap[String, String] + if (orderId == null) throw new Exception("Missing required parameter 'orderId' when calling StoreApi->deleteOrder") - if (body == null) throw new Exception("Missing required parameter 'body' when calling StoreApi->placeOrder") - + val resFuture = client.submit("DELETE", path, queryParams.toMap, headerParams.toMap, "") + resFuture flatMap { resp => + process(reader.read(resp)) + } + } - var postBody: AnyRef = body + def getInventory()(implicit reader: ClientResponseReader[Map[String, Integer]]): Future[Map[String, Integer]] = { + // create path and map variables + val path = (addFmt("/store/inventory")) - if (contentType.startsWith("multipart/form-data")) { - val mp = new FormDataMultiPart - postBody = mp - } else { + // query params + val queryParams = new mutable.HashMap[String, String] + val headerParams = new mutable.HashMap[String, String] + + + val resFuture = client.submit("GET", path, queryParams.toMap, headerParams.toMap, "") + resFuture flatMap { resp => + process(reader.read(resp)) + } } - try { - apiInvoker.invokeApi(basePath, path, "POST", queryParams.toMap, formParams.toMap, postBody, headerParams.toMap, contentType) match { - case s: String => - Some(apiInvoker.deserialize(s, "", classOf[Order]).asInstanceOf[Order]) - case _ => None - } - } catch { - case ex: ApiException if ex.code == 404 => None - case ex: ApiException => throw ex + def getOrderById(orderId: Long)(implicit reader: ClientResponseReader[Order]): Future[Order] = { + // create path and map variables + val path = (addFmt("/store/order/{orderId}") + replaceAll ("\\{" + "orderId" + "\\}",orderId.toString)) + + // query params + val queryParams = new mutable.HashMap[String, String] + val headerParams = new mutable.HashMap[String, String] + + + val resFuture = client.submit("GET", path, queryParams.toMap, headerParams.toMap, "") + resFuture flatMap { resp => + process(reader.read(resp)) } - } + } + + def placeOrder(body: Order)(implicit reader: ClientResponseReader[Order], writer: RequestWriter[Order]): Future[Order] = { + // create path and map variables + val path = (addFmt("/store/order")) + + // query params + val queryParams = new mutable.HashMap[String, String] + val headerParams = new mutable.HashMap[String, String] + + if (body == null) throw new Exception("Missing required parameter 'body' when calling StoreApi->placeOrder") + + val resFuture = client.submit("POST", path, queryParams.toMap, headerParams.toMap, writer.write(body)) + resFuture flatMap { resp => + process(reader.read(resp)) + } + } + } diff --git a/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/UserApi.scala b/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/UserApi.scala index df039c1f7cc..36fe21022a6 100644 --- a/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/UserApi.scala +++ b/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/UserApi.scala @@ -15,6 +15,7 @@ package io.swagger.client.api import io.swagger.client.model.User import io.swagger.client.ApiInvoker import io.swagger.client.ApiException +import io.swagger.client.AsyncClient import com.sun.jersey.multipart.FormDataMultiPart import com.sun.jersey.multipart.file.FileDataBodyPart @@ -26,12 +27,38 @@ import java.util.Date import scala.collection.mutable.HashMap +import com.wordnik.swagger.client._ +import scala.concurrent.Future +import collection.mutable + +import java.net.URI + +import com.wordnik.swagger.client.ClientResponseReaders.Json4sFormatsReader._ +import com.wordnik.swagger.client.RequestWriters.Json4sFormatsWriter._ + +import scala.concurrent.ExecutionContext.Implicits.global +import scala.concurrent._ +import scala.concurrent.duration._ +import scala.util.{Failure, Success, Try} + class UserApi(val defBasePath: String = "http://petstore.swagger.io/v2", defApiInvoker: ApiInvoker = ApiInvoker) { + + implicit val formats = org.json4s.DefaultFormats + implicit val reader = JsonFormatsReader + implicit val writer = JsonFormatsWriter + var basePath = defBasePath var apiInvoker = defApiInvoker - def addHeader(key: String, value: String) = apiInvoker.defaultHeaders += key -> value + def addHeader(key: String, value: String) = apiInvoker.defaultHeaders += key -> value + + private[this] val client = transportClient + + protected def transportClient: TransportClient = new RestClient(config) + + val config = SwaggerConfig.forUrl(new URI(defBasePath)) + val helper = new UserApiAsyncHelper(client, config) /** * Create user @@ -40,39 +67,25 @@ class UserApi(val defBasePath: String = "http://petstore.swagger.io/v2", * @return void */ def createUser(body: User) = { - // create path and map variables - val path = "/user".replaceAll("\\{format\\}", "json") - - val contentTypes = List("application/json") - val contentType = contentTypes(0) - - val queryParams = new HashMap[String, String] - val headerParams = new HashMap[String, String] - val formParams = new HashMap[String, String] - - if (body == null) throw new Exception("Missing required parameter 'body' when calling UserApi->createUser") - - - - var postBody: AnyRef = body - - if (contentType.startsWith("multipart/form-data")) { - val mp = new FormDataMultiPart - postBody = mp - } else { + val await = Try(Await.result(createUserAsync(body), Duration.Inf)) + await match { + case Success(i) => Some(await.get) + case Failure(t) => None } - try { - apiInvoker.invokeApi(basePath, path, "POST", queryParams.toMap, formParams.toMap, postBody, headerParams.toMap, contentType) match { - case s: String => - case _ => None - } - } catch { - case ex: ApiException if ex.code == 404 => None - case ex: ApiException => throw ex - } } + /** + * Create user asynchronously + * This can only be done by the logged in user. + * @param body Created user object + * @return Future(void) + */ + def createUserAsync(body: User) = { + helper.createUser(body) + } + + /** * Creates list of users with given input array * @@ -80,39 +93,25 @@ class UserApi(val defBasePath: String = "http://petstore.swagger.io/v2", * @return void */ def createUsersWithArrayInput(body: List[User]) = { - // create path and map variables - val path = "/user/createWithArray".replaceAll("\\{format\\}", "json") - - val contentTypes = List("application/json") - val contentType = contentTypes(0) - - val queryParams = new HashMap[String, String] - val headerParams = new HashMap[String, String] - val formParams = new HashMap[String, String] - - if (body == null) throw new Exception("Missing required parameter 'body' when calling UserApi->createUsersWithArrayInput") - - - - var postBody: AnyRef = body - - if (contentType.startsWith("multipart/form-data")) { - val mp = new FormDataMultiPart - postBody = mp - } else { + val await = Try(Await.result(createUsersWithArrayInputAsync(body), Duration.Inf)) + await match { + case Success(i) => Some(await.get) + case Failure(t) => None } - try { - apiInvoker.invokeApi(basePath, path, "POST", queryParams.toMap, formParams.toMap, postBody, headerParams.toMap, contentType) match { - case s: String => - case _ => None - } - } catch { - case ex: ApiException if ex.code == 404 => None - case ex: ApiException => throw ex - } } + /** + * Creates list of users with given input array asynchronously + * + * @param body List of user object + * @return Future(void) + */ + def createUsersWithArrayInputAsync(body: List[User]) = { + helper.createUsersWithArrayInput(body) + } + + /** * Creates list of users with given input array * @@ -120,39 +119,25 @@ class UserApi(val defBasePath: String = "http://petstore.swagger.io/v2", * @return void */ def createUsersWithListInput(body: List[User]) = { - // create path and map variables - val path = "/user/createWithList".replaceAll("\\{format\\}", "json") - - val contentTypes = List("application/json") - val contentType = contentTypes(0) - - val queryParams = new HashMap[String, String] - val headerParams = new HashMap[String, String] - val formParams = new HashMap[String, String] - - if (body == null) throw new Exception("Missing required parameter 'body' when calling UserApi->createUsersWithListInput") - - - - var postBody: AnyRef = body - - if (contentType.startsWith("multipart/form-data")) { - val mp = new FormDataMultiPart - postBody = mp - } else { + val await = Try(Await.result(createUsersWithListInputAsync(body), Duration.Inf)) + await match { + case Success(i) => Some(await.get) + case Failure(t) => None } - try { - apiInvoker.invokeApi(basePath, path, "POST", queryParams.toMap, formParams.toMap, postBody, headerParams.toMap, contentType) match { - case s: String => - case _ => None - } - } catch { - case ex: ApiException if ex.code == 404 => None - case ex: ApiException => throw ex - } } + /** + * Creates list of users with given input array asynchronously + * + * @param body List of user object + * @return Future(void) + */ + def createUsersWithListInputAsync(body: List[User]) = { + helper.createUsersWithListInput(body) + } + + /** * Delete user * This can only be done by the logged in user. @@ -160,39 +145,25 @@ class UserApi(val defBasePath: String = "http://petstore.swagger.io/v2", * @return void */ def deleteUser(username: String) = { - // create path and map variables - val path = "/user/{username}".replaceAll("\\{format\\}", "json").replaceAll("\\{" + "username" + "\\}",apiInvoker.escape(username)) - - val contentTypes = List("application/json") - val contentType = contentTypes(0) - - val queryParams = new HashMap[String, String] - val headerParams = new HashMap[String, String] - val formParams = new HashMap[String, String] - - if (username == null) throw new Exception("Missing required parameter 'username' when calling UserApi->deleteUser") - - - - var postBody: AnyRef = null - - if (contentType.startsWith("multipart/form-data")) { - val mp = new FormDataMultiPart - postBody = mp - } else { + val await = Try(Await.result(deleteUserAsync(username), Duration.Inf)) + await match { + case Success(i) => Some(await.get) + case Failure(t) => None } - try { - apiInvoker.invokeApi(basePath, path, "DELETE", queryParams.toMap, formParams.toMap, postBody, headerParams.toMap, contentType) match { - case s: String => - case _ => None - } - } catch { - case ex: ApiException if ex.code == 404 => None - case ex: ApiException => throw ex - } } + /** + * Delete user asynchronously + * This can only be done by the logged in user. + * @param username The name that needs to be deleted + * @return Future(void) + */ + def deleteUserAsync(username: String) = { + helper.deleteUser(username) + } + + /** * Get user by user name * @@ -200,40 +171,25 @@ class UserApi(val defBasePath: String = "http://petstore.swagger.io/v2", * @return User */ def getUserByName(username: String): Option[User] = { - // create path and map variables - val path = "/user/{username}".replaceAll("\\{format\\}", "json").replaceAll("\\{" + "username" + "\\}",apiInvoker.escape(username)) - - val contentTypes = List("application/json") - val contentType = contentTypes(0) - - val queryParams = new HashMap[String, String] - val headerParams = new HashMap[String, String] - val formParams = new HashMap[String, String] - - if (username == null) throw new Exception("Missing required parameter 'username' when calling UserApi->getUserByName") - - - - var postBody: AnyRef = null - - if (contentType.startsWith("multipart/form-data")) { - val mp = new FormDataMultiPart - postBody = mp - } else { + val await = Try(Await.result(getUserByNameAsync(username), Duration.Inf)) + await match { + case Success(i) => Some(await.get) + case Failure(t) => None } - try { - apiInvoker.invokeApi(basePath, path, "GET", queryParams.toMap, formParams.toMap, postBody, headerParams.toMap, contentType) match { - case s: String => - Some(apiInvoker.deserialize(s, "", classOf[User]).asInstanceOf[User]) - case _ => None - } - } catch { - case ex: ApiException if ex.code == 404 => None - case ex: ApiException => throw ex - } } + /** + * Get user by user name asynchronously + * + * @param username The name that needs to be fetched. Use user1 for testing. + * @return Future(User) + */ + def getUserByNameAsync(username: String): Future[User] = { + helper.getUserByName(username) + } + + /** * Logs user into the system * @@ -242,81 +198,50 @@ class UserApi(val defBasePath: String = "http://petstore.swagger.io/v2", * @return String */ def loginUser(username: String, password: String): Option[String] = { - // create path and map variables - val path = "/user/login".replaceAll("\\{format\\}", "json") - - val contentTypes = List("application/json") - val contentType = contentTypes(0) - - val queryParams = new HashMap[String, String] - val headerParams = new HashMap[String, String] - val formParams = new HashMap[String, String] - - if (username == null) throw new Exception("Missing required parameter 'username' when calling UserApi->loginUser") - - if (password == null) throw new Exception("Missing required parameter 'password' when calling UserApi->loginUser") - - queryParams += "username" -> username.toString - queryParams += "password" -> password.toString - - - var postBody: AnyRef = null - - if (contentType.startsWith("multipart/form-data")) { - val mp = new FormDataMultiPart - postBody = mp - } else { + val await = Try(Await.result(loginUserAsync(username, password), Duration.Inf)) + await match { + case Success(i) => Some(await.get) + case Failure(t) => None } - try { - apiInvoker.invokeApi(basePath, path, "GET", queryParams.toMap, formParams.toMap, postBody, headerParams.toMap, contentType) match { - case s: String => - Some(apiInvoker.deserialize(s, "", classOf[String]).asInstanceOf[String]) - case _ => None - } - } catch { - case ex: ApiException if ex.code == 404 => None - case ex: ApiException => throw ex - } } + /** + * Logs user into the system asynchronously + * + * @param username The user name for login + * @param password The password for login in clear text + * @return Future(String) + */ + def loginUserAsync(username: String, password: String): Future[String] = { + helper.loginUser(username, password) + } + + /** * Logs out current logged in user session * * @return void */ def logoutUser() = { - // create path and map variables - val path = "/user/logout".replaceAll("\\{format\\}", "json") - - val contentTypes = List("application/json") - val contentType = contentTypes(0) - - val queryParams = new HashMap[String, String] - val headerParams = new HashMap[String, String] - val formParams = new HashMap[String, String] - - - - var postBody: AnyRef = null - - if (contentType.startsWith("multipart/form-data")) { - val mp = new FormDataMultiPart - postBody = mp - } else { + val await = Try(Await.result(logoutUserAsync(), Duration.Inf)) + await match { + case Success(i) => Some(await.get) + case Failure(t) => None } - try { - apiInvoker.invokeApi(basePath, path, "GET", queryParams.toMap, formParams.toMap, postBody, headerParams.toMap, contentType) match { - case s: String => - case _ => None - } - } catch { - case ex: ApiException if ex.code == 404 => None - case ex: ApiException => throw ex - } } + /** + * Logs out current logged in user session asynchronously + * + * @return Future(void) + */ + def logoutUserAsync() = { + helper.logoutUser() + } + + /** * Updated user * This can only be done by the logged in user. @@ -325,39 +250,170 @@ class UserApi(val defBasePath: String = "http://petstore.swagger.io/v2", * @return void */ def updateUser(username: String, body: User) = { + val await = Try(Await.result(updateUserAsync(username, body), Duration.Inf)) + await match { + case Success(i) => Some(await.get) + case Failure(t) => None + } + + } + + /** + * Updated user asynchronously + * This can only be done by the logged in user. + * @param username name that need to be deleted + * @param body Updated user object + * @return Future(void) + */ + def updateUserAsync(username: String, body: User) = { + helper.updateUser(username, body) + } + + +} + +class UserApiAsyncHelper(client: TransportClient, config: SwaggerConfig) extends ApiClient(client, config) { + + def createUser(body: User)(implicit reader: ClientResponseReader[Unit], writer: RequestWriter[User]): Future[Unit] = { // create path and map variables - val path = "/user/{username}".replaceAll("\\{format\\}", "json").replaceAll("\\{" + "username" + "\\}",apiInvoker.escape(username)) + val path = (addFmt("/user")) - val contentTypes = List("application/json") - val contentType = contentTypes(0) + // query params + val queryParams = new mutable.HashMap[String, String] + val headerParams = new mutable.HashMap[String, String] - val queryParams = new HashMap[String, String] - val headerParams = new HashMap[String, String] - val formParams = new HashMap[String, String] + if (body == null) throw new Exception("Missing required parameter 'body' when calling UserApi->createUser") - if (username == null) throw new Exception("Missing required parameter 'username' when calling UserApi->updateUser") + val resFuture = client.submit("POST", path, queryParams.toMap, headerParams.toMap, writer.write(body)) + resFuture flatMap { resp => + process(reader.read(resp)) + } + } - if (body == null) throw new Exception("Missing required parameter 'body' when calling UserApi->updateUser") + def createUsersWithArrayInput(body: List[User])(implicit reader: ClientResponseReader[Unit], writer: RequestWriter[List[User]]): Future[Unit] = { + // create path and map variables + val path = (addFmt("/user/createWithArray")) - + // query params + val queryParams = new mutable.HashMap[String, String] + val headerParams = new mutable.HashMap[String, String] - var postBody: AnyRef = body + if (body == null) throw new Exception("Missing required parameter 'body' when calling UserApi->createUsersWithArrayInput") - if (contentType.startsWith("multipart/form-data")) { - val mp = new FormDataMultiPart - postBody = mp - } else { + val resFuture = client.submit("POST", path, queryParams.toMap, headerParams.toMap, writer.write(body)) + resFuture flatMap { resp => + process(reader.read(resp)) + } } - try { - apiInvoker.invokeApi(basePath, path, "PUT", queryParams.toMap, formParams.toMap, postBody, headerParams.toMap, contentType) match { - case s: String => - case _ => None - } - } catch { - case ex: ApiException if ex.code == 404 => None - case ex: ApiException => throw ex + def createUsersWithListInput(body: List[User])(implicit reader: ClientResponseReader[Unit], writer: RequestWriter[List[User]]): Future[Unit] = { + // create path and map variables + val path = (addFmt("/user/createWithList")) + + // query params + val queryParams = new mutable.HashMap[String, String] + val headerParams = new mutable.HashMap[String, String] + + if (body == null) throw new Exception("Missing required parameter 'body' when calling UserApi->createUsersWithListInput") + + val resFuture = client.submit("POST", path, queryParams.toMap, headerParams.toMap, writer.write(body)) + resFuture flatMap { resp => + process(reader.read(resp)) } - } + } + + def deleteUser(username: String)(implicit reader: ClientResponseReader[Unit]): Future[Unit] = { + // create path and map variables + val path = (addFmt("/user/{username}") + replaceAll ("\\{" + "username" + "\\}",username.toString)) + + // query params + val queryParams = new mutable.HashMap[String, String] + val headerParams = new mutable.HashMap[String, String] + + if (username == null) throw new Exception("Missing required parameter 'username' when calling UserApi->deleteUser") + + + val resFuture = client.submit("DELETE", path, queryParams.toMap, headerParams.toMap, "") + resFuture flatMap { resp => + process(reader.read(resp)) + } + } + + def getUserByName(username: String)(implicit reader: ClientResponseReader[User]): Future[User] = { + // create path and map variables + val path = (addFmt("/user/{username}") + replaceAll ("\\{" + "username" + "\\}",username.toString)) + + // query params + val queryParams = new mutable.HashMap[String, String] + val headerParams = new mutable.HashMap[String, String] + + if (username == null) throw new Exception("Missing required parameter 'username' when calling UserApi->getUserByName") + + + val resFuture = client.submit("GET", path, queryParams.toMap, headerParams.toMap, "") + resFuture flatMap { resp => + process(reader.read(resp)) + } + } + + def loginUser(username: String, + password: String)(implicit reader: ClientResponseReader[String]): Future[String] = { + // create path and map variables + val path = (addFmt("/user/login")) + + // query params + val queryParams = new mutable.HashMap[String, String] + val headerParams = new mutable.HashMap[String, String] + + if (username == null) throw new Exception("Missing required parameter 'username' when calling UserApi->loginUser") + + if (password == null) throw new Exception("Missing required parameter 'password' when calling UserApi->loginUser") + + queryParams += "username" -> username.toString + queryParams += "password" -> password.toString + + val resFuture = client.submit("GET", path, queryParams.toMap, headerParams.toMap, "") + resFuture flatMap { resp => + process(reader.read(resp)) + } + } + + def logoutUser()(implicit reader: ClientResponseReader[Unit]): Future[Unit] = { + // create path and map variables + val path = (addFmt("/user/logout")) + + // query params + val queryParams = new mutable.HashMap[String, String] + val headerParams = new mutable.HashMap[String, String] + + + val resFuture = client.submit("GET", path, queryParams.toMap, headerParams.toMap, "") + resFuture flatMap { resp => + process(reader.read(resp)) + } + } + + def updateUser(username: String, + body: User)(implicit reader: ClientResponseReader[Unit], writer: RequestWriter[User]): Future[Unit] = { + // create path and map variables + val path = (addFmt("/user/{username}") + replaceAll ("\\{" + "username" + "\\}",username.toString)) + + // query params + val queryParams = new mutable.HashMap[String, String] + val headerParams = new mutable.HashMap[String, String] + + if (username == null) throw new Exception("Missing required parameter 'username' when calling UserApi->updateUser") + + if (body == null) throw new Exception("Missing required parameter 'body' when calling UserApi->updateUser") + + val resFuture = client.submit("PUT", path, queryParams.toMap, headerParams.toMap, writer.write(body)) + resFuture flatMap { resp => + process(reader.read(resp)) + } + } + } From 3a905518964824dbd22b4a15122a686b936ac401 Mon Sep 17 00:00:00 2001 From: clasnake Date: Sun, 19 Feb 2017 11:24:46 +0800 Subject: [PATCH 2/6] Format api endpoints. --- .../src/main/resources/scala/api.mustache | 37 ++++----- .../scala/io/swagger/client/api/PetApi.scala | 81 +++++++++---------- .../io/swagger/client/api/StoreApi.scala | 37 ++++----- .../scala/io/swagger/client/api/UserApi.scala | 81 +++++++++---------- 4 files changed, 112 insertions(+), 124 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/scala/api.mustache b/modules/swagger-codegen/src/main/resources/scala/api.mustache index c85a78be27b..6cb5869953d 100644 --- a/modules/swagger-codegen/src/main/resources/scala/api.mustache +++ b/modules/swagger-codegen/src/main/resources/scala/api.mustache @@ -44,11 +44,8 @@ class {{classname}}(val defBasePath: String = "{{basePath}}", def addHeader(key: String, value: String) = apiInvoker.defaultHeaders += key -> value - private[this] val client = transportClient - - protected def transportClient: TransportClient = new RestClient(config) - val config = SwaggerConfig.forUrl(new URI(defBasePath)) + val client = new RestClient(config) val helper = new {{classname}}AsyncHelper(client, config) {{#operation}} @@ -84,12 +81,12 @@ class {{classname}}(val defBasePath: String = "{{basePath}}", class {{classname}}AsyncHelper(client: TransportClient, config: SwaggerConfig) extends ApiClient(client, config) { {{#operation}} - def {{operationId}}({{#allParams}}{{^required}}{{paramName}}: Option[{{dataType}}] = {{#defaultValue}}Some({{defaultValue}}){{/defaultValue}}{{^defaultValue}}None{{/defaultValue}}{{#hasMore}},{{/hasMore}} + def {{operationId}}({{#allParams}}{{^required}}{{paramName}}: Option[{{dataType}}] = {{#defaultValue}}Some({{defaultValue}}){{/defaultValue}}{{^defaultValue}}None{{/defaultValue}}{{#hasMore}},{{/hasMore}} {{/required}}{{#required}}{{paramName}}: {{dataType}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/allParams}})(implicit reader: ClientResponseReader[{{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}Unit{{/returnType}}]{{#bodyParams}}, writer: RequestWriter[{{dataType}}]{{/bodyParams}}){{#returnType}}: Future[{{returnType}}]{{/returnType}}{{^returnType}}: Future[Unit]{{/returnType}} = { // create path and map variables val path = (addFmt("{{path}}"){{#pathParams}} - replaceAll ("\\{" + "{{baseName}}" + "\\}",{{paramName}}.toString){{/pathParams}}) + replaceAll ("\\{" + "{{baseName}}" + "\\}",{{paramName}}.toString){{/pathParams}}) // query params val queryParams = new mutable.HashMap[String, String] @@ -98,42 +95,42 @@ class {{classname}}AsyncHelper(client: TransportClient, config: SwaggerConfig) e {{#allParams}} {{#required}} {{^isPrimitiveType}} - if ({{paramName}} == null) throw new Exception("Missing required parameter '{{paramName}}' when calling {{classname}}->{{operationId}}") + if ({{paramName}} == null) throw new Exception("Missing required parameter '{{paramName}}' when calling {{classname}}->{{operationId}}") {{/isPrimitiveType}} {{#isString}} - if ({{paramName}} == null) throw new Exception("Missing required parameter '{{paramName}}' when calling {{classname}}->{{operationId}}") + if ({{paramName}} == null) throw new Exception("Missing required parameter '{{paramName}}' when calling {{classname}}->{{operationId}}") {{/isString}} {{/required}} {{/allParams}} {{#queryParams}} {{^required}} - {{paramName}} match { - case Some(param) => queryParams += "{{baseName}}" -> param.toString - case _ => queryParams - } + {{paramName}} match { + case Some(param) => queryParams += "{{baseName}}" -> param.toString + case _ => queryParams + } {{/required}} {{#required}} - queryParams += "{{baseName}}" -> {{paramName}}.toString + queryParams += "{{baseName}}" -> {{paramName}}.toString {{/required}} {{/queryParams}} {{#headerParams}} {{^required}} - {{paramName}} match { - case Some(param) => headerParams += "{{baseName}}" -> param.toString - case _ => headerParams - } + {{paramName}} match { + case Some(param) => headerParams += "{{baseName}}" -> param.toString + case _ => headerParams + } {{/required}} {{#required}} - headerParams += "{{baseName}}" -> {{paramName}}.toString + headerParams += "{{baseName}}" -> {{paramName}}.toString {{/required}} {{/headerParams}} val resFuture = client.submit("{{httpMethod}}", path, queryParams.toMap, headerParams.toMap, {{#bodyParam}}writer.write({{paramName}}){{/bodyParam}}{{^bodyParam}}"{{emptyBodyParam}}"{{/bodyParam}}) resFuture flatMap { resp => - process(reader.read(resp)) - } + process(reader.read(resp)) } + } {{/operation}} diff --git a/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/PetApi.scala b/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/PetApi.scala index ef7401ad51e..61b1624569c 100644 --- a/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/PetApi.scala +++ b/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/PetApi.scala @@ -55,11 +55,8 @@ class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2", def addHeader(key: String, value: String) = apiInvoker.defaultHeaders += key -> value - private[this] val client = transportClient - - protected def transportClient: TransportClient = new RestClient(config) - val config = SwaggerConfig.forUrl(new URI(defBasePath)) + val client = new RestClient(config) val helper = new PetApiAsyncHelper(client, config) /** @@ -284,7 +281,7 @@ class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2", class PetApiAsyncHelper(client: TransportClient, config: SwaggerConfig) extends ApiClient(client, config) { - def addPet(body: Pet)(implicit reader: ClientResponseReader[Unit], writer: RequestWriter[Pet]): Future[Unit] = { + def addPet(body: Pet)(implicit reader: ClientResponseReader[Unit], writer: RequestWriter[Pet]): Future[Unit] = { // create path and map variables val path = (addFmt("/pet")) @@ -292,37 +289,37 @@ class PetApiAsyncHelper(client: TransportClient, config: SwaggerConfig) extends val queryParams = new mutable.HashMap[String, String] val headerParams = new mutable.HashMap[String, String] - if (body == null) throw new Exception("Missing required parameter 'body' when calling PetApi->addPet") + if (body == null) throw new Exception("Missing required parameter 'body' when calling PetApi->addPet") val resFuture = client.submit("POST", path, queryParams.toMap, headerParams.toMap, writer.write(body)) resFuture flatMap { resp => - process(reader.read(resp)) - } + process(reader.read(resp)) } + } - def deletePet(petId: Long, + def deletePet(petId: Long, apiKey: Option[String] = None )(implicit reader: ClientResponseReader[Unit]): Future[Unit] = { // create path and map variables val path = (addFmt("/pet/{petId}") - replaceAll ("\\{" + "petId" + "\\}",petId.toString)) + replaceAll ("\\{" + "petId" + "\\}",petId.toString)) // query params val queryParams = new mutable.HashMap[String, String] val headerParams = new mutable.HashMap[String, String] - apiKey match { - case Some(param) => headerParams += "api_key" -> param.toString - case _ => headerParams - } + apiKey match { + case Some(param) => headerParams += "api_key" -> param.toString + case _ => headerParams + } val resFuture = client.submit("DELETE", path, queryParams.toMap, headerParams.toMap, "") resFuture flatMap { resp => - process(reader.read(resp)) - } + process(reader.read(resp)) } + } - def findPetsByStatus(status: List[String])(implicit reader: ClientResponseReader[List[Pet]]): Future[List[Pet]] = { + def findPetsByStatus(status: List[String])(implicit reader: ClientResponseReader[List[Pet]]): Future[List[Pet]] = { // create path and map variables val path = (addFmt("/pet/findByStatus")) @@ -330,16 +327,16 @@ class PetApiAsyncHelper(client: TransportClient, config: SwaggerConfig) extends val queryParams = new mutable.HashMap[String, String] val headerParams = new mutable.HashMap[String, String] - if (status == null) throw new Exception("Missing required parameter 'status' when calling PetApi->findPetsByStatus") - queryParams += "status" -> status.toString + if (status == null) throw new Exception("Missing required parameter 'status' when calling PetApi->findPetsByStatus") + queryParams += "status" -> status.toString val resFuture = client.submit("GET", path, queryParams.toMap, headerParams.toMap, "") resFuture flatMap { resp => - process(reader.read(resp)) - } + process(reader.read(resp)) } + } - def findPetsByTags(tags: List[String])(implicit reader: ClientResponseReader[List[Pet]]): Future[List[Pet]] = { + def findPetsByTags(tags: List[String])(implicit reader: ClientResponseReader[List[Pet]]): Future[List[Pet]] = { // create path and map variables val path = (addFmt("/pet/findByTags")) @@ -347,19 +344,19 @@ class PetApiAsyncHelper(client: TransportClient, config: SwaggerConfig) extends val queryParams = new mutable.HashMap[String, String] val headerParams = new mutable.HashMap[String, String] - if (tags == null) throw new Exception("Missing required parameter 'tags' when calling PetApi->findPetsByTags") - queryParams += "tags" -> tags.toString + if (tags == null) throw new Exception("Missing required parameter 'tags' when calling PetApi->findPetsByTags") + queryParams += "tags" -> tags.toString val resFuture = client.submit("GET", path, queryParams.toMap, headerParams.toMap, "") resFuture flatMap { resp => - process(reader.read(resp)) - } + process(reader.read(resp)) } + } - def getPetById(petId: Long)(implicit reader: ClientResponseReader[Pet]): Future[Pet] = { + def getPetById(petId: Long)(implicit reader: ClientResponseReader[Pet]): Future[Pet] = { // create path and map variables val path = (addFmt("/pet/{petId}") - replaceAll ("\\{" + "petId" + "\\}",petId.toString)) + replaceAll ("\\{" + "petId" + "\\}",petId.toString)) // query params val queryParams = new mutable.HashMap[String, String] @@ -368,11 +365,11 @@ class PetApiAsyncHelper(client: TransportClient, config: SwaggerConfig) extends val resFuture = client.submit("GET", path, queryParams.toMap, headerParams.toMap, "") resFuture flatMap { resp => - process(reader.read(resp)) - } + process(reader.read(resp)) } + } - def updatePet(body: Pet)(implicit reader: ClientResponseReader[Unit], writer: RequestWriter[Pet]): Future[Unit] = { + def updatePet(body: Pet)(implicit reader: ClientResponseReader[Unit], writer: RequestWriter[Pet]): Future[Unit] = { // create path and map variables val path = (addFmt("/pet")) @@ -380,21 +377,21 @@ class PetApiAsyncHelper(client: TransportClient, config: SwaggerConfig) extends val queryParams = new mutable.HashMap[String, String] val headerParams = new mutable.HashMap[String, String] - if (body == null) throw new Exception("Missing required parameter 'body' when calling PetApi->updatePet") + if (body == null) throw new Exception("Missing required parameter 'body' when calling PetApi->updatePet") val resFuture = client.submit("PUT", path, queryParams.toMap, headerParams.toMap, writer.write(body)) resFuture flatMap { resp => - process(reader.read(resp)) - } + process(reader.read(resp)) } + } - def updatePetWithForm(petId: Long, + def updatePetWithForm(petId: Long, name: Option[String] = None, status: Option[String] = None )(implicit reader: ClientResponseReader[Unit]): Future[Unit] = { // create path and map variables val path = (addFmt("/pet/{petId}") - replaceAll ("\\{" + "petId" + "\\}",petId.toString)) + replaceAll ("\\{" + "petId" + "\\}",petId.toString)) // query params val queryParams = new mutable.HashMap[String, String] @@ -403,17 +400,17 @@ class PetApiAsyncHelper(client: TransportClient, config: SwaggerConfig) extends val resFuture = client.submit("POST", path, queryParams.toMap, headerParams.toMap, "") resFuture flatMap { resp => - process(reader.read(resp)) - } + process(reader.read(resp)) } + } - def uploadFile(petId: Long, + def uploadFile(petId: Long, additionalMetadata: Option[String] = None, file: Option[File] = None )(implicit reader: ClientResponseReader[ApiResponse]): Future[ApiResponse] = { // create path and map variables val path = (addFmt("/pet/{petId}/uploadImage") - replaceAll ("\\{" + "petId" + "\\}",petId.toString)) + replaceAll ("\\{" + "petId" + "\\}",petId.toString)) // query params val queryParams = new mutable.HashMap[String, String] @@ -422,9 +419,9 @@ class PetApiAsyncHelper(client: TransportClient, config: SwaggerConfig) extends val resFuture = client.submit("POST", path, queryParams.toMap, headerParams.toMap, "") resFuture flatMap { resp => - process(reader.read(resp)) - } + process(reader.read(resp)) } + } } diff --git a/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/StoreApi.scala b/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/StoreApi.scala index f394221cf85..21225c35aea 100644 --- a/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/StoreApi.scala +++ b/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/StoreApi.scala @@ -53,11 +53,8 @@ class StoreApi(val defBasePath: String = "http://petstore.swagger.io/v2", def addHeader(key: String, value: String) = apiInvoker.defaultHeaders += key -> value - private[this] val client = transportClient - - protected def transportClient: TransportClient = new RestClient(config) - val config = SwaggerConfig.forUrl(new URI(defBasePath)) + val client = new RestClient(config) val helper = new StoreApiAsyncHelper(client, config) /** @@ -166,25 +163,25 @@ class StoreApi(val defBasePath: String = "http://petstore.swagger.io/v2", class StoreApiAsyncHelper(client: TransportClient, config: SwaggerConfig) extends ApiClient(client, config) { - def deleteOrder(orderId: String)(implicit reader: ClientResponseReader[Unit]): Future[Unit] = { + def deleteOrder(orderId: String)(implicit reader: ClientResponseReader[Unit]): Future[Unit] = { // create path and map variables val path = (addFmt("/store/order/{orderId}") - replaceAll ("\\{" + "orderId" + "\\}",orderId.toString)) + replaceAll ("\\{" + "orderId" + "\\}",orderId.toString)) // query params val queryParams = new mutable.HashMap[String, String] val headerParams = new mutable.HashMap[String, String] - if (orderId == null) throw new Exception("Missing required parameter 'orderId' when calling StoreApi->deleteOrder") + if (orderId == null) throw new Exception("Missing required parameter 'orderId' when calling StoreApi->deleteOrder") val resFuture = client.submit("DELETE", path, queryParams.toMap, headerParams.toMap, "") resFuture flatMap { resp => - process(reader.read(resp)) - } + process(reader.read(resp)) } + } - def getInventory()(implicit reader: ClientResponseReader[Map[String, Integer]]): Future[Map[String, Integer]] = { + def getInventory()(implicit reader: ClientResponseReader[Map[String, Integer]]): Future[Map[String, Integer]] = { // create path and map variables val path = (addFmt("/store/inventory")) @@ -195,14 +192,14 @@ class StoreApiAsyncHelper(client: TransportClient, config: SwaggerConfig) extend val resFuture = client.submit("GET", path, queryParams.toMap, headerParams.toMap, "") resFuture flatMap { resp => - process(reader.read(resp)) - } + process(reader.read(resp)) } + } - def getOrderById(orderId: Long)(implicit reader: ClientResponseReader[Order]): Future[Order] = { + def getOrderById(orderId: Long)(implicit reader: ClientResponseReader[Order]): Future[Order] = { // create path and map variables val path = (addFmt("/store/order/{orderId}") - replaceAll ("\\{" + "orderId" + "\\}",orderId.toString)) + replaceAll ("\\{" + "orderId" + "\\}",orderId.toString)) // query params val queryParams = new mutable.HashMap[String, String] @@ -211,11 +208,11 @@ class StoreApiAsyncHelper(client: TransportClient, config: SwaggerConfig) extend val resFuture = client.submit("GET", path, queryParams.toMap, headerParams.toMap, "") resFuture flatMap { resp => - process(reader.read(resp)) - } + process(reader.read(resp)) } + } - def placeOrder(body: Order)(implicit reader: ClientResponseReader[Order], writer: RequestWriter[Order]): Future[Order] = { + def placeOrder(body: Order)(implicit reader: ClientResponseReader[Order], writer: RequestWriter[Order]): Future[Order] = { // create path and map variables val path = (addFmt("/store/order")) @@ -223,13 +220,13 @@ class StoreApiAsyncHelper(client: TransportClient, config: SwaggerConfig) extend val queryParams = new mutable.HashMap[String, String] val headerParams = new mutable.HashMap[String, String] - if (body == null) throw new Exception("Missing required parameter 'body' when calling StoreApi->placeOrder") + if (body == null) throw new Exception("Missing required parameter 'body' when calling StoreApi->placeOrder") val resFuture = client.submit("POST", path, queryParams.toMap, headerParams.toMap, writer.write(body)) resFuture flatMap { resp => - process(reader.read(resp)) - } + process(reader.read(resp)) } + } } diff --git a/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/UserApi.scala b/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/UserApi.scala index 36fe21022a6..b7dfbe98bb6 100644 --- a/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/UserApi.scala +++ b/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/UserApi.scala @@ -53,11 +53,8 @@ class UserApi(val defBasePath: String = "http://petstore.swagger.io/v2", def addHeader(key: String, value: String) = apiInvoker.defaultHeaders += key -> value - private[this] val client = transportClient - - protected def transportClient: TransportClient = new RestClient(config) - val config = SwaggerConfig.forUrl(new URI(defBasePath)) + val client = new RestClient(config) val helper = new UserApiAsyncHelper(client, config) /** @@ -274,7 +271,7 @@ class UserApi(val defBasePath: String = "http://petstore.swagger.io/v2", class UserApiAsyncHelper(client: TransportClient, config: SwaggerConfig) extends ApiClient(client, config) { - def createUser(body: User)(implicit reader: ClientResponseReader[Unit], writer: RequestWriter[User]): Future[Unit] = { + def createUser(body: User)(implicit reader: ClientResponseReader[Unit], writer: RequestWriter[User]): Future[Unit] = { // create path and map variables val path = (addFmt("/user")) @@ -282,15 +279,15 @@ class UserApiAsyncHelper(client: TransportClient, config: SwaggerConfig) extends val queryParams = new mutable.HashMap[String, String] val headerParams = new mutable.HashMap[String, String] - if (body == null) throw new Exception("Missing required parameter 'body' when calling UserApi->createUser") + if (body == null) throw new Exception("Missing required parameter 'body' when calling UserApi->createUser") val resFuture = client.submit("POST", path, queryParams.toMap, headerParams.toMap, writer.write(body)) resFuture flatMap { resp => - process(reader.read(resp)) - } + process(reader.read(resp)) } + } - def createUsersWithArrayInput(body: List[User])(implicit reader: ClientResponseReader[Unit], writer: RequestWriter[List[User]]): Future[Unit] = { + def createUsersWithArrayInput(body: List[User])(implicit reader: ClientResponseReader[Unit], writer: RequestWriter[List[User]]): Future[Unit] = { // create path and map variables val path = (addFmt("/user/createWithArray")) @@ -298,15 +295,15 @@ class UserApiAsyncHelper(client: TransportClient, config: SwaggerConfig) extends val queryParams = new mutable.HashMap[String, String] val headerParams = new mutable.HashMap[String, String] - if (body == null) throw new Exception("Missing required parameter 'body' when calling UserApi->createUsersWithArrayInput") + if (body == null) throw new Exception("Missing required parameter 'body' when calling UserApi->createUsersWithArrayInput") val resFuture = client.submit("POST", path, queryParams.toMap, headerParams.toMap, writer.write(body)) resFuture flatMap { resp => - process(reader.read(resp)) - } + process(reader.read(resp)) } + } - def createUsersWithListInput(body: List[User])(implicit reader: ClientResponseReader[Unit], writer: RequestWriter[List[User]]): Future[Unit] = { + def createUsersWithListInput(body: List[User])(implicit reader: ClientResponseReader[Unit], writer: RequestWriter[List[User]]): Future[Unit] = { // create path and map variables val path = (addFmt("/user/createWithList")) @@ -314,51 +311,51 @@ class UserApiAsyncHelper(client: TransportClient, config: SwaggerConfig) extends val queryParams = new mutable.HashMap[String, String] val headerParams = new mutable.HashMap[String, String] - if (body == null) throw new Exception("Missing required parameter 'body' when calling UserApi->createUsersWithListInput") + if (body == null) throw new Exception("Missing required parameter 'body' when calling UserApi->createUsersWithListInput") val resFuture = client.submit("POST", path, queryParams.toMap, headerParams.toMap, writer.write(body)) resFuture flatMap { resp => - process(reader.read(resp)) - } + process(reader.read(resp)) } + } - def deleteUser(username: String)(implicit reader: ClientResponseReader[Unit]): Future[Unit] = { + def deleteUser(username: String)(implicit reader: ClientResponseReader[Unit]): Future[Unit] = { // create path and map variables val path = (addFmt("/user/{username}") - replaceAll ("\\{" + "username" + "\\}",username.toString)) + replaceAll ("\\{" + "username" + "\\}",username.toString)) // query params val queryParams = new mutable.HashMap[String, String] val headerParams = new mutable.HashMap[String, String] - if (username == null) throw new Exception("Missing required parameter 'username' when calling UserApi->deleteUser") + if (username == null) throw new Exception("Missing required parameter 'username' when calling UserApi->deleteUser") val resFuture = client.submit("DELETE", path, queryParams.toMap, headerParams.toMap, "") resFuture flatMap { resp => - process(reader.read(resp)) - } + process(reader.read(resp)) } + } - def getUserByName(username: String)(implicit reader: ClientResponseReader[User]): Future[User] = { + def getUserByName(username: String)(implicit reader: ClientResponseReader[User]): Future[User] = { // create path and map variables val path = (addFmt("/user/{username}") - replaceAll ("\\{" + "username" + "\\}",username.toString)) + replaceAll ("\\{" + "username" + "\\}",username.toString)) // query params val queryParams = new mutable.HashMap[String, String] val headerParams = new mutable.HashMap[String, String] - if (username == null) throw new Exception("Missing required parameter 'username' when calling UserApi->getUserByName") + if (username == null) throw new Exception("Missing required parameter 'username' when calling UserApi->getUserByName") val resFuture = client.submit("GET", path, queryParams.toMap, headerParams.toMap, "") resFuture flatMap { resp => - process(reader.read(resp)) - } + process(reader.read(resp)) } + } - def loginUser(username: String, + def loginUser(username: String, password: String)(implicit reader: ClientResponseReader[String]): Future[String] = { // create path and map variables val path = (addFmt("/user/login")) @@ -367,20 +364,20 @@ class UserApiAsyncHelper(client: TransportClient, config: SwaggerConfig) extends val queryParams = new mutable.HashMap[String, String] val headerParams = new mutable.HashMap[String, String] - if (username == null) throw new Exception("Missing required parameter 'username' when calling UserApi->loginUser") + if (username == null) throw new Exception("Missing required parameter 'username' when calling UserApi->loginUser") - if (password == null) throw new Exception("Missing required parameter 'password' when calling UserApi->loginUser") + if (password == null) throw new Exception("Missing required parameter 'password' when calling UserApi->loginUser") - queryParams += "username" -> username.toString - queryParams += "password" -> password.toString + queryParams += "username" -> username.toString + queryParams += "password" -> password.toString val resFuture = client.submit("GET", path, queryParams.toMap, headerParams.toMap, "") resFuture flatMap { resp => - process(reader.read(resp)) - } + process(reader.read(resp)) } + } - def logoutUser()(implicit reader: ClientResponseReader[Unit]): Future[Unit] = { + def logoutUser()(implicit reader: ClientResponseReader[Unit]): Future[Unit] = { // create path and map variables val path = (addFmt("/user/logout")) @@ -391,29 +388,29 @@ class UserApiAsyncHelper(client: TransportClient, config: SwaggerConfig) extends val resFuture = client.submit("GET", path, queryParams.toMap, headerParams.toMap, "") resFuture flatMap { resp => - process(reader.read(resp)) - } + process(reader.read(resp)) } + } - def updateUser(username: String, + def updateUser(username: String, body: User)(implicit reader: ClientResponseReader[Unit], writer: RequestWriter[User]): Future[Unit] = { // create path and map variables val path = (addFmt("/user/{username}") - replaceAll ("\\{" + "username" + "\\}",username.toString)) + replaceAll ("\\{" + "username" + "\\}",username.toString)) // query params val queryParams = new mutable.HashMap[String, String] val headerParams = new mutable.HashMap[String, String] - if (username == null) throw new Exception("Missing required parameter 'username' when calling UserApi->updateUser") + if (username == null) throw new Exception("Missing required parameter 'username' when calling UserApi->updateUser") - if (body == null) throw new Exception("Missing required parameter 'body' when calling UserApi->updateUser") + if (body == null) throw new Exception("Missing required parameter 'body' when calling UserApi->updateUser") val resFuture = client.submit("PUT", path, queryParams.toMap, headerParams.toMap, writer.write(body)) resFuture flatMap { resp => - process(reader.read(resp)) - } + process(reader.read(resp)) } + } } From c224c8d81c683e994174a75e75ce77ef42e7b205 Mon Sep 17 00:00:00 2001 From: clasnake Date: Sat, 25 Feb 2017 09:57:44 +0800 Subject: [PATCH 3/6] Modify build.sbt. --- .../src/main/resources/scala/api.mustache | 12 +- .../main/resources/scala/build.sbt.mustache | 68 +++++------ samples/client/petstore/scala/build.sbt | 68 +++++------ .../scala/io/swagger/client/api/PetApi.scala | 106 +++++++++--------- .../io/swagger/client/api/StoreApi.scala | 46 ++++---- .../scala/io/swagger/client/api/UserApi.scala | 98 ++++++++-------- 6 files changed, 199 insertions(+), 199 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/scala/api.mustache b/modules/swagger-codegen/src/main/resources/scala/api.mustache index 6cb5869953d..22fbfa3125a 100644 --- a/modules/swagger-codegen/src/main/resources/scala/api.mustache +++ b/modules/swagger-codegen/src/main/resources/scala/api.mustache @@ -64,12 +64,12 @@ class {{classname}}(val defBasePath: String = "{{basePath}}", } - /** - * {{summary}} asynchronously - * {{notes}} - {{#allParams}} * @param {{paramName}} {{description}} {{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}} - {{/allParams}} * @return Future({{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}) - */ + /** + * {{summary}} asynchronously + * {{notes}} +{{#allParams}} * @param {{paramName}} {{description}} {{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}} +{{/allParams}} * @return Future({{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}) + */ def {{operationId}}Async({{#allParams}}{{paramName}}: {{#required}}{{dataType}}{{#defaultValue}} /* = {{{defaultValue}}}*/{{/defaultValue}}{{/required}}{{^required}}Option[{{dataType}}]{{#defaultValue}} /* = {{{defaultValue}}}*/{{/defaultValue}}{{^defaultValue}} = None{{/defaultValue}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}){{#returnType}}: Future[{{returnType}}]{{/returnType}} = { helper.{{operationId}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) } diff --git a/modules/swagger-codegen/src/main/resources/scala/build.sbt.mustache b/modules/swagger-codegen/src/main/resources/scala/build.sbt.mustache index daf4b6ae94d..24ee01b6fd5 100644 --- a/modules/swagger-codegen/src/main/resources/scala/build.sbt.mustache +++ b/modules/swagger-codegen/src/main/resources/scala/build.sbt.mustache @@ -1,34 +1,34 @@ -lazy val root = (project in file(".")). - settings( - version := "{{artifactVersion}}", - name := "{{artifactId}}", - organization := "{{groupId}}", - scalaVersion := "2.11.8", - - libraryDependencies ++= Seq( - "com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.4.2", - "com.sun.jersey" % "jersey-core" % "1.19", - "com.sun.jersey" % "jersey-client" % "1.19", - "com.sun.jersey.contribs" % "jersey-multipart" % "1.19", - "org.jfarcand" % "jersey-ahc-client" % "1.0.5", - "io.swagger" % "swagger-core" % "1.5.8", - "joda-time" % "joda-time" % "2.2", - "org.joda" % "joda-convert" % "1.2", - "org.scalatest" %% "scalatest" % "2.2.4" % "test", - "junit" % "junit" % "4.8.1" % "test", - "com.wordnik.swagger" %% "swagger-async-httpclient" % "0.3.5" - ), - - resolvers ++= Seq( - Resolver.jcenterRepo, - Resolver.mavenLocal - ), - - scalacOptions := Seq( - "-unchecked", - "-deprecation", - "-feature" - ), - - publishArtifact in (Compile, packageDoc) := false - ) \ No newline at end of file +version := "{{artifactVersion}}" + +name := "{{artifactId}}" + +organization := "{{groupId}}" + +scalaVersion := "2.11.8" + +libraryDependencies ++= Seq( + "com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.4.2", + "com.sun.jersey" % "jersey-core" % "1.19", + "com.sun.jersey" % "jersey-client" % "1.19", + "com.sun.jersey.contribs" % "jersey-multipart" % "1.19", + "org.jfarcand" % "jersey-ahc-client" % "1.0.5", + "io.swagger" % "swagger-core" % "1.5.8", + "joda-time" % "joda-time" % "2.2", + "org.joda" % "joda-convert" % "1.2", + "org.scalatest" %% "scalatest" % "2.2.4" % "test", + "junit" % "junit" % "4.8.1" % "test", + "com.wordnik.swagger" %% "swagger-async-httpclient" % "0.3.5" +) + +resolvers ++= Seq( + Resolver.mavenLocal +) + +scalacOptions := Seq( + "-unchecked", + "-deprecation", + "-feature" +) + +publishArtifact in (Compile, packageDoc) := false + diff --git a/samples/client/petstore/scala/build.sbt b/samples/client/petstore/scala/build.sbt index 005d7aeca1b..bececaf181b 100644 --- a/samples/client/petstore/scala/build.sbt +++ b/samples/client/petstore/scala/build.sbt @@ -1,34 +1,34 @@ -lazy val root = (project in file(".")). - settings( - version := "1.0.0", - name := "swagger-scala-client", - organization := "io.swagger", - scalaVersion := "2.11.8", - - libraryDependencies ++= Seq( - "com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.4.2", - "com.sun.jersey" % "jersey-core" % "1.19", - "com.sun.jersey" % "jersey-client" % "1.19", - "com.sun.jersey.contribs" % "jersey-multipart" % "1.19", - "org.jfarcand" % "jersey-ahc-client" % "1.0.5", - "io.swagger" % "swagger-core" % "1.5.8", - "joda-time" % "joda-time" % "2.2", - "org.joda" % "joda-convert" % "1.2", - "org.scalatest" %% "scalatest" % "2.2.4" % "test", - "junit" % "junit" % "4.8.1" % "test", - "com.wordnik.swagger" %% "swagger-async-httpclient" % "0.3.5" - ), - - resolvers ++= Seq( - Resolver.jcenterRepo, - Resolver.mavenLocal - ), - - scalacOptions := Seq( - "-unchecked", - "-deprecation", - "-feature" - ), - - publishArtifact in (Compile, packageDoc) := false - ) \ No newline at end of file +version := "1.0.0" + +name := "swagger-scala-client" + +organization := "io.swagger" + +scalaVersion := "2.11.8" + +libraryDependencies ++= Seq( + "com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.4.2", + "com.sun.jersey" % "jersey-core" % "1.19", + "com.sun.jersey" % "jersey-client" % "1.19", + "com.sun.jersey.contribs" % "jersey-multipart" % "1.19", + "org.jfarcand" % "jersey-ahc-client" % "1.0.5", + "io.swagger" % "swagger-core" % "1.5.8", + "joda-time" % "joda-time" % "2.2", + "org.joda" % "joda-convert" % "1.2", + "org.scalatest" %% "scalatest" % "2.2.4" % "test", + "junit" % "junit" % "4.8.1" % "test", + "com.wordnik.swagger" %% "swagger-async-httpclient" % "0.3.5" +) + +resolvers ++= Seq( + Resolver.mavenLocal +) + +scalacOptions := Seq( + "-unchecked", + "-deprecation", + "-feature" +) + +publishArtifact in (Compile, packageDoc) := false + diff --git a/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/PetApi.scala b/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/PetApi.scala index 61b1624569c..a7c840b4276 100644 --- a/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/PetApi.scala +++ b/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/PetApi.scala @@ -74,12 +74,12 @@ class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2", } - /** - * Add a new pet to the store asynchronously - * - * @param body Pet object that needs to be added to the store - * @return Future(void) - */ + /** + * Add a new pet to the store asynchronously + * + * @param body Pet object that needs to be added to the store + * @return Future(void) + */ def addPetAsync(body: Pet) = { helper.addPet(body) } @@ -101,13 +101,13 @@ class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2", } - /** - * Deletes a pet asynchronously - * - * @param petId Pet id to delete - * @param apiKey (optional) - * @return Future(void) - */ + /** + * Deletes a pet asynchronously + * + * @param petId Pet id to delete + * @param apiKey (optional) + * @return Future(void) + */ def deletePetAsync(petId: Long, apiKey: Option[String] = None) = { helper.deletePet(petId, apiKey) } @@ -128,12 +128,12 @@ class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2", } - /** - * Finds Pets by status asynchronously - * Multiple status values can be provided with comma separated strings - * @param status Status values that need to be considered for filter - * @return Future(List[Pet]) - */ + /** + * Finds Pets by status asynchronously + * Multiple status values can be provided with comma separated strings + * @param status Status values that need to be considered for filter + * @return Future(List[Pet]) + */ def findPetsByStatusAsync(status: List[String]): Future[List[Pet]] = { helper.findPetsByStatus(status) } @@ -154,12 +154,12 @@ class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2", } - /** - * Finds Pets by tags asynchronously - * Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. - * @param tags Tags to filter by - * @return Future(List[Pet]) - */ + /** + * Finds Pets by tags asynchronously + * Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. + * @param tags Tags to filter by + * @return Future(List[Pet]) + */ def findPetsByTagsAsync(tags: List[String]): Future[List[Pet]] = { helper.findPetsByTags(tags) } @@ -180,12 +180,12 @@ class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2", } - /** - * Find pet by ID asynchronously - * Returns a single pet - * @param petId ID of pet to return - * @return Future(Pet) - */ + /** + * Find pet by ID asynchronously + * Returns a single pet + * @param petId ID of pet to return + * @return Future(Pet) + */ def getPetByIdAsync(petId: Long): Future[Pet] = { helper.getPetById(petId) } @@ -206,12 +206,12 @@ class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2", } - /** - * Update an existing pet asynchronously - * - * @param body Pet object that needs to be added to the store - * @return Future(void) - */ + /** + * Update an existing pet asynchronously + * + * @param body Pet object that needs to be added to the store + * @return Future(void) + */ def updatePetAsync(body: Pet) = { helper.updatePet(body) } @@ -234,14 +234,14 @@ class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2", } - /** - * Updates a pet in the store with form data asynchronously - * - * @param petId ID of pet that needs to be updated - * @param name Updated name of the pet (optional) - * @param status Updated status of the pet (optional) - * @return Future(void) - */ + /** + * Updates a pet in the store with form data asynchronously + * + * @param petId ID of pet that needs to be updated + * @param name Updated name of the pet (optional) + * @param status Updated status of the pet (optional) + * @return Future(void) + */ def updatePetWithFormAsync(petId: Long, name: Option[String] = None, status: Option[String] = None) = { helper.updatePetWithForm(petId, name, status) } @@ -264,14 +264,14 @@ class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2", } - /** - * uploads an image asynchronously - * - * @param petId ID of pet to update - * @param additionalMetadata Additional data to pass to server (optional) - * @param file file to upload (optional) - * @return Future(ApiResponse) - */ + /** + * uploads an image asynchronously + * + * @param petId ID of pet to update + * @param additionalMetadata Additional data to pass to server (optional) + * @param file file to upload (optional) + * @return Future(ApiResponse) + */ def uploadFileAsync(petId: Long, additionalMetadata: Option[String] = None, file: Option[File] = None): Future[ApiResponse] = { helper.uploadFile(petId, additionalMetadata, file) } diff --git a/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/StoreApi.scala b/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/StoreApi.scala index 21225c35aea..6713f5d16f9 100644 --- a/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/StoreApi.scala +++ b/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/StoreApi.scala @@ -72,12 +72,12 @@ class StoreApi(val defBasePath: String = "http://petstore.swagger.io/v2", } - /** - * Delete purchase order by ID asynchronously - * For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors - * @param orderId ID of the order that needs to be deleted - * @return Future(void) - */ + /** + * Delete purchase order by ID asynchronously + * For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors + * @param orderId ID of the order that needs to be deleted + * @return Future(void) + */ def deleteOrderAsync(orderId: String) = { helper.deleteOrder(orderId) } @@ -97,11 +97,11 @@ class StoreApi(val defBasePath: String = "http://petstore.swagger.io/v2", } - /** - * Returns pet inventories by status asynchronously - * Returns a map of status codes to quantities - * @return Future(Map[String, Integer]) - */ + /** + * Returns pet inventories by status asynchronously + * Returns a map of status codes to quantities + * @return Future(Map[String, Integer]) + */ def getInventoryAsync(): Future[Map[String, Integer]] = { helper.getInventory() } @@ -122,12 +122,12 @@ class StoreApi(val defBasePath: String = "http://petstore.swagger.io/v2", } - /** - * Find purchase order by ID asynchronously - * For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions - * @param orderId ID of pet that needs to be fetched - * @return Future(Order) - */ + /** + * Find purchase order by ID asynchronously + * For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions + * @param orderId ID of pet that needs to be fetched + * @return Future(Order) + */ def getOrderByIdAsync(orderId: Long): Future[Order] = { helper.getOrderById(orderId) } @@ -148,12 +148,12 @@ class StoreApi(val defBasePath: String = "http://petstore.swagger.io/v2", } - /** - * Place an order for a pet asynchronously - * - * @param body order placed for purchasing the pet - * @return Future(Order) - */ + /** + * Place an order for a pet asynchronously + * + * @param body order placed for purchasing the pet + * @return Future(Order) + */ def placeOrderAsync(body: Order): Future[Order] = { helper.placeOrder(body) } diff --git a/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/UserApi.scala b/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/UserApi.scala index b7dfbe98bb6..9249989a579 100644 --- a/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/UserApi.scala +++ b/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/UserApi.scala @@ -72,12 +72,12 @@ class UserApi(val defBasePath: String = "http://petstore.swagger.io/v2", } - /** - * Create user asynchronously - * This can only be done by the logged in user. - * @param body Created user object - * @return Future(void) - */ + /** + * Create user asynchronously + * This can only be done by the logged in user. + * @param body Created user object + * @return Future(void) + */ def createUserAsync(body: User) = { helper.createUser(body) } @@ -98,12 +98,12 @@ class UserApi(val defBasePath: String = "http://petstore.swagger.io/v2", } - /** - * Creates list of users with given input array asynchronously - * - * @param body List of user object - * @return Future(void) - */ + /** + * Creates list of users with given input array asynchronously + * + * @param body List of user object + * @return Future(void) + */ def createUsersWithArrayInputAsync(body: List[User]) = { helper.createUsersWithArrayInput(body) } @@ -124,12 +124,12 @@ class UserApi(val defBasePath: String = "http://petstore.swagger.io/v2", } - /** - * Creates list of users with given input array asynchronously - * - * @param body List of user object - * @return Future(void) - */ + /** + * Creates list of users with given input array asynchronously + * + * @param body List of user object + * @return Future(void) + */ def createUsersWithListInputAsync(body: List[User]) = { helper.createUsersWithListInput(body) } @@ -150,12 +150,12 @@ class UserApi(val defBasePath: String = "http://petstore.swagger.io/v2", } - /** - * Delete user asynchronously - * This can only be done by the logged in user. - * @param username The name that needs to be deleted - * @return Future(void) - */ + /** + * Delete user asynchronously + * This can only be done by the logged in user. + * @param username The name that needs to be deleted + * @return Future(void) + */ def deleteUserAsync(username: String) = { helper.deleteUser(username) } @@ -176,12 +176,12 @@ class UserApi(val defBasePath: String = "http://petstore.swagger.io/v2", } - /** - * Get user by user name asynchronously - * - * @param username The name that needs to be fetched. Use user1 for testing. - * @return Future(User) - */ + /** + * Get user by user name asynchronously + * + * @param username The name that needs to be fetched. Use user1 for testing. + * @return Future(User) + */ def getUserByNameAsync(username: String): Future[User] = { helper.getUserByName(username) } @@ -203,13 +203,13 @@ class UserApi(val defBasePath: String = "http://petstore.swagger.io/v2", } - /** - * Logs user into the system asynchronously - * - * @param username The user name for login - * @param password The password for login in clear text - * @return Future(String) - */ + /** + * Logs user into the system asynchronously + * + * @param username The user name for login + * @param password The password for login in clear text + * @return Future(String) + */ def loginUserAsync(username: String, password: String): Future[String] = { helper.loginUser(username, password) } @@ -229,11 +229,11 @@ class UserApi(val defBasePath: String = "http://petstore.swagger.io/v2", } - /** - * Logs out current logged in user session asynchronously - * - * @return Future(void) - */ + /** + * Logs out current logged in user session asynchronously + * + * @return Future(void) + */ def logoutUserAsync() = { helper.logoutUser() } @@ -255,13 +255,13 @@ class UserApi(val defBasePath: String = "http://petstore.swagger.io/v2", } - /** - * Updated user asynchronously - * This can only be done by the logged in user. - * @param username name that need to be deleted - * @param body Updated user object - * @return Future(void) - */ + /** + * Updated user asynchronously + * This can only be done by the logged in user. + * @param username name that need to be deleted + * @param body Updated user object + * @return Future(void) + */ def updateUserAsync(username: String, body: User) = { helper.updateUser(username, body) } From 564a1f13fb7ca9d409e766b870943c2c4e29ac17 Mon Sep 17 00:00:00 2001 From: clasnake Date: Sat, 25 Feb 2017 15:26:47 +0800 Subject: [PATCH 4/6] Change date format and pass StoreApis tests. --- .../codegen/languages/ScalaClientCodegen.java | 11 +++++---- .../src/main/resources/scala/api.mustache | 10 ++++---- .../swagger/codegen/scala/ScalaModelTest.java | 4 ++-- .../typescript-angular2/model/ModelReturn.ts | 24 ------------------- .../scala/io/swagger/client/api/PetApi.scala | 10 ++++---- .../io/swagger/client/api/StoreApi.scala | 10 ++++---- .../scala/io/swagger/client/api/UserApi.scala | 10 ++++---- .../scala/io/swagger/client/model/Order.scala | 10 ++++---- .../scala/src/test/scala/StoreApiTest.scala | 17 ++++++------- 9 files changed, 47 insertions(+), 59 deletions(-) delete mode 100644 samples/client/petstore-security-test/typescript-angular2/model/ModelReturn.ts diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/ScalaClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/ScalaClientCodegen.java index e3c3873e8be..3efd6dcd101 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/ScalaClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/ScalaClientCodegen.java @@ -9,6 +9,7 @@ import java.io.File; import java.util.Arrays; import java.util.HashMap; +import java.util.Map; import org.apache.commons.lang3.StringUtils; @@ -79,8 +80,8 @@ public ScalaClientCodegen() { importMapping.remove("Set"); importMapping.remove("Map"); - importMapping.put("DateTime", "org.joda.time.DateTime"); - importMapping.put("ListBuffer", "scala.collection.mutable.ListBuffer"); + importMapping.put("Date", "java.util.Date"); + importMapping.put("ListBuffer", "scala.collections.mutable.ListBuffer"); typeMapping = new HashMap(); typeMapping.put("enum", "NSString"); @@ -94,7 +95,6 @@ public ScalaClientCodegen() { typeMapping.put("byte", "Byte"); typeMapping.put("short", "Short"); typeMapping.put("char", "Char"); - typeMapping.put("long", "Long"); typeMapping.put("double", "Double"); typeMapping.put("object", "Any"); typeMapping.put("file", "File"); @@ -102,6 +102,10 @@ public ScalaClientCodegen() { // mapped to String as a workaround typeMapping.put("binary", "String"); typeMapping.put("ByteArray", "String"); + typeMapping.put("date-time", "Date"); +// typeMapping.put("date", "Date"); +// typeMapping.put("Date", "Date"); + typeMapping.put("DateTime", "Date"); instantiationTypes.put("array", "ListBuffer"); instantiationTypes.put("map", "HashMap"); @@ -112,7 +116,6 @@ public ScalaClientCodegen() { @Override public void processOpts() { super.processOpts(); - if (additionalProperties.containsKey(CodegenConstants.MODEL_PROPERTY_NAMING)) { setModelPropertyNaming((String) additionalProperties.get(CodegenConstants.MODEL_PROPERTY_NAMING)); } diff --git a/modules/swagger-codegen/src/main/resources/scala/api.mustache b/modules/swagger-codegen/src/main/resources/scala/api.mustache index 22fbfa3125a..a50640ba2e4 100644 --- a/modules/swagger-codegen/src/main/resources/scala/api.mustache +++ b/modules/swagger-codegen/src/main/resources/scala/api.mustache @@ -1,11 +1,11 @@ {{>licenseInfo}} package {{package}} +import java.text.SimpleDateFormat + {{#imports}}import {{import}} {{/imports}} -import {{invokerPackage}}.ApiInvoker -import {{invokerPackage}}.ApiException -import {{invokerPackage}}.AsyncClient +import {{invokerPackage}}.{ApiInvoker, ApiException} import com.sun.jersey.multipart.FormDataMultiPart import com.sun.jersey.multipart.file.FileDataBodyPart @@ -35,7 +35,9 @@ import scala.util.{Failure, Success, Try} class {{classname}}(val defBasePath: String = "{{basePath}}", defApiInvoker: ApiInvoker = ApiInvoker) { - implicit val formats = org.json4s.DefaultFormats + implicit val formats = new org.json4s.DefaultFormats { + override def dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+0000") + } implicit val reader = JsonFormatsReader implicit val writer = JsonFormatsWriter diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/scala/ScalaModelTest.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/scala/ScalaModelTest.java index 95e08c6b362..57b9023b077 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/scala/ScalaModelTest.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/scala/ScalaModelTest.java @@ -66,10 +66,10 @@ public void simpleModelTest() { Assert.assertEquals(property3.baseName, "createdAt"); Assert.assertEquals(property3.getter, "getCreatedAt"); Assert.assertEquals(property3.setter, "setCreatedAt"); - Assert.assertEquals(property3.datatype, "DateTime"); + Assert.assertEquals(property3.datatype, "Date"); Assert.assertEquals(property3.name, "createdAt"); Assert.assertEquals(property3.defaultValue, "null"); - Assert.assertEquals(property3.baseType, "DateTime"); + Assert.assertEquals(property3.baseType, "Date"); Assert.assertFalse(property3.hasMore); Assert.assertFalse(property3.required); Assert.assertTrue(property3.isNotContainer); diff --git a/samples/client/petstore-security-test/typescript-angular2/model/ModelReturn.ts b/samples/client/petstore-security-test/typescript-angular2/model/ModelReturn.ts deleted file mode 100644 index 5e9e114be05..00000000000 --- a/samples/client/petstore-security-test/typescript-angular2/model/ModelReturn.ts +++ /dev/null @@ -1,24 +0,0 @@ -/** - * Swagger Petstore *_/ ' \" =end -- \\r\\n \\n \\r - * This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ *_/ ' \" =end -- - * - * OpenAPI spec version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r - * Contact: apiteam@swagger.io *_/ ' \" =end -- \\r\\n \\n \\r - * - * NOTE: This class is auto generated by the swagger code generator program. - * https://github.com/swagger-api/swagger-codegen.git - * Do not edit the class manually. - */ - -import * as models from './models'; - -/** - * Model for testing reserved words *_/ ' \" =end -- \\r\\n \\n \\r - */ -export interface ModelReturn { - /** - * property description *_/ ' \" =end -- \\r\\n \\n \\r - */ - return?: number; - -} diff --git a/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/PetApi.scala b/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/PetApi.scala index a7c840b4276..823d49bfa5e 100644 --- a/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/PetApi.scala +++ b/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/PetApi.scala @@ -12,12 +12,12 @@ package io.swagger.client.api +import java.text.SimpleDateFormat + import io.swagger.client.model.ApiResponse import java.io.File import io.swagger.client.model.Pet -import io.swagger.client.ApiInvoker -import io.swagger.client.ApiException -import io.swagger.client.AsyncClient +import io.swagger.client.{ApiInvoker, ApiException} import com.sun.jersey.multipart.FormDataMultiPart import com.sun.jersey.multipart.file.FileDataBodyPart @@ -46,7 +46,9 @@ import scala.util.{Failure, Success, Try} class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2", defApiInvoker: ApiInvoker = ApiInvoker) { - implicit val formats = org.json4s.DefaultFormats + implicit val formats = new org.json4s.DefaultFormats { + override def dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+0000") + } implicit val reader = JsonFormatsReader implicit val writer = JsonFormatsWriter diff --git a/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/StoreApi.scala b/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/StoreApi.scala index 6713f5d16f9..7c2fa6445e4 100644 --- a/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/StoreApi.scala +++ b/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/StoreApi.scala @@ -12,10 +12,10 @@ package io.swagger.client.api +import java.text.SimpleDateFormat + import io.swagger.client.model.Order -import io.swagger.client.ApiInvoker -import io.swagger.client.ApiException -import io.swagger.client.AsyncClient +import io.swagger.client.{ApiInvoker, ApiException} import com.sun.jersey.multipart.FormDataMultiPart import com.sun.jersey.multipart.file.FileDataBodyPart @@ -44,7 +44,9 @@ import scala.util.{Failure, Success, Try} class StoreApi(val defBasePath: String = "http://petstore.swagger.io/v2", defApiInvoker: ApiInvoker = ApiInvoker) { - implicit val formats = org.json4s.DefaultFormats + implicit val formats = new org.json4s.DefaultFormats { + override def dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+0000") + } implicit val reader = JsonFormatsReader implicit val writer = JsonFormatsWriter diff --git a/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/UserApi.scala b/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/UserApi.scala index 9249989a579..fddc89aa197 100644 --- a/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/UserApi.scala +++ b/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/UserApi.scala @@ -12,10 +12,10 @@ package io.swagger.client.api +import java.text.SimpleDateFormat + import io.swagger.client.model.User -import io.swagger.client.ApiInvoker -import io.swagger.client.ApiException -import io.swagger.client.AsyncClient +import io.swagger.client.{ApiInvoker, ApiException} import com.sun.jersey.multipart.FormDataMultiPart import com.sun.jersey.multipart.file.FileDataBodyPart @@ -44,7 +44,9 @@ import scala.util.{Failure, Success, Try} class UserApi(val defBasePath: String = "http://petstore.swagger.io/v2", defApiInvoker: ApiInvoker = ApiInvoker) { - implicit val formats = org.json4s.DefaultFormats + implicit val formats = new org.json4s.DefaultFormats { + override def dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+0000") + } implicit val reader = JsonFormatsReader implicit val writer = JsonFormatsWriter diff --git a/samples/client/petstore/scala/src/main/scala/io/swagger/client/model/Order.scala b/samples/client/petstore/scala/src/main/scala/io/swagger/client/model/Order.scala index a88c0ec23d9..086521285f9 100644 --- a/samples/client/petstore/scala/src/main/scala/io/swagger/client/model/Order.scala +++ b/samples/client/petstore/scala/src/main/scala/io/swagger/client/model/Order.scala @@ -12,13 +12,13 @@ package io.swagger.client.model -import org.joda.time.DateTime +import java.util.Date case class Order ( - id: Option[Long], - petId: Option[Long], - quantity: Option[Integer], - shipDate: Option[DateTime], + id: Long, + petId: Long, + quantity: Integer, + shipDate: Date, /* Order Status */ status: Option[String], complete: Option[Boolean] diff --git a/samples/client/petstore/scala/src/test/scala/StoreApiTest.scala b/samples/client/petstore/scala/src/test/scala/StoreApiTest.scala index 4c1a06f1cb6..826f711f47d 100644 --- a/samples/client/petstore/scala/src/test/scala/StoreApiTest.scala +++ b/samples/client/petstore/scala/src/test/scala/StoreApiTest.scala @@ -1,7 +1,8 @@ import io.swagger.client._ import io.swagger.client.api._ import io.swagger.client.model._ - +import org.joda.time.DateTime + import org.junit.runner.RunWith import org.scalatest.junit.JUnitRunner import org.scalatest._ @@ -9,6 +10,7 @@ import org.scalatest._ import scala.collection.mutable.{ ListBuffer, HashMap } import scala.collection.JavaConverters._ import scala.beans.BeanProperty +import java.util.Date @RunWith(classOf[JUnitRunner]) class StoreApiTest extends FlatSpec with Matchers { @@ -18,7 +20,7 @@ class StoreApiTest extends FlatSpec with Matchers { api.apiInvoker.defaultHeaders += "api_key" -> "special-key" it should "place and fetch an order" in { - val now = new org.joda.time.DateTime + val now = new Date() val order = Order( petId = Some(10), id = Some(1000), @@ -31,18 +33,17 @@ class StoreApiTest extends FlatSpec with Matchers { api.getOrderById(1000) match { case Some(order) => { - order.id should be(Some(1000)) - order.petId should be(Some(10)) - order.quantity should be(Some(101)) - // use `getMillis` to compare across timezones - order.shipDate.get.getMillis.equals(now.getMillis) should be(true) + order.id should be(1000) + order.petId should be(10) + order.quantity should be(101) + order.shipDate.getTime().equals(now.getTime()) should be(true) } case None => fail("didn't find order created") } } it should "delete an order" in { - val now = new org.joda.time.DateTime + val now = new Date() val order = Order( id = Some(1001), petId = Some(10), From 386ae73bfc5ec7013f89a4f4cfffe91dce5ba066 Mon Sep 17 00:00:00 2001 From: clasnake Date: Sat, 25 Feb 2017 16:02:08 +0800 Subject: [PATCH 5/6] Add StringReader and StringWriter to support string serialization/deserialization. --- .../swagger-codegen/src/main/resources/scala/api.mustache | 6 ++++-- .../scala/src/main/scala/io/swagger/client/api/PetApi.scala | 6 ++++-- .../src/main/scala/io/swagger/client/api/StoreApi.scala | 6 ++++-- .../src/main/scala/io/swagger/client/api/UserApi.scala | 6 ++++-- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/scala/api.mustache b/modules/swagger-codegen/src/main/resources/scala/api.mustache index a50640ba2e4..b83e4de8b5b 100644 --- a/modules/swagger-codegen/src/main/resources/scala/api.mustache +++ b/modules/swagger-codegen/src/main/resources/scala/api.mustache @@ -38,8 +38,10 @@ class {{classname}}(val defBasePath: String = "{{basePath}}", implicit val formats = new org.json4s.DefaultFormats { override def dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+0000") } - implicit val reader = JsonFormatsReader - implicit val writer = JsonFormatsWriter + implicit val stringReader = ClientResponseReaders.StringReader + implicit val jsonReader = JsonFormatsReader + implicit val stringWriter = RequestWriters.StringWriter + implicit val jsonWriter = JsonFormatsWriter var basePath = defBasePath var apiInvoker = defApiInvoker diff --git a/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/PetApi.scala b/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/PetApi.scala index 823d49bfa5e..69717d60b1d 100644 --- a/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/PetApi.scala +++ b/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/PetApi.scala @@ -49,8 +49,10 @@ class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2", implicit val formats = new org.json4s.DefaultFormats { override def dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+0000") } - implicit val reader = JsonFormatsReader - implicit val writer = JsonFormatsWriter + implicit val stringReader = ClientResponseReaders.StringReader + implicit val jsonReader = JsonFormatsReader + implicit val stringWriter = RequestWriters.StringWriter + implicit val jsonWriter = JsonFormatsWriter var basePath = defBasePath var apiInvoker = defApiInvoker diff --git a/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/StoreApi.scala b/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/StoreApi.scala index 7c2fa6445e4..3d6b30ffcca 100644 --- a/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/StoreApi.scala +++ b/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/StoreApi.scala @@ -47,8 +47,10 @@ class StoreApi(val defBasePath: String = "http://petstore.swagger.io/v2", implicit val formats = new org.json4s.DefaultFormats { override def dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+0000") } - implicit val reader = JsonFormatsReader - implicit val writer = JsonFormatsWriter + implicit val stringReader = ClientResponseReaders.StringReader + implicit val jsonReader = JsonFormatsReader + implicit val stringWriter = RequestWriters.StringWriter + implicit val jsonWriter = JsonFormatsWriter var basePath = defBasePath var apiInvoker = defApiInvoker diff --git a/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/UserApi.scala b/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/UserApi.scala index fddc89aa197..ea2bb4d8b2a 100644 --- a/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/UserApi.scala +++ b/samples/client/petstore/scala/src/main/scala/io/swagger/client/api/UserApi.scala @@ -47,8 +47,10 @@ class UserApi(val defBasePath: String = "http://petstore.swagger.io/v2", implicit val formats = new org.json4s.DefaultFormats { override def dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+0000") } - implicit val reader = JsonFormatsReader - implicit val writer = JsonFormatsWriter + implicit val stringReader = ClientResponseReaders.StringReader + implicit val jsonReader = JsonFormatsReader + implicit val stringWriter = RequestWriters.StringWriter + implicit val jsonWriter = JsonFormatsWriter var basePath = defBasePath var apiInvoker = defApiInvoker From 9b90f78f0d356fc88c3fc8670790e5216683d3d6 Mon Sep 17 00:00:00 2001 From: wing328 Date: Sat, 18 Mar 2017 17:20:23 +0800 Subject: [PATCH 6/6] update petstore samples for scala clients --- .../src/main/scala/io/swagger/client/SwaggerClient.scala | 1 + .../src/main/scala/io/swagger/client/model/Order.scala | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/samples/client/petstore/async-scala/src/main/scala/io/swagger/client/SwaggerClient.scala b/samples/client/petstore/async-scala/src/main/scala/io/swagger/client/SwaggerClient.scala index f9f2e5d6216..7c4afb20e09 100644 --- a/samples/client/petstore/async-scala/src/main/scala/io/swagger/client/SwaggerClient.scala +++ b/samples/client/petstore/async-scala/src/main/scala/io/swagger/client/SwaggerClient.scala @@ -25,3 +25,4 @@ class SwaggerClient(config: SwaggerConfig) extends Closeable { client.close() } } + diff --git a/samples/client/petstore/scala/src/main/scala/io/swagger/client/model/Order.scala b/samples/client/petstore/scala/src/main/scala/io/swagger/client/model/Order.scala index 086521285f9..84691796eaf 100644 --- a/samples/client/petstore/scala/src/main/scala/io/swagger/client/model/Order.scala +++ b/samples/client/petstore/scala/src/main/scala/io/swagger/client/model/Order.scala @@ -15,10 +15,10 @@ package io.swagger.client.model import java.util.Date case class Order ( - id: Long, - petId: Long, - quantity: Integer, - shipDate: Date, + id: Option[Long], + petId: Option[Long], + quantity: Option[Integer], + shipDate: Option[Date], /* Order Status */ status: Option[String], complete: Option[Boolean]