From ea15cad8d5933592555c3823246f2baec90a0ad0 Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Mon, 16 Sep 2013 16:24:24 -0700 Subject: [PATCH] fixes for position, and null arg removal --- src/main/resources/scala/api.mustache | 5 ++- src/main/resources/scala/apiInvoker.mustache | 44 +++++++++++++------- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/main/resources/scala/api.mustache b/src/main/resources/scala/api.mustache index 494b597c8be..d277a69bddf 100644 --- a/src/main/resources/scala/api.mustache +++ b/src/main/resources/scala/api.mustache @@ -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") } diff --git a/src/main/resources/scala/apiInvoker.mustache b/src/main/resources/scala/apiInvoker.mustache index 30a55b80d38..5c6d60d37b5 100644 --- a/src/main/resources/scala/apiInvoker.mustache +++ b/src/main/resources/scala/apiInvoker.mustache @@ -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)