updated samples

This commit is contained in:
Tony Tam
2015-02-05 20:58:32 -08:00
parent f1ac82f8a0
commit 69dceb7f69
18 changed files with 1888 additions and 0 deletions

View File

@@ -0,0 +1,164 @@
package io.swagger.client
import com.sun.jersey.api.client.Client
import com.sun.jersey.api.client.ClientResponse
import com.sun.jersey.api.client.config.ClientConfig
import com.sun.jersey.api.client.config.DefaultClientConfig
import com.sun.jersey.api.client.filter.LoggingFilter
import com.sun.jersey.multipart.FormDataMultiPart
import com.sun.jersey.multipart.file.FileDataBodyPart
import java.io.File
import java.net.URLEncoder
import javax.ws.rs.core.MediaType
import scala.collection.JavaConverters._
import scala.collection.mutable.HashMap
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.core.JsonGenerator.Feature
import com.fasterxml.jackson.databind._
import com.fasterxml.jackson.annotation._
import com.fasterxml.jackson.databind.annotation.JsonSerialize
object ScalaJsonUtil {
def getJsonMapper = {
val mapper = new ObjectMapper()
mapper.registerModule(new DefaultScalaModule())
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT)
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
mapper
}
}
object ApiInvoker {
val mapper = ScalaJsonUtil.getJsonMapper
val defaultHeaders: HashMap[String, String] = HashMap()
val hostMap: HashMap[String, Client] = HashMap()
def escape(value: String): String = {
URLEncoder.encode(value, "utf-8").replaceAll("\\+", "%20")
}
def escape(value: Long): String = value.toString
def escape(value: Double): String = value.toString
def escape(value: Float): String = value.toString
def deserialize(json: String, containerType: String, cls: Class[_]) = {
if (cls == classOf[String]) {
json match {
case s: String => {
if (s.startsWith("\"") && s.endsWith("\"") && s.length > 1) s.substring(1, s.length - 2)
else s
}
case _ => null
}
} else {
containerType.toLowerCase match {
case "array" => {
val typeInfo = mapper.getTypeFactory().constructCollectionType(classOf[java.util.List[_]], cls)
val response = mapper.readValue(json, typeInfo).asInstanceOf[java.util.List[_]]
response.asScala.toList
}
case "list" => {
val typeInfo = mapper.getTypeFactory().constructCollectionType(classOf[java.util.List[_]], cls)
val response = mapper.readValue(json, typeInfo).asInstanceOf[java.util.List[_]]
response.asScala.toList
}
case _ => {
json match {
case e: String if ("\"\"" == e) => null
case _ => mapper.readValue(json, cls)
}
}
}
}
}
def serialize(obj: AnyRef): String = {
if (obj != null) {
obj match {
case e: List[_] => mapper.writeValueAsString(obj.asInstanceOf[List[_]].asJava)
case _ => mapper.writeValueAsString(obj)
}
} else null
}
def invokeApi(host: String, path: String, method: String, queryParams: Map[String, String], body: AnyRef, headerParams: Map[String, String], contentType: String): String = {
val client = getClient(host)
val querystring = queryParams.filter(k => k._2 != null).map(k => (escape(k._1) + "=" + escape(k._2))).mkString("?", "&", "")
val builder = client.resource(host + path + querystring).accept(contentType)
headerParams.map(p => builder.header(p._1, p._2))
defaultHeaders.map(p => {
headerParams.contains(p._1) match {
case true => // override default with supplied header
case false => if (p._2 != null) builder.header(p._1, p._2)
}
})
val response: ClientResponse = method match {
case "GET" => {
builder.get(classOf[ClientResponse]).asInstanceOf[ClientResponse]
}
case "POST" => {
if(body != null && body.isInstanceOf[File]) {
val file = body.asInstanceOf[File]
val form = new FormDataMultiPart()
form.field("filename", file.getName())
form.bodyPart(new FileDataBodyPart("file", file, MediaType.MULTIPART_FORM_DATA_TYPE))
builder.post(classOf[ClientResponse], form)
}
else {
if(body == null) builder.post(classOf[ClientResponse], serialize(body))
else builder.`type`(contentType).post(classOf[ClientResponse], serialize(body))
}
}
case "PUT" => {
if(body == null) builder.put(classOf[ClientResponse], null)
else builder.`type`(contentType).put(classOf[ClientResponse], serialize(body))
}
case "DELETE" => {
builder.delete(classOf[ClientResponse])
}
case _ => null
}
response.getClientResponseStatus().getStatusCode() match {
case 204 => ""
case code: Int if (Range(200, 299).contains(code)) => {
response.hasEntity() match {
case true => response.getEntity(classOf[String])
case false => ""
}
}
case _ => {
val entity = response.hasEntity() match {
case true => response.getEntity(classOf[String])
case false => "no data"
}
throw new ApiException(
response.getClientResponseStatus().getStatusCode(),
entity)
}
}
}
def getClient(host: String): Client = {
hostMap.contains(host) match {
case true => hostMap(host)
case false => {
val client = Client.create()
// client.addFilter(new LoggingFilter())
hostMap += host -> client
client
}
}
}
}
class ApiException(val code: Int, msg: String) extends RuntimeException(msg)

