forked from loafle/openapi-generator-original
rollback scala template
This commit is contained in:
parent
7d6fdf96c0
commit
b636d2a2c9
@ -16,7 +16,7 @@ import java.util.Date
|
|||||||
import scala.collection.mutable.HashMap
|
import scala.collection.mutable.HashMap
|
||||||
|
|
||||||
{{#operations}}
|
{{#operations}}
|
||||||
class {{classname}}(val defBasePath: String = "{{basePath}}",
|
class {{classname}}(val defBasePath: String = "{{basePath}}",
|
||||||
defApiInvoker: ApiInvoker = ApiInvoker) {
|
defApiInvoker: ApiInvoker = ApiInvoker) {
|
||||||
var basePath = defBasePath
|
var basePath = defBasePath
|
||||||
var apiInvoker = defApiInvoker
|
var apiInvoker = defApiInvoker
|
||||||
@ -27,8 +27,8 @@ import scala.collection.mutable.HashMap
|
|||||||
/**
|
/**
|
||||||
* {{summary}}
|
* {{summary}}
|
||||||
* {{notes}}
|
* {{notes}}
|
||||||
{{#allParams}} * @param {{paramName}} {{description}}
|
{{#allParams}} * @param {{paramName}} {{description}}
|
||||||
{{/allParams}} * @return {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}
|
{{/allParams}} * @return {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}
|
||||||
*/
|
*/
|
||||||
def {{nickname}} ({{#allParams}}{{paramName}}: {{dataType}}{{#defaultValue}} /* = {{{defaultValue}}} */{{/defaultValue}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) {{#returnType}}: Option[{{returnType}}]{{/returnType}} = {
|
def {{nickname}} ({{#allParams}}{{paramName}}: {{dataType}}{{#defaultValue}} /* = {{{defaultValue}}} */{{/defaultValue}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) {{#returnType}}: Option[{{returnType}}]{{/returnType}} = {
|
||||||
// create path and map variables
|
// create path and map variables
|
||||||
@ -88,5 +88,5 @@ import scala.collection.mutable.HashMap
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
{{/operation}}
|
{{/operation}}
|
||||||
}
|
}
|
||||||
{{/operations}}
|
{{/operations}}
|
||||||
|
@ -25,179 +25,179 @@ import com.fasterxml.jackson.annotation._
|
|||||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize
|
import com.fasterxml.jackson.databind.annotation.JsonSerialize
|
||||||
|
|
||||||
object ScalaJsonUtil {
|
object ScalaJsonUtil {
|
||||||
def getJsonMapper = {
|
def getJsonMapper = {
|
||||||
val mapper = new ObjectMapper()
|
val mapper = new ObjectMapper()
|
||||||
mapper.registerModule(new DefaultScalaModule())
|
mapper.registerModule(new DefaultScalaModule())
|
||||||
mapper.registerModule(new JodaModule());
|
mapper.registerModule(new JodaModule());
|
||||||
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||||
mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT)
|
mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT)
|
||||||
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
|
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
|
||||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
|
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
|
||||||
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
|
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
|
||||||
mapper
|
mapper
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ApiInvoker(val mapper: ObjectMapper = ScalaJsonUtil.getJsonMapper,
|
class ApiInvoker(val mapper: ObjectMapper = ScalaJsonUtil.getJsonMapper,
|
||||||
httpHeaders: HashMap[String, String] = HashMap(),
|
httpHeaders: HashMap[String, String] = HashMap(),
|
||||||
hostMap: HashMap[String, Client] = HashMap(),
|
hostMap: HashMap[String, Client] = HashMap(),
|
||||||
asyncHttpClient: Boolean = false,
|
asyncHttpClient: Boolean = false,
|
||||||
authScheme: String = "",
|
authScheme: String = "",
|
||||||
authPreemptive: Boolean = false) {
|
authPreemptive: Boolean = false) {
|
||||||
|
|
||||||
var defaultHeaders: HashMap[String, String] = httpHeaders
|
var defaultHeaders: HashMap[String, String] = httpHeaders
|
||||||
|
|
||||||
def escape(value: String): String = {
|
def escape(value: String): String = {
|
||||||
URLEncoder.encode(value, "utf-8").replaceAll("\\+", "%20")
|
URLEncoder.encode(value, "utf-8").replaceAll("\\+", "%20")
|
||||||
}
|
}
|
||||||
|
|
||||||
def escape(value: Long): String = value.toString
|
def escape(value: Long): String = value.toString
|
||||||
def escape(value: Double): String = value.toString
|
def escape(value: Double): String = value.toString
|
||||||
def escape(value: Float): String = value.toString
|
def escape(value: Float): String = value.toString
|
||||||
|
|
||||||
def deserialize(json: String, containerType: String, cls: Class[_]) = {
|
def deserialize(json: String, containerType: String, cls: Class[_]) = {
|
||||||
if (cls == classOf[String]) {
|
if (cls == classOf[String]) {
|
||||||
json match {
|
json match {
|
||||||
case s: String => {
|
case s: String => {
|
||||||
if (s.startsWith("\"") && s.endsWith("\"") && s.length > 1) s.substring(1, s.length - 2)
|
if (s.startsWith("\"") && s.endsWith("\"") && s.length > 1) s.substring(1, s.length - 2)
|
||||||
else s
|
else s
|
||||||
}
|
}
|
||||||
case _ => null
|
case _ => null
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
containerType.toLowerCase match {
|
containerType.toLowerCase match {
|
||||||
case "array" => {
|
case "array" => {
|
||||||
val typeInfo = mapper.getTypeFactory().constructCollectionType(classOf[java.util.List[_]], cls)
|
val typeInfo = mapper.getTypeFactory().constructCollectionType(classOf[java.util.List[_]], cls)
|
||||||
val response = mapper.readValue(json, typeInfo).asInstanceOf[java.util.List[_]]
|
val response = mapper.readValue(json, typeInfo).asInstanceOf[java.util.List[_]]
|
||||||
response.asScala.toList
|
response.asScala.toList
|
||||||
}
|
}
|
||||||
case "list" => {
|
case "list" => {
|
||||||
val typeInfo = mapper.getTypeFactory().constructCollectionType(classOf[java.util.List[_]], cls)
|
val typeInfo = mapper.getTypeFactory().constructCollectionType(classOf[java.util.List[_]], cls)
|
||||||
val response = mapper.readValue(json, typeInfo).asInstanceOf[java.util.List[_]]
|
val response = mapper.readValue(json, typeInfo).asInstanceOf[java.util.List[_]]
|
||||||
response.asScala.toList
|
response.asScala.toList
|
||||||
}
|
}
|
||||||
case _ => {
|
case _ => {
|
||||||
json match {
|
json match {
|
||||||
case e: String if ("\"\"" == e) => null
|
case e: String if ("\"\"" == e) => null
|
||||||
case _ => mapper.readValue(json, cls)
|
case _ => mapper.readValue(json, cls)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
def serialize(obj: AnyRef): String = {
|
def serialize(obj: AnyRef): String = {
|
||||||
if (obj != null) {
|
if (obj != null) {
|
||||||
obj match {
|
obj match {
|
||||||
case e: List[_] => mapper.writeValueAsString(obj.asInstanceOf[List[_]].asJava)
|
case e: List[_] => mapper.writeValueAsString(obj.asInstanceOf[List[_]].asJava)
|
||||||
case _ => mapper.writeValueAsString(obj)
|
case _ => mapper.writeValueAsString(obj)
|
||||||
}
|
}
|
||||||
} else null
|
} else null
|
||||||
}
|
}
|
||||||
|
|
||||||
def invokeApi(host: String, path: String, method: String, queryParams: Map[String, String], formParams: Map[String, String], body: AnyRef, headerParams: Map[String, String], contentType: String): String = {
|
def invokeApi(host: String, path: String, method: String, queryParams: Map[String, String], formParams: Map[String, String], body: AnyRef, headerParams: Map[String, String], contentType: String): String = {
|
||||||
val client = getClient(host)
|
val client = getClient(host)
|
||||||
|
|
||||||
val querystring = queryParams.filter(k => k._2 != null).map(k => (escape(k._1) + "=" + escape(k._2))).mkString("?", "&", "")
|
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)
|
val builder = client.resource(host + path + querystring).accept(contentType)
|
||||||
headerParams.map(p => builder.header(p._1, p._2))
|
headerParams.map(p => builder.header(p._1, p._2))
|
||||||
defaultHeaders.map(p => {
|
defaultHeaders.map(p => {
|
||||||
headerParams.contains(p._1) match {
|
headerParams.contains(p._1) match {
|
||||||
case true => // override default with supplied header
|
case true => // override default with supplied header
|
||||||
case false => if (p._2 != null) builder.header(p._1, p._2)
|
case false => if (p._2 != null) builder.header(p._1, p._2)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
var formData: MultivaluedMapImpl = null
|
var formData: MultivaluedMapImpl = null
|
||||||
if(contentType == "application/x-www-form-urlencoded") {
|
if(contentType == "application/x-www-form-urlencoded") {
|
||||||
formData = new MultivaluedMapImpl()
|
formData = new MultivaluedMapImpl()
|
||||||
formParams.map(p => formData.add(p._1, p._2))
|
formParams.map(p => formData.add(p._1, p._2))
|
||||||
}
|
}
|
||||||
|
|
||||||
val response: ClientResponse = method match {
|
val response: ClientResponse = method match {
|
||||||
case "GET" => {
|
case "GET" => {
|
||||||
builder.get(classOf[ClientResponse]).asInstanceOf[ClientResponse]
|
builder.get(classOf[ClientResponse]).asInstanceOf[ClientResponse]
|
||||||
}
|
}
|
||||||
case "POST" => {
|
case "POST" => {
|
||||||
if(formData != null) builder.post(classOf[ClientResponse], formData)
|
if(formData != null) builder.post(classOf[ClientResponse], formData)
|
||||||
else if(body != null && body.isInstanceOf[File]) {
|
else if(body != null && body.isInstanceOf[File]) {
|
||||||
val file = body.asInstanceOf[File]
|
val file = body.asInstanceOf[File]
|
||||||
val form = new FormDataMultiPart()
|
val form = new FormDataMultiPart()
|
||||||
form.field("filename", file.getName())
|
form.field("filename", file.getName())
|
||||||
form.bodyPart(new FileDataBodyPart("file", file, MediaType.MULTIPART_FORM_DATA_TYPE))
|
form.bodyPart(new FileDataBodyPart("file", file, MediaType.MULTIPART_FORM_DATA_TYPE))
|
||||||
builder.post(classOf[ClientResponse], form)
|
builder.post(classOf[ClientResponse], form)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if(body == null) builder.post(classOf[ClientResponse], serialize(body))
|
if(body == null) builder.post(classOf[ClientResponse], serialize(body))
|
||||||
else builder.`type`(contentType).post(classOf[ClientResponse], serialize(body))
|
else builder.`type`(contentType).post(classOf[ClientResponse], serialize(body))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case "PUT" => {
|
case "PUT" => {
|
||||||
if(formData != null) builder.post(classOf[ClientResponse], formData)
|
if(formData != null) builder.post(classOf[ClientResponse], formData)
|
||||||
else if(body == null) builder.put(classOf[ClientResponse], null)
|
else if(body == null) builder.put(classOf[ClientResponse], null)
|
||||||
else builder.`type`(contentType).put(classOf[ClientResponse], serialize(body))
|
else builder.`type`(contentType).put(classOf[ClientResponse], serialize(body))
|
||||||
}
|
}
|
||||||
case "DELETE" => {
|
case "DELETE" => {
|
||||||
builder.delete(classOf[ClientResponse])
|
builder.delete(classOf[ClientResponse])
|
||||||
}
|
}
|
||||||
case _ => null
|
case _ => null
|
||||||
}
|
}
|
||||||
response.getClientResponseStatus().getStatusCode() match {
|
response.getClientResponseStatus().getStatusCode() match {
|
||||||
case 204 => ""
|
case 204 => ""
|
||||||
case code: Int if (Range(200, 299).contains(code)) => {
|
case code: Int if (Range(200, 299).contains(code)) => {
|
||||||
response.hasEntity() match {
|
response.hasEntity() match {
|
||||||
case true => response.getEntity(classOf[String])
|
case true => response.getEntity(classOf[String])
|
||||||
case false => ""
|
case false => ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case _ => {
|
case _ => {
|
||||||
val entity = response.hasEntity() match {
|
val entity = response.hasEntity() match {
|
||||||
case true => response.getEntity(classOf[String])
|
case true => response.getEntity(classOf[String])
|
||||||
case false => "no data"
|
case false => "no data"
|
||||||
}
|
}
|
||||||
throw new ApiException(
|
throw new ApiException(
|
||||||
response.getClientResponseStatus().getStatusCode(),
|
response.getClientResponseStatus().getStatusCode(),
|
||||||
entity)
|
entity)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
def getClient(host: String): Client = {
|
def getClient(host: String): Client = {
|
||||||
hostMap.contains(host) match {
|
hostMap.contains(host) match {
|
||||||
case true => hostMap(host)
|
case true => hostMap(host)
|
||||||
case false => {
|
case false => {
|
||||||
val client = newClient(host)
|
val client = newClient(host)
|
||||||
// client.addFilter(new LoggingFilter())
|
// client.addFilter(new LoggingFilter())
|
||||||
hostMap += host -> client
|
hostMap += host -> client
|
||||||
client
|
client
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
def newClient(host: String): Client = asyncHttpClient match {
|
def newClient(host: String): Client = asyncHttpClient match {
|
||||||
case true => {
|
case true => {
|
||||||
import org.sonatype.spice.jersey.client.ahc.config.DefaultAhcConfig
|
import org.sonatype.spice.jersey.client.ahc.config.DefaultAhcConfig
|
||||||
import org.sonatype.spice.jersey.client.ahc.AhcHttpClient
|
import org.sonatype.spice.jersey.client.ahc.AhcHttpClient
|
||||||
import com.ning.http.client.Realm
|
import com.ning.http.client.Realm
|
||||||
|
|
||||||
val config: DefaultAhcConfig = new DefaultAhcConfig()
|
val config: DefaultAhcConfig = new DefaultAhcConfig()
|
||||||
if (!authScheme.isEmpty) {
|
if (!authScheme.isEmpty) {
|
||||||
val authSchemeEnum = Realm.AuthScheme.valueOf(authScheme)
|
val authSchemeEnum = Realm.AuthScheme.valueOf(authScheme)
|
||||||
config.getAsyncHttpClientConfigBuilder
|
config.getAsyncHttpClientConfigBuilder
|
||||||
.setRealm(new Realm.RealmBuilder().setScheme(authSchemeEnum)
|
.setRealm(new Realm.RealmBuilder().setScheme(authSchemeEnum)
|
||||||
.setUsePreemptiveAuth(authPreemptive).build)
|
.setUsePreemptiveAuth(authPreemptive).build)
|
||||||
}
|
}
|
||||||
AhcHttpClient.create(config)
|
AhcHttpClient.create(config)
|
||||||
}
|
}
|
||||||
case _ => Client.create()
|
case _ => Client.create()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
object ApiInvoker extends ApiInvoker(mapper = ScalaJsonUtil.getJsonMapper,
|
object ApiInvoker extends ApiInvoker(mapper = ScalaJsonUtil.getJsonMapper,
|
||||||
httpHeaders = HashMap(),
|
httpHeaders = HashMap(),
|
||||||
hostMap = HashMap(),
|
hostMap = HashMap(),
|
||||||
asyncHttpClient = {{asyncHttpClient}},
|
asyncHttpClient = {{asyncHttpClient}},
|
||||||
authScheme = "{{authScheme}}",
|
authScheme = "{{authScheme}}",
|
||||||
authPreemptive = {{authPreemptive}})
|
authPreemptive = {{authPreemptive}})
|
||||||
|
|
||||||
class ApiException(val code: Int, msg: String) extends RuntimeException(msg)
|
class ApiException(val code: Int, msg: String) extends RuntimeException(msg)
|
||||||
|
|
||||||
|
@ -5,11 +5,11 @@ package {{package}}
|
|||||||
|
|
||||||
{{#models}}
|
{{#models}}
|
||||||
|
|
||||||
{{#model}}
|
{{#model}}
|
||||||
|
|
||||||
case class {{classname}} (
|
case class {{classname}} (
|
||||||
{{#vars}}{{#description}}/* {{{description}}} */
|
{{#vars}}{{#description}}/* {{{description}}} */
|
||||||
{{/description}}{{name}}: {{{datatype}}}{{#hasMore}},{{/hasMore}}{{^hasMore}}){{/hasMore}}
|
{{/description}}{{name}}: {{{datatype}}}{{#hasMore}},{{/hasMore}}{{^hasMore}}){{/hasMore}}
|
||||||
{{/vars}}
|
{{/vars}}
|
||||||
{{/model}}
|
{{/model}}
|
||||||
{{/models}}
|
{{/models}}
|
@ -81,8 +81,7 @@
|
|||||||
</goals>
|
</goals>
|
||||||
<configuration>
|
<configuration>
|
||||||
<sources>
|
<sources>
|
||||||
<source>
|
<source>src/main/java</source>
|
||||||
src/main/java</source>
|
|
||||||
</sources>
|
</sources>
|
||||||
</configuration>
|
</configuration>
|
||||||
</execution>
|
</execution>
|
||||||
@ -94,8 +93,7 @@
|
|||||||
</goals>
|
</goals>
|
||||||
<configuration>
|
<configuration>
|
||||||
<sources>
|
<sources>
|
||||||
<source>
|
<source>src/test/java</source>
|
||||||
src/test/java</source>
|
|
||||||
</sources>
|
</sources>
|
||||||
</configuration>
|
</configuration>
|
||||||
</execution>
|
</execution>
|
||||||
@ -106,8 +104,7 @@
|
|||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
<version>2.3.2</version>
|
<version>2.3.2</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<source>
|
<source>1.6</source>
|
||||||
1.6</source>
|
|
||||||
<target>1.6</target>
|
<target>1.6</target>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user