forked from loafle/openapi-generator-original
139 lines
4.7 KiB
Plaintext
139 lines
4.7 KiB
Plaintext
package {{package}};
|
|
|
|
import {{invokerPackage}}.ApiException;
|
|
import {{invokerPackage}}.ApiInvoker;
|
|
{{#imports}}import {{import}};
|
|
{{/imports}}
|
|
|
|
import java.util.*;
|
|
import java.io.File;
|
|
|
|
import org.apache.http.NameValuePair;
|
|
import org.apache.http.message.BasicNameValuePair;
|
|
import org.apache.http.entity.mime.*;
|
|
import org.apache.http.entity.mime.content.*;
|
|
import org.apache.http.entity.ContentType;
|
|
|
|
import android.webkit.MimeTypeMap;
|
|
|
|
{{#operations}}
|
|
public class {{classname}} {
|
|
String basePath = "{{basePath}}";
|
|
ApiInvoker apiInvoker = ApiInvoker.getInstance();
|
|
|
|
public void addHeader(String key, String value) {
|
|
getInvoker().addDefaultHeader(key, value);
|
|
}
|
|
|
|
public ApiInvoker getInvoker() {
|
|
return apiInvoker;
|
|
}
|
|
|
|
public void setBasePath(String basePath) {
|
|
this.basePath = basePath;
|
|
}
|
|
|
|
public String getBasePath() {
|
|
return basePath;
|
|
}
|
|
|
|
private static String getMimeType(File file) {
|
|
MimeTypeMap mime = MimeTypeMap.getSingleton();
|
|
int index = file.getName().lastIndexOf('.')+1;
|
|
String ext = file.getName().substring(index).toLowerCase();
|
|
return mime.getMimeTypeFromExtension(ext);
|
|
}
|
|
|
|
{{#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}}{{bodyParam}}{{/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>();
|
|
|
|
{{#queryParams}}if(!"null".equals(String.valueOf({{paramName}})))
|
|
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("application/x-www-form-urlencoded")) {
|
|
boolean hasFields = false;
|
|
List<NameValuePair> mp = new ArrayList<NameValuePair>();
|
|
{{#formParams}}
|
|
{{^isFile}}
|
|
hasFields = true;
|
|
mp.add(new BasicNameValuePair("{{baseName}}", {{paramName}}));
|
|
{{/isFile}}
|
|
{{/formParams}}
|
|
if(hasFields)
|
|
postBody = mp;
|
|
}
|
|
else if(contentType.startsWith("multipart/form-data")) {
|
|
boolean hasFields = false;
|
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
|
|
{{#formParams}}
|
|
hasFields = true;
|
|
{{^isFile}}
|
|
builder.addTextBody("{{baseName}}", {{paramName}}.toString());
|
|
{{/isFile}}
|
|
{{#isFile}}
|
|
builder.addBinaryBody("file", {{paramName}}, ContentType.create(getMimeType({{paramName}})), {{paramName}}.getName());
|
|
{{/isFile}}
|
|
{{/formParams}}
|
|
{{#bodyParams}}
|
|
{{#isFile}}
|
|
hasFields = true;
|
|
builder.addBinaryBody("file", {{paramName}}, ContentType.create(getMimeType({{paramName}})), {{paramName}}.getName());
|
|
{{/isFile}}
|
|
{{/bodyParams}}
|
|
if(hasFields)
|
|
postBody = builder;
|
|
}
|
|
else {
|
|
postBody = {{#bodyParam}}{{bodyParam}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}};
|
|
}
|
|
|
|
try {
|
|
String response = apiInvoker.invokeAPI(basePath, path, "{{httpMethod}}", queryParams, postBody, headerParams, 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}}
|