View File

@@ -0,0 +1,261 @@
package io.swagger.client.api
import io.swagger.client.model.Pet
import io.swagger.client.ApiInvoker
import io.swagger.client.ApiException
import java.io.File
import java.util.Date
import scala.collection.mutable.HashMap
class PetApi {
var basePath: String = "http://petstore.swagger.wordnik.com/v2"
var apiInvoker = ApiInvoker
def addHeader(key: String, value: String) = apiInvoker.defaultHeaders += key -> value
def updatePet (body: Pet) = {
// create path and map variables
val path = "/pet".replaceAll("\\{format\\}","json")
val contentType = {
if(body != null && body.isInstanceOf[File] )
"multipart/form-data"
else "application/json"
}
// query params
val queryParams = new HashMap[String, String]
val headerParams = new HashMap[String, String]
try {
apiInvoker.invokeApi(basePath, path, "PUT", queryParams.toMap, body, headerParams.toMap, contentType) match {
case s: String =>
case _ => None
}
} catch {
case ex: ApiException if ex.code == 404 => None
case ex: ApiException => throw ex
}
}
def addPet (body: Pet) = {
// create path and map variables
val path = "/pet".replaceAll("\\{format\\}","json")
val contentType = {
if(body != null && body.isInstanceOf[File] )
"multipart/form-data"
else "application/json"
}
// query params
val queryParams = new HashMap[String, String]
val headerParams = new HashMap[String, String]
try {
apiInvoker.invokeApi(basePath, path, "POST", queryParams.toMap, body, headerParams.toMap, contentType) match {
case s: String =>
case _ => None
}
} catch {
case ex: ApiException if ex.code == 404 => None
case ex: ApiException => throw ex
}
}
def findPetsByStatus (status: List[String]) : Option[List[Pet]] = {
// create path and map variables
val path = "/pet/findByStatus".replaceAll("\\{format\\}","json")
val contentType = {
"application/json"
}
// query params
val queryParams = new HashMap[String, String]
val headerParams = new HashMap[String, String]
if(String.valueOf(status) != "null") queryParams += "status" -> status.toString
try {
apiInvoker.invokeApi(basePath, path, "GET", queryParams.toMap, None, headerParams.toMap, contentType) match {
case s: String =>
Some(ApiInvoker.deserialize(s, "Pet", classOf[Pet]).asInstanceOf[List[Pet]])
case _ => None
}
} catch {
case ex: ApiException if ex.code == 404 => None
case ex: ApiException => throw ex
}
}
def findPetsByTags (tags: List[String]) : Option[List[Pet]] = {
// create path and map variables
val path = "/pet/findByTags".replaceAll("\\{format\\}","json")
val contentType = {
"application/json"
}
// query params
val queryParams = new HashMap[String, String]
val headerParams = new HashMap[String, String]
if(String.valueOf(tags) != "null") queryParams += "tags" -> tags.toString
try {
apiInvoker.invokeApi(basePath, path, "GET", queryParams.toMap, None, headerParams.toMap, contentType) match {
case s: String =>
Some(ApiInvoker.deserialize(s, "Pet", classOf[Pet]).asInstanceOf[List[Pet]])
case _ => None
}
} catch {
case ex: ApiException if ex.code == 404 => None
case ex: ApiException => throw ex
}
}
def getPetById (petId: Long) : Option[Pet] = {
// create path and map variables
val path = "/pet/{petId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "petId" + "\\}",apiInvoker.escape(petId))
val contentType = {
"application/json"
}
// query params
val queryParams = new HashMap[String, String]
val headerParams = new HashMap[String, String]
try {
apiInvoker.invokeApi(basePath, path, "GET", queryParams.toMap, None, 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
}
}
def updatePetWithForm (petId: String, name: String, status: String) = {
// create path and map variables
val path = "/pet/{petId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "petId" + "\\}",apiInvoker.escape(petId))
val contentType = {
"application/json"
}
// query params
val queryParams = new HashMap[String, String]
val headerParams = new HashMap[String, String]
try {
apiInvoker.invokeApi(basePath, path, "POST", queryParams.toMap, None, headerParams.toMap, contentType) match {
case s: String =>
case _ => None
}
} catch {
case ex: ApiException if ex.code == 404 => None
case ex: ApiException => throw ex
}
}
def deletePet (api_key: String, petId: Long) = {
// create path and map variables
val path = "/pet/{petId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "petId" + "\\}",apiInvoker.escape(petId))
val contentType = {
"application/json"
}
// query params
val queryParams = new HashMap[String, String]
val headerParams = new HashMap[String, String]
headerParams += "api_key" -> api_key
try {
apiInvoker.invokeApi(basePath, path, "DELETE", queryParams.toMap, None, headerParams.toMap, contentType) match {
case s: String =>
case _ => None
}
} catch {
case ex: ApiException if ex.code == 404 => None
case ex: ApiException => throw ex
}
}
}

