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..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; @@ -20,6 +21,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 +53,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 @@ -75,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"); @@ -90,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"); @@ -98,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"); @@ -108,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/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..b83e4de8b5b 100644 --- a/modules/swagger-codegen/src/main/resources/scala/api.mustache +++ b/modules/swagger-codegen/src/main/resources/scala/api.mustache @@ -1,10 +1,11 @@ {{>licenseInfo}} package {{package}} +import java.text.SimpleDateFormat + {{#imports}}import {{import}} {{/imports}} -import {{invokerPackage}}.ApiInvoker -import {{invokerPackage}}.ApiException +import {{invokerPackage}}.{ApiInvoker, ApiException} import com.sun.jersey.multipart.FormDataMultiPart import com.sun.jersey.multipart.file.FileDataBodyPart @@ -16,13 +17,40 @@ 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 = new org.json4s.DefaultFormats { + override def dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+0000") + } + implicit val stringReader = ClientResponseReaders.StringReader + implicit val jsonReader = JsonFormatsReader + implicit val stringWriter = RequestWriters.StringWriter + implicit val jsonWriter = 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 + + val config = SwaggerConfig.forUrl(new URI(defBasePath)) + val client = new RestClient(config) + val helper = new {{classname}}AsyncHelper(client, config) {{#operation}} /** @@ -32,97 +60,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) { + +{{#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}}) - var postBody: AnyRef = {{#bodyParam}}{{#required}}{{paramName}}{{/required}}{{^required}}{{paramName}}.map(paramVal => paramVal){{/required}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}} + // query params + val queryParams = new mutable.HashMap[String, String] + val headerParams = new mutable.HashMap[String, String] - if (contentType.startsWith("multipart/form-data")) { - val mp = new FormDataMultiPart - {{#formParams}} - {{#notFile}} + {{#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 + headerParams += "{{baseName}}" -> {{paramName}}.toString {{/required}} - {{^required}} - {{paramName}}.map(paramVal => formParams += "{{baseName}}" -> paramVal.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..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,33 +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" - ), - - 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/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/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/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/build.sbt b/samples/client/petstore/scala/build.sbt index 063b2b0d490..bececaf181b 100644 --- a/samples/client/petstore/scala/build.sbt +++ b/samples/client/petstore/scala/build.sbt @@ -1,33 +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" - ), - - 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/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..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 @@ -12,11 +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.{ApiInvoker, ApiException} import com.sun.jersey.multipart.FormDataMultiPart import com.sun.jersey.multipart.file.FileDataBodyPart @@ -28,12 +29,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} + class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2", defApiInvoker: ApiInvoker = ApiInvoker) { + + implicit val formats = new org.json4s.DefaultFormats { + override def dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+0000") + } + implicit val stringReader = ClientResponseReaders.StringReader + implicit val jsonReader = JsonFormatsReader + implicit val stringWriter = RequestWriters.StringWriter + implicit val jsonWriter = 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 + + val config = SwaggerConfig.forUrl(new URI(defBasePath)) + val client = new RestClient(config) + val helper = new PetApiAsyncHelper(client, config) /** * Add a new pet to the store @@ -42,39 +70,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 +97,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 +124,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 +150,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 +176,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 +202,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 +230,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 +260,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 = (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->addPet") + + 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 = "/pet/{petId}/uploadImage".replaceAll("\\{format\\}", "json").replaceAll("\\{" + "petId" + "\\}",apiInvoker.escape(petId)) + val path = (addFmt("/pet/{petId}") + replaceAll ("\\{" + "petId" + "\\}",petId.toString)) - val contentTypes = List("multipart/form-data") - 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] + 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)) + } + } + + 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] - var postBody: AnyRef = null + if (status == null) throw new Exception("Missing required parameter 'status' when calling PetApi->findPetsByStatus") + queryParams += "status" -> status.toString - 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("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] - 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 (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..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 @@ -12,9 +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.{ApiInvoker, ApiException} import com.sun.jersey.multipart.FormDataMultiPart import com.sun.jersey.multipart.file.FileDataBodyPart @@ -26,12 +27,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} + class StoreApi(val defBasePath: String = "http://petstore.swagger.io/v2", defApiInvoker: ApiInvoker = ApiInvoker) { + + implicit val formats = new org.json4s.DefaultFormats { + override def dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+0000") + } + implicit val stringReader = ClientResponseReaders.StringReader + implicit val jsonReader = JsonFormatsReader + implicit val stringWriter = RequestWriters.StringWriter + implicit val jsonWriter = 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 + + val config = SwaggerConfig.forUrl(new URI(defBasePath)) + val client = new RestClient(config) + val helper = new StoreApiAsyncHelper(client, config) /** * Delete purchase order by ID @@ -40,77 +68,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 +118,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 +144,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)) + } + } + + def getInventory()(implicit reader: ClientResponseReader[Map[String, Integer]]): Future[Map[String, Integer]] = { + // create path and map variables + val path = (addFmt("/store/inventory")) + + // 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 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] - var postBody: AnyRef = body - if (contentType.startsWith("multipart/form-data")) { - val mp = new FormDataMultiPart - postBody = mp - } else { + 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] - 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 + 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..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 @@ -12,9 +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.{ApiInvoker, ApiException} import com.sun.jersey.multipart.FormDataMultiPart import com.sun.jersey.multipart.file.FileDataBodyPart @@ -26,12 +27,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} + class UserApi(val defBasePath: String = "http://petstore.swagger.io/v2", defApiInvoker: ApiInvoker = ApiInvoker) { + + implicit val formats = new org.json4s.DefaultFormats { + override def dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+0000") + } + implicit val stringReader = ClientResponseReaders.StringReader + implicit val jsonReader = JsonFormatsReader + implicit val stringWriter = RequestWriters.StringWriter + implicit val jsonWriter = 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 + + val config = SwaggerConfig.forUrl(new URI(defBasePath)) + val client = new RestClient(config) + val helper = new UserApiAsyncHelper(client, config) /** * Create user @@ -40,324 +68,353 @@ 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] + val await = Try(Await.result(createUserAsync(body), Duration.Inf)) + await match { + case Success(i) => Some(await.get) + case Failure(t) => None + } - if (body == null) throw new Exception("Missing required parameter 'body' when calling UserApi->createUser") + } - + /** + * 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) + } - var postBody: AnyRef = body - if (contentType.startsWith("multipart/form-data")) { - val mp = new FormDataMultiPart - postBody = mp - } else { + /** + * Creates list of users with given input array + * + * @param body List of user object + * @return void + */ + def createUsersWithArrayInput(body: List[User]) = { + 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 * * @param body List of user object * @return void */ - def createUsersWithArrayInput(body: List[User]) = { - // create path and map variables - val path = "/user/createWithArray".replaceAll("\\{format\\}", "json") + def createUsersWithListInput(body: List[User]) = { + val await = Try(Await.result(createUsersWithListInputAsync(body), Duration.Inf)) + await match { + case Success(i) => Some(await.get) + case Failure(t) => None + } - 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] + /** + * 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) + } - if (body == null) throw new Exception("Missing required parameter 'body' when calling UserApi->createUsersWithArrayInput") - + /** + * Delete user + * This can only be done by the logged in user. + * @param username The name that needs to be deleted + * @return void + */ + def deleteUser(username: String) = { + val await = Try(Await.result(deleteUserAsync(username), Duration.Inf)) + await match { + case Success(i) => Some(await.get) + case Failure(t) => None + } - var postBody: AnyRef = body + } + + /** + * 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) + } - if (contentType.startsWith("multipart/form-data")) { - val mp = new FormDataMultiPart - postBody = mp - } else { - } - 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 + /** + * Get user by user name + * + * @param username The name that needs to be fetched. Use user1 for testing. + * @return User + */ + def getUserByName(username: String): Option[User] = { + val await = Try(Await.result(getUserByNameAsync(username), Duration.Inf)) + await match { + case Success(i) => Some(await.get) + case Failure(t) => None } + } /** - * Creates list of users with given input array + * Get user by user name asynchronously * - * @param body List of user object - * @return void - */ - def createUsersWithListInput(body: List[User]) = { - // create path and map variables - val path = "/user/createWithList".replaceAll("\\{format\\}", "json") + * @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) + } - 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] + /** + * Logs user into the system + * + * @param username The user name for login + * @param password The password for login in clear text + * @return String + */ + def loginUser(username: String, password: String): Option[String] = { + val await = Try(Await.result(loginUserAsync(username, password), Duration.Inf)) + await match { + case Success(i) => Some(await.get) + case Failure(t) => None + } - if (body == null) throw new Exception("Missing required parameter 'body' when calling UserApi->createUsersWithListInput") + } - + /** + * 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) + } - var postBody: AnyRef = body - if (contentType.startsWith("multipart/form-data")) { - val mp = new FormDataMultiPart - postBody = mp - } else { + /** + * Logs out current logged in user session + * + * @return void + */ + def logoutUser() = { + 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, "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 - } } /** - * Delete user + * 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. - * @param username The name that needs to be deleted + * @param username name that need to be deleted + * @param body Updated user object * @return void */ - def deleteUser(username: String) = { - // create path and map variables - val path = "/user/{username}".replaceAll("\\{format\\}", "json").replaceAll("\\{" + "username" + "\\}",apiInvoker.escape(username)) + 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 + } - 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] + /** + * 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) + } - if (username == null) throw new Exception("Missing required parameter 'username' when calling UserApi->deleteUser") - +} - var postBody: AnyRef = null +class UserApiAsyncHelper(client: TransportClient, config: SwaggerConfig) extends ApiClient(client, config) { - if (contentType.startsWith("multipart/form-data")) { - val mp = new FormDataMultiPart - postBody = mp - } else { - } + def createUser(body: User)(implicit reader: ClientResponseReader[Unit], writer: RequestWriter[User]): Future[Unit] = { + // create path and map variables + val path = (addFmt("/user")) + + // 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->createUser") - 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 + val resFuture = client.submit("POST", path, queryParams.toMap, headerParams.toMap, writer.write(body)) + resFuture flatMap { resp => + process(reader.read(resp)) } } - /** - * Get user by user name - * - * @param username The name that needs to be fetched. Use user1 for testing. - * @return User - */ - def getUserByName(username: String): Option[User] = { + def createUsersWithArrayInput(body: List[User])(implicit reader: ClientResponseReader[Unit], writer: RequestWriter[List[User]]): Future[Unit] = { // create path and map variables - val path = "/user/{username}".replaceAll("\\{format\\}", "json").replaceAll("\\{" + "username" + "\\}",apiInvoker.escape(username)) + val path = (addFmt("/user/createWithArray")) - 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->createUsersWithArrayInput") - if (username == null) throw new Exception("Missing required parameter 'username' when calling UserApi->getUserByName") + val resFuture = client.submit("POST", path, queryParams.toMap, headerParams.toMap, writer.write(body)) + resFuture flatMap { resp => + process(reader.read(resp)) + } + } - + 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")) - var postBody: AnyRef = null + // query params + val queryParams = new mutable.HashMap[String, String] + val headerParams = new mutable.HashMap[String, String] - if (contentType.startsWith("multipart/form-data")) { - val mp = new FormDataMultiPart - postBody = mp - } else { - } + if (body == null) throw new Exception("Missing required parameter 'body' when calling UserApi->createUsersWithListInput") - 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 + val resFuture = client.submit("POST", path, queryParams.toMap, headerParams.toMap, writer.write(body)) + resFuture flatMap { resp => + process(reader.read(resp)) } } - /** - * Logs user into the system - * - * @param username The user name for login - * @param password The password for login in clear text - * @return String - */ - def loginUser(username: String, password: String): Option[String] = { + def deleteUser(username: String)(implicit reader: ClientResponseReader[Unit]): Future[Unit] = { // create path and map variables - val path = "/user/login".replaceAll("\\{format\\}", "json") + val path = (addFmt("/user/{username}") + replaceAll ("\\{" + "username" + "\\}",username.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 (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->loginUser") - if (password == null) throw new Exception("Missing required parameter 'password' when calling UserApi->loginUser") + val resFuture = client.submit("DELETE", path, queryParams.toMap, headerParams.toMap, "") + resFuture flatMap { resp => + process(reader.read(resp)) + } + } - queryParams += "username" -> username.toString - queryParams += "password" -> password.toString - + def getUserByName(username: String)(implicit reader: ClientResponseReader[User]): Future[User] = { + // create path and map variables + val path = (addFmt("/user/{username}") + replaceAll ("\\{" + "username" + "\\}",username.toString)) - var postBody: AnyRef = null + // 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 (contentType.startsWith("multipart/form-data")) { - val mp = new FormDataMultiPart - postBody = mp - } else { - } - 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 + val resFuture = client.submit("GET", path, queryParams.toMap, headerParams.toMap, "") + resFuture flatMap { resp => + process(reader.read(resp)) } } - /** - * Logs out current logged in user session - * - * @return void - */ - def logoutUser() = { + def loginUser(username: String, + password: String)(implicit reader: ClientResponseReader[String]): Future[String] = { // create path and map variables - val path = "/user/logout".replaceAll("\\{format\\}", "json") + val path = (addFmt("/user/login")) - 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 (username == null) throw new Exception("Missing required parameter 'username' when calling UserApi->loginUser") - var postBody: AnyRef = null + if (password == null) throw new Exception("Missing required parameter 'password' when calling UserApi->loginUser") - if (contentType.startsWith("multipart/form-data")) { - val mp = new FormDataMultiPart - postBody = mp - } else { - } + queryParams += "username" -> username.toString + queryParams += "password" -> password.toString - 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 + val resFuture = client.submit("GET", path, queryParams.toMap, headerParams.toMap, "") + resFuture flatMap { resp => + process(reader.read(resp)) } } - /** - * Updated user - * This can only be done by the logged in user. - * @param username name that need to be deleted - * @param body Updated user object - * @return void - */ - def updateUser(username: String, body: User) = { + def logoutUser()(implicit reader: ClientResponseReader[Unit]): Future[Unit] = { // create path and map variables - val path = "/user/{username}".replaceAll("\\{format\\}", "json").replaceAll("\\{" + "username" + "\\}",apiInvoker.escape(username)) + val path = (addFmt("/user/logout")) - 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 (username == null) throw new Exception("Missing required parameter 'username' when calling UserApi->updateUser") + val resFuture = client.submit("GET", path, queryParams.toMap, headerParams.toMap, "") + resFuture flatMap { resp => + process(reader.read(resp)) + } + } - if (body == null) throw new Exception("Missing required parameter 'body' when calling UserApi->updateUser") + 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] - var postBody: AnyRef = body + if (username == null) throw new Exception("Missing required parameter 'username' when calling UserApi->updateUser") - if (contentType.startsWith("multipart/form-data")) { - val mp = new FormDataMultiPart - postBody = mp - } else { - } + if (body == null) throw new Exception("Missing required parameter 'body' when calling UserApi->updateUser") - 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 + val resFuture = client.submit("PUT", 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/model/Order.scala b/samples/client/petstore/scala/src/main/scala/io/swagger/client/model/Order.scala index a88c0ec23d9..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 @@ -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], + shipDate: Option[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),