Changes to Scala ResourceObject: (1) If return type is primitive, wrap it in option. (2) If APIInvoker returns null or raises 404 exception return None if primitive return or List() if collection.

This commit is contained in:
Colin Pollock 2012-02-28 01:08:56 -08:00
parent a4e8a18fd6
commit d4a8287a40

View File

@ -62,8 +62,7 @@ $if(method.hasArguments)$
$endif$ $endif$
@throws(classOf[APIException]) @throws(classOf[APIException])
def $method.name$($method.argumentDefinitions; separator=", "$) $if(method.hasResponseValue)$:$method.returnValue$$endif$ = { def $method.name$($method.argumentDefinitions; separator=", "$)$if(method.hasResponseValue)$: $if(method.returnValueList)$$method.returnValue$$else$Option[$method.returnValue$]$endif$$endif$ = {
//parse inputs //parse inputs
var resourcePath = "$method.resourcePath$".replace("{format}","json") var resourcePath = "$method.resourcePath$".replace("{format}","json")
val method = "$method.methodType$"; val method = "$method.methodType$";
@ -106,11 +105,20 @@ $method.pathParameters:{ argument |
$endif$ $endif$
//make the API Call //make the API Call
$if(method.hasResponseValue)$ $if(method.hasResponseValue)$
var response: String = null
try {
$if(method.postObject)$ $if(method.postObject)$
val response = apiInvoker.invokeAPI(resourcePath, method, queryParams, postData, headerParams) response = apiInvoker.invokeAPI(resourcePath, method, queryParams, postData, headerParams)
$else$ $else$
val response = apiInvoker.invokeAPI(resourcePath, method, queryParams, null, headerParams) response = apiInvoker.invokeAPI(resourcePath, method, queryParams, null, headerParams)
$endif$ $endif$
} catch {
case ex: APIException if ex.getCode == 404 =>
$if(method.returnValueList)$ return List()
$else$ return None
$endif$
case ex: APIException => throw ex
}
$else$ $else$
$if(method.postObject)$ $if(method.postObject)$
apiInvoker.invokeAPI(resourcePath, method, queryParams, postData, headerParams) apiInvoker.invokeAPI(resourcePath, method, queryParams, postData, headerParams)
@ -121,15 +129,22 @@ $endif$
$if(!method.responseVoid)$ $if(!method.responseVoid)$
$if(method.hasResponseValue)$ $if(method.hasResponseValue)$
if(null == response || response.length() == 0){ $if(!method.returnValueList)$
return null if(null == response){
return None
} }
$endif$ $endif$
$endif$
$if(!method.returnValueList)$ $if(!method.returnValueList)$
$if(method.hasResponseValue)$ $if(method.hasResponseValue)$
//create output objects if the response has more than one object //create output objects if the response has more than one object
val responseObject = APIInvoker.deserialize(response, classOf[$method.returnClassName$]).asInstanceOf[$method.returnValue$] val responseObject = APIInvoker.deserialize(response, classOf[$method.returnClassName$]).asInstanceOf[$method.returnValue$]
$if(method.returnValueList)$
responseObject responseObject
$else$
Some(responseObject)
$endif$
$endif$ $endif$
$endif$ $endif$
$if(method.returnValueList)$ $if(method.returnValueList)$