fixes for position, and null arg removal

This commit is contained in:
Tony Tam 2013-09-16 16:24:24 -07:00
parent e4a4f25f22
commit ea15cad8d5
2 changed files with 31 additions and 18 deletions

View File

@ -6,6 +6,7 @@ import {{invokerPackage}}.ApiInvoker
import {{invokerPackage}}.ApiException
import java.io.File
import java.util.Date
import scala.collection.mutable.HashMap
@ -19,7 +20,7 @@ class {{classname}} {
{{#operation}}
def {{nickname}} ({{#allParams}}{{paramName}}: {{dataType}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) {{#returnType}}: Option[{{returnType}}]{{/returnType}} = {
// create path and map variables
val path = "{{path}}".replaceAll("\\{format\\}","json"){{#pathParams}}.replaceAll("\\{" + "{{baseName}}" + "\\}",apiInvoker.escapeString({{paramName}}))
val path = "{{path}}".replaceAll("\\{format\\}","json"){{#pathParams}}.replaceAll("\\{" + "{{baseName}}" + "\\}",apiInvoker.escape({{paramName}}))
{{/pathParams}}
@ -38,7 +39,7 @@ class {{classname}} {
{{#requiredParamCount}}
// verify required params are set
(Set({{/requiredParamCount}}{{#requiredParams}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/requiredParams}}{{#requiredParamCount}}) - null).size match {
(List({{/requiredParamCount}}{{#requiredParams}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/requiredParams}}{{#requiredParamCount}}).filter(_ != null)).size match {
case {{requiredParamCount}} => // all required values set
case _ => throw new Exception("missing required params")
}

View File

@ -40,10 +40,14 @@ object ApiInvoker {
val defaultHeaders: HashMap[String, String] = HashMap()
val hostMap: HashMap[String, Client] = HashMap()
def escapeString(value: String): String = {
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 {
@ -54,8 +58,13 @@ object ApiInvoker {
case _ => null
}
} else {
containerType match {
case "List" => {
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
@ -79,12 +88,11 @@ object ApiInvoker {
} else null
}
def invokeApi(host: String, path: String, method: String, queryParams: Map[String, String], body: AnyRef, headerParams: Map[String, String], contentType: String) = {
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 => (escapeString(k._1) + "=" + escapeString(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)
headerParams.map(p => builder.header(p._1, p._2))
defaultHeaders.map(p => {
headerParams.contains(p._1) match {
@ -119,12 +127,22 @@ object ApiInvoker {
}
case _ => null
}
response.getClientResponseStatus() match {
case ClientResponse.Status.OK => response.getEntity(classOf[String])
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(),
response.getEntity(classOf[String]))
entity)
}
}
}
@ -142,11 +160,5 @@ object ApiInvoker {
}
}
class ApiException extends Exception {
var code = 0
def this(code: Int, msg: String) = {
this()
}
}
class ApiException(val code: Int, msg: String) extends RuntimeException(msg)