Files
openapi-generator/conf/java/templates/ResourceObject.st
rpidikiti 9b1d1a9dad 1. Added list imports for java and scala model objects
2. Added logic to parse lists and convert them to coma separated values for query string input
3. For java class name generator remove . if the same exists in the class name
4. If input model is already generated then ignore the same.
2011-11-10 14:34:09 -08:00

153 lines
5.1 KiB
Smalltalk

/**
* Copyright 2011 Wordnik, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package $packageName$;
import $modelPackageName$.*;
import org.codehaus.jackson.map.DeserializationConfig.Feature;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import com.wordnik.swagger.runtime.annotations.*;
import com.wordnik.swagger.runtime.common.*;
import com.wordnik.swagger.runtime.exception.*;
import java.util.*;
import java.lang.Long;
import java.io.IOException;
$imports:{ import |
import $import$;
}$
/**
*
* NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually.
* @author ramesh
*
*/
public class $resource$ extends $extends$ {
private static APIInvoker apiInvoker = null;
$methods:{ method |
/**
* $method.title$
*
$if(method.description)$
* $method.description$
* $endif$
* $method.arguments:{ argument |@param $argument.name$ $argument.description$
* $if(argument.allowedValues)$
* Allowed values are - $argument.allowedValues$ $endif$ }$
$if(!method.responseVoid)$
* @return $method.returnValue$ {@link $method.returnClassName$} $endif$
* @throws APIException $method.exceptionDescription$
*/
$if(method.hasArguments)$
@MethodArgumentNames(value="$method.argumentNames; separator=", "$")
$endif$
public static $method.returnValue$ $method.name$($method.argumentDefinitions; separator=", "$) throws APIException {
//parse inputs
String resourcePath = "$method.resourcePath$";
resourcePath = resourcePath.replace("{format}","json");
String method = "$method.methodType$";
Map<String, String> queryParams = new HashMap<String, String>();
Map<String, String> headerParams = new HashMap<String, String>();
$if(!method.inputModel)$
$method.queryParameters:{ argument |
if( $argument.name$ != null) {
queryParams.put("$argument.name$", APIInvoker.toPathValue($argument.name$));
}
}$
$method.pathParameters:{ argument |
if( $argument.name$ != null) {
resourcePath = resourcePath.replace("{$argument.name$}", $argument.name$);
}
}$
$method.headerParameters:{ argument |
if( $argument.name$ != null) {
headerParams.put("$argument.name$", APIInvoker.toPathValue($argument.name$));
}
}$
$endif$
$if(method.inputModel)$
$method.queryParameters:{ argument |
if( $argument.inputModelClassArgument$ != null && $argument.methodNameFromModelClass$ != null) {
queryParams.put("$argument.name$", APIInvoker.toPathValue($argument.methodNameFromModelClass$));
}
}$
$method.pathParameters:{ argument |
if( $argument.inputModelClassArgument$ != null && $argument.methodNameFromModelClass$ != null) {
resourcePath = resourcePath.replace("{$argument.name$}", $argument.methodNameFromModelClass$);
}
}$
$method.headerParameters:{ argument |
if( $argument.inputModelClassArgument$ != null && $argument.methodNameFromModelClass$ != null) {
headerParams.put("$argument.name$", APIInvoker.toPathValue($argument.methodNameFromModelClass$));
}
}$
$endif$
//make the API Call
$if(method.postObject)$
String response = getApiInvoker().invokeAPI(resourcePath, method, queryParams, postData, headerParams);
$endif$
$if(!method.postObject)$
String response = getApiInvoker().invokeAPI(resourcePath, method, queryParams, null, headerParams);
$endif$
$if(!method.responseVoid)$
if(response == null || response.length() == 0){
return null;
}
$if(!method.returnValueList)$
//create output objects if the response has more than one object
$method.returnValue$ responseObject = ($method.returnValue$) getApiInvoker().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$>) getApiInvoker().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$
}
}$
public static APIInvoker getApiInvoker() {
if(apiInvoker == null){
apiInvoker = APIInvoker.getApiInvoker();
}
return apiInvoker;
}
public static void setApiInvoker(APIInvoker invoker) {
apiInvoker = invoker;
}
}