153 lines
4.7 KiB
Smalltalk

package $packageName$;
import $annotationPackageName$.MethodArgumentNames;
import $exceptionPackageName$.APIExceptionCodes;
import $exceptionPackageName$.APIException;
import $modelPackageName$.*;
import org.codehaus.jackson.map.DeserializationConfig.Feature;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import java.util.*;
import java.io.IOException;
$imports:{ import |
import $import$;
}$
/**
* NOTE: This class is auto generated by the drive code generator program so please do not edit the program manually.
* @author ramesh
*
*/
public class $resource$ extends $extends$ {
$methods:{ method |
/**
* $method.description$
$method.arguments:{ argument |
* @param $argument.name$ $argument.description$
$if(argument.allowedValues)$
* Allowed values are - $argument.allowedValues$
$endif$
}$
*
* @return $method.returnValue$ {@link $method.returnClassName$}
* @throws APIException $method.exceptionDescription$
*/
$if(method.hasArguments)$
@MethodArgumentNames(value="$method.argumentNames; separator=", "$")
$endif$
public static $method.returnValue$ $method.name$($method.argumentDefinitions; separator=", "$) throws APIException {
$if(method.authToken)$
if(authToken == null || authToken.length() == 0) {
throw new APIException(APIExceptionCodes.AUTH_TOKEN_NOT_VALID);
}
$endif$
//parse inputs
String resourcePath = "$method.resourcePath$";
resourcePath = resourcePath.replace("{format}","json");
String method = "$method.methodType$";
Map<String, String> queryParams = new HashMap<String, String>();
$if(!method.inputModel)$
$method.queryParameters:{ argument |
if( $argument.name$ != null) {
queryParams.put("$argument.name$", toPathValue($argument.name$));
}
}$
$method.pathParameters:{ argument |
if( $argument.name$ != null) {
resourcePath = resourcePath.replace("{$argument.name$}", $argument.name$);
}
}$
$endif$
$if(method.inputModel)$
$method.queryParameters:{ argument |
if( $argument.inputModelClassArgument$ != null && $argument.methodNameFromModelClass$ != null) {
queryParams.put("$argument.name$", $argument.methodNameFromModelClass$);
}
}$
$method.pathParameters:{ argument |
if( $argument.inputModelClassArgument$ != null && $argument.methodNameFromModelClass$ != null) {
resourcePath = resourcePath.replace("{$argument.name$}", $argument.methodNameFromModelClass$);
}
}$
$endif$
//make the API Call
$if(method.postObject)$
$if(method.authToken)$
String response = invokeAPI(authToken, resourcePath, method, queryParams, postObject);
$endif$
$if(!method.authToken)$
String response = invokeAPI(null, resourcePath, method, queryParams, postObject);
$endif$
$endif$
$if(!method.postObject)$
$if(method.authToken)$
String response = invokeAPI(authToken, resourcePath, method, queryParams, null);
$endif$
$if(!method.authToken)$
String response = invokeAPI(null, resourcePath, method, queryParams, null);
$endif$
$endif$
//create output objects if the response has more than one object
$if(!method.responseVoid)$
if(response == null || response.length() == 0){
return null;
}
$if(!method.returnValueList)$
$method.returnValue$ responseObject = ($method.returnValue$)deserialize(response, $method.returnClassName$.class);
return responseObject;
$endif$
$if(method.returnValueList)$
TypeReference<ArrayList<$method.returnClassName$>> typeRef = new TypeReference<ArrayList<$method.returnClassName$>>() {
};
try {
List<$method.returnClassName$> responseObject = (List<$method.returnClassName$>) mapper.readValue(response, typeRef);
return responseObject;
} catch (IOException ioe) {
String[] args = new String[]{response, typeRef.toString()};
throw new APIException(APIExceptionCodes.ERROR_CONVERTING_JSON_TO_JAVA, args, "Error in converting response json value to java object : " + ioe.getMessage(), ioe);
}
$endif$
$endif$
}
}$
/**
* Overloaded method for returning the path value
* For a string value an empty value is returned if the value is null
* @param value
* @return
*/
private static String toPathValue(String value) {
return value == null ? "" : value;
}
/**
* Overloaded method for returning a path value
* For a list of objects a comma separated string is returned
* @param objects
* @return
*/
private static String toPathValue(List objects) {
StringBuilder out = new StringBuilder();
for(Object o: objects){
out.append(o.toString());
out.append(",");
}
if(out.indexOf(",") != -1) {
return out.substring(0, out.lastIndexOf(",") );
}
return out.toString();
}
}