mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-06-06 00:40:52 +00:00
to make it clear and also allow a query parameter to be set with string value: "null" (4 characters string)
110 lines
3.6 KiB
Plaintext
110 lines
3.6 KiB
Plaintext
package {{package}};
|
|
|
|
import {{invokerPackage}}.ApiException;
|
|
import {{invokerPackage}}.ApiInvoker;
|
|
|
|
import {{modelPackage}}.*;
|
|
|
|
import java.util.*;
|
|
|
|
{{#imports}}import {{import}};
|
|
{{/imports}}
|
|
|
|
import com.sun.jersey.multipart.FormDataMultiPart;
|
|
|
|
import javax.ws.rs.core.MediaType;
|
|
|
|
import java.io.File;
|
|
import java.util.Map;
|
|
import java.util.HashMap;
|
|
|
|
{{#operations}}
|
|
public class {{classname}} {
|
|
String basePath = "{{basePath}}";
|
|
ApiInvoker apiInvoker = ApiInvoker.getInstance();
|
|
|
|
public ApiInvoker getInvoker() {
|
|
return apiInvoker;
|
|
}
|
|
|
|
public void setBasePath(String basePath) {
|
|
this.basePath = basePath;
|
|
}
|
|
|
|
public String getBasePath() {
|
|
return basePath;
|
|
}
|
|
|
|
{{#operation}}
|
|
{{#errorList}} //error info- code: {{code}} reason: "{{reason}}" model: {{#responseModel}}{{responseModel}}
|
|
{{/responseModel}}{{^responseModel}}<none>
|
|
{{/responseModel}}
|
|
{{/errorList}}
|
|
public {{#returnType}}{{{returnType}}} {{/returnType}}{{^returnType}}void {{/returnType}}{{nickname}} ({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) throws ApiException {
|
|
Object postBody = {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}};
|
|
{{#requiredParamCount}}
|
|
// verify required params are set
|
|
if({{/requiredParamCount}}{{#requiredParams}} {{paramName}} == null {{#hasMore}}|| {{/hasMore}}{{/requiredParams}}{{#requiredParamCount}}) {
|
|
throw new ApiException(400, "missing required params");
|
|
}
|
|
{{/requiredParamCount}}
|
|
|
|
// create path and map variables
|
|
String path = "{{path}}".replaceAll("\\{format\\}","json"){{#pathParams}}
|
|
.replaceAll("\\{" + "{{paramName}}" + "\\}", apiInvoker.escapeString({{{paramName}}}.toString())){{/pathParams}};
|
|
|
|
// query params
|
|
Map<String, String> queryParams = new HashMap<String, String>();
|
|
Map<String, String> headerParams = new HashMap<String, String>();
|
|
Map<String, String> formParams = new HashMap<String, String>();
|
|
|
|
{{#queryParams}}if ({{paramName}} != null)
|
|
queryParams.put("{{baseName}}", String.valueOf({{paramName}}));
|
|
{{/queryParams}}
|
|
{{#headerParams}}headerParams.put("{{baseName}}", {{paramName}});
|
|
{{/headerParams}}
|
|
String[] contentTypes = {
|
|
{{#consumes}}"{{mediaType}}"{{#hasMore}},{{/hasMore}}{{/consumes}}
|
|
};
|
|
|
|
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
|
|
|
|
if(contentType.startsWith("multipart/form-data")) {
|
|
boolean hasFields = false;
|
|
FormDataMultiPart mp = new FormDataMultiPart();
|
|
{{#formParams}}{{#notFile}}
|
|
hasFields = true;
|
|
mp.field("{{baseName}}", {{paramName}}, MediaType.MULTIPART_FORM_DATA_TYPE);
|
|
{{/notFile}}{{#isFile}}
|
|
hasFields = true;
|
|
mp.field("{{baseName}}", {{paramName}}, MediaType.MULTIPART_FORM_DATA_TYPE);
|
|
{{/isFile}}{{/formParams}}
|
|
if(hasFields)
|
|
postBody = mp;
|
|
}
|
|
else {
|
|
{{#formParams}}{{#notFile}}formParams.put("{{baseName}}", {{paramName}});{{/notFile}}
|
|
{{/formParams}}
|
|
}
|
|
|
|
try {
|
|
String response = apiInvoker.invokeAPI(basePath, path, "{{httpMethod}}", queryParams, postBody, headerParams, formParams, contentType);
|
|
if(response != null){
|
|
return {{#returnType}}({{{returnType}}}) ApiInvoker.deserialize(response, "{{returnContainer}}", {{returnBaseType}}.class){{/returnType}};
|
|
}
|
|
else {
|
|
return {{#returnType}}null{{/returnType}};
|
|
}
|
|
} catch (ApiException ex) {
|
|
if(ex.getCode() == 404) {
|
|
return {{#returnType}} null{{/returnType}};
|
|
}
|
|
else {
|
|
throw ex;
|
|
}
|
|
}
|
|
}
|
|
{{/operation}}
|
|
}
|
|
{{/operations}}
|