View File

@@ -0,0 +1,157 @@
package io.swagger.client.api
import io.swagger.client.model.Order
import io.swagger.client.ApiInvoker
import io.swagger.client.ApiException
import java.io.File
import java.util.Date
import scala.collection.mutable.HashMap
class StoreApi {
var basePath: String = "http://petstore.swagger.wordnik.com/v2"
var apiInvoker = ApiInvoker
def addHeader(key: String, value: String) = apiInvoker.defaultHeaders += key -> value
def getInventory () : Option[Map[String, Integer]] = {
// create path and map variables
val path = "/store/inventory".replaceAll("\\{format\\}","json")
val contentType = {
"application/json"
}
// query params
val queryParams = new HashMap[String, String]
val headerParams = new HashMap[String, String]
try {
apiInvoker.invokeApi(basePath, path, "GET", queryParams.toMap, None, headerParams.toMap, contentType) match {
case s: String =>
Some(ApiInvoker.deserialize(s, "Integer", classOf[Integer]).asInstanceOf[Map[String, Integer]])
case _ => None
}
} catch {
case ex: ApiException if ex.code == 404 => None
case ex: ApiException => throw ex
}
}
def placeOrder (body: Order) : Option[Order] = {
// create path and map variables
val path = "/store/order".replaceAll("\\{format\\}","json")
val contentType = {
if(body != null && body.isInstanceOf[File] )
"multipart/form-data"
else "application/json"
}
// query params
val queryParams = new HashMap[String, String]
val headerParams = new HashMap[String, String]
try {
apiInvoker.invokeApi(basePath, path, "POST", queryParams.toMap, body, 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: String) : Option[Order] = {
// create path and map variables
val path = "/store/order/{orderId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "orderId" + "\\}",apiInvoker.escape(orderId))
val contentType = {
"application/json"
}
// query params
val queryParams = new HashMap[String, String]
val headerParams = new HashMap[String, String]
try {
apiInvoker.invokeApi(basePath, path, "GET", queryParams.toMap, None, 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 deleteOrder (orderId: String) = {
// create path and map variables
val path = "/store/order/{orderId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "orderId" + "\\}",apiInvoker.escape(orderId))
val contentType = {
"application/json"
}
// query params
val queryParams = new HashMap[String, String]
val headerParams = new HashMap[String, String]
try {
apiInvoker.invokeApi(basePath, path, "DELETE", queryParams.toMap, None, headerParams.toMap, contentType) match {
case s: String =>
case _ => None
}
} catch {
case ex: ApiException if ex.code == 404 => None
case ex: ApiException => throw ex
}
}
}

View File

@@ -0,0 +1,297 @@
package io.swagger.client.api
import io.swagger.client.model.User
import io.swagger.client.ApiInvoker
import io.swagger.client.ApiException
import java.io.File
import java.util.Date
import scala.collection.mutable.HashMap
class UserApi {
var basePath: String = "http://petstore.swagger.wordnik.com/v2"
var apiInvoker = ApiInvoker
def addHeader(key: String, value: String) = apiInvoker.defaultHeaders += key -> value
def createUser (body: User) = {
// create path and map variables
val path = "/user".replaceAll("\\{format\\}","json")
val contentType = {
if(body != null && body.isInstanceOf[File] )
"multipart/form-data"
else "application/json"
}
// query params
val queryParams = new HashMap[String, String]
val headerParams = new HashMap[String, String]
try {
apiInvoker.invokeApi(basePath, path, "POST", queryParams.toMap, body, headerParams.toMap, contentType) match {
case s: String =>
case _ => None
}
} catch {
case ex: ApiException if ex.code == 404 => None
case ex: ApiException => throw ex
}
}
def createUsersWithArrayInput (body: List[User]) = {
// create path and map variables
val path = "/user/createWithArray".replaceAll("\\{format\\}","json")
val contentType = {
if(body != null && body.isInstanceOf[File] )
"multipart/form-data"
else "application/json"
}
// query params
val queryParams = new HashMap[String, String]
val headerParams = new HashMap[String, String]
try {
apiInvoker.invokeApi(basePath, path, "POST", queryParams.toMap, body, 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]) = {
// create path and map variables
val path = "/user/createWithList".replaceAll("\\{format\\}","json")
val contentType = {
if(body != null && body.isInstanceOf[File] )
"multipart/form-data"
else "application/json"
}
// query params
val queryParams = new HashMap[String, String]
val headerParams = new HashMap[String, String]
try {
apiInvoker.invokeApi(basePath, path, "POST", queryParams.toMap, body, headerParams.toMap, contentType) match {
case s: String =>
case _ => None
}
} catch {
case ex: ApiException if ex.code == 404 => None
case ex: ApiException => throw ex
}
}
def loginUser (username: String, password: String) : Option[String] = {
// create path and map variables
val path = "/user/login".replaceAll("\\{format\\}","json")
val contentType = {
"application/json"
}
// query params
val queryParams = new HashMap[String, String]
val headerParams = new HashMap[String, String]
if(String.valueOf(username) != "null") queryParams += "username" -> username.toString
if(String.valueOf(password) != "null") queryParams += "password" -> password.toString
try {
apiInvoker.invokeApi(basePath, path, "GET", queryParams.toMap, None, 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
}
}
def logoutUser () = {
// create path and map variables
val path = "/user/logout".replaceAll("\\{format\\}","json")
val contentType = {
"application/json"
}
// query params
val queryParams = new HashMap[String, String]
val headerParams = new HashMap[String, String]
try {
apiInvoker.invokeApi(basePath, path, "GET", queryParams.toMap, None, headerParams.toMap, contentType) match {
case s: String =>
case _ => None
}
} catch {
case ex: ApiException if ex.code == 404 => None
case ex: ApiException => throw ex
}
}
def getUserByName (username: String) : Option[User] = {
// create path and map variables
val path = "/user/{username}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "username" + "\\}",apiInvoker.escape(username))
val contentType = {
"application/json"
}
// query params
val queryParams = new HashMap[String, String]
val headerParams = new HashMap[String, String]
try {
apiInvoker.invokeApi(basePath, path, "GET", queryParams.toMap, None, 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
}
}
def updateUser (username: String, body: User) = {
// create path and map variables
val path = "/user/{username}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "username" + "\\}",apiInvoker.escape(username))
val contentType = {
if(body != null && body.isInstanceOf[File] )
"multipart/form-data"
else "application/json"
}
// query params
val queryParams = new HashMap[String, String]
val headerParams = new HashMap[String, String]
try {
apiInvoker.invokeApi(basePath, path, "PUT", queryParams.toMap, body, headerParams.toMap, contentType) match {
case s: String =>
case _ => None
}
} catch {
case ex: ApiException if ex.code == 404 => None
case ex: ApiException => throw ex
}
}
def deleteUser (username: String) = {
// create path and map variables
val path = "/user/{username}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "username" + "\\}",apiInvoker.escape(username))
val contentType = {
"application/json"
}
// query params
val queryParams = new HashMap[String, String]
val headerParams = new HashMap[String, String]
try {
apiInvoker.invokeApi(basePath, path, "DELETE", queryParams.toMap, None, headerParams.toMap, contentType) match {
case s: String =>
case _ => None
}
} catch {
case ex: ApiException if ex.code == 404 => None
case ex: ApiException => throw ex
}
}
}

View File

@@ -0,0 +1,9 @@
package io.swagger.client.model
case class Category (
id: Long,
name: String)

View File

@@ -0,0 +1,15 @@
package io.swagger.client.model
import org.joda.time.DateTime
case class Order (
id: Long,
petId: Long,
quantity: Integer,
shipDate: DateTime,
/* Order Status */
status: String,
complete: Boolean)

View File

@@ -0,0 +1,16 @@
package io.swagger.client.model
import io.swagger.client.model.Category
import io.swagger.client.model.Tag
case class Pet (
id: Long,
category: Category,
name: String,
photoUrls: List[String],
tags: List[Tag],
/* pet status in the store */
status: String)

View File

@@ -0,0 +1,9 @@
package io.swagger.client.model
case class Tag (
id: Long,
name: String)

View File

@@ -0,0 +1,16 @@
package io.swagger.client.model
case class User (
id: Long,
username: String,
firstName: String,
lastName: String,
email: String,
password: String,
phone: String,
/* User Status */
userStatus: Integer)