forked from loafle/openapi-generator-original
add support to multipart form data and urlencoded for android client
This commit is contained in:
parent
12eba440ae
commit
5e4e838a48
@ -8,6 +8,14 @@ import {{invokerPackage}}.ApiInvoker;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.io.File;
|
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}}
|
{{#operations}}
|
||||||
public class {{classname}} {
|
public class {{classname}} {
|
||||||
String basePath = "{{basePath}}";
|
String basePath = "{{basePath}}";
|
||||||
@ -29,12 +37,20 @@ public class {{classname}} {
|
|||||||
return basePath;
|
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}}
|
{{#operation}}
|
||||||
{{#errorList}} //error info- code: {{code}} reason: "{{reason}}" model: {{#responseModel}}{{responseModel}}
|
{{#errorList}} //error info- code: {{code}} reason: "{{reason}}" model: {{#responseModel}}{{responseModel}}
|
||||||
{{/responseModel}}{{^responseModel}}<none>
|
{{/responseModel}}{{^responseModel}}<none>
|
||||||
{{/responseModel}}
|
{{/responseModel}}
|
||||||
{{/errorList}}
|
{{/errorList}}
|
||||||
public {{#returnType}}{{{returnType}}} {{/returnType}}{{^returnType}}void {{/returnType}} {{nickname}} ({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) throws ApiException {
|
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}}
|
{{#requiredParamCount}}
|
||||||
// verify required params are set
|
// verify required params are set
|
||||||
if({{/requiredParamCount}}{{#requiredParams}} {{paramName}} == null {{#hasMore}}|| {{/hasMore}}{{/requiredParams}}{{#requiredParamCount}}) {
|
if({{/requiredParamCount}}{{#requiredParams}} {{paramName}} == null {{#hasMore}}|| {{/hasMore}}{{/requiredParams}}{{#requiredParamCount}}) {
|
||||||
@ -56,10 +72,43 @@ public class {{classname}} {
|
|||||||
{{#headerParams}}headerParams.put("{{baseName}}", {{paramName}});
|
{{#headerParams}}headerParams.put("{{baseName}}", {{paramName}});
|
||||||
{{/headerParams}}
|
{{/headerParams}}
|
||||||
|
|
||||||
String contentType = "application/json";
|
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}}
|
||||||
|
hasFields = true;
|
||||||
|
mp.add(new BasicNameValuePair("{{baseName}}", {{paramName}}));
|
||||||
|
{{/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;
|
||||||
|
builder.addTextBody("{{baseName}}", {{paramName}}.toString());
|
||||||
|
{{/formParams}}
|
||||||
|
{{#bodyParam}}
|
||||||
|
hasFields = true;
|
||||||
|
builder.addBinaryBody("file", {{bodyParam}}, ContentType.create(getMimeType({{bodyParam}})), {{bodyParam}}.getName());
|
||||||
|
{{/bodyParam}}
|
||||||
|
if(hasFields)
|
||||||
|
postBody = builder;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
postBody = {{#bodyParam}}{{bodyParam}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}};
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String response = apiInvoker.invokeAPI(basePath, path, "{{httpMethod}}", queryParams, {{#bodyParam}}{{bodyParam}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}}, headerParams, contentType);
|
String response = apiInvoker.invokeAPI(basePath, path, "{{httpMethod}}", queryParams, postBody, headerParams, contentType);
|
||||||
if(response != null){
|
if(response != null){
|
||||||
return {{#returnType}}({{{returnType}}}) ApiInvoker.deserialize(response, "{{returnContainer}}", {{returnBaseType}}.class){{/returnType}};
|
return {{#returnType}}({{{returnType}}}) ApiInvoker.deserialize(response, "{{returnContainer}}", {{returnBaseType}}.class){{/returnType}};
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,9 @@ import org.apache.http.client.methods.*;
|
|||||||
import org.apache.http.conn.*;
|
import org.apache.http.conn.*;
|
||||||
import org.apache.http.conn.scheme.*;
|
import org.apache.http.conn.scheme.*;
|
||||||
import org.apache.http.conn.ssl.*;
|
import org.apache.http.conn.ssl.*;
|
||||||
|
import org.apache.http.entity.mime.*;
|
||||||
import org.apache.http.entity.StringEntity;
|
import org.apache.http.entity.StringEntity;
|
||||||
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||||
import org.apache.http.impl.client.*;
|
import org.apache.http.impl.client.*;
|
||||||
import org.apache.http.impl.conn.*;
|
import org.apache.http.impl.conn.*;
|
||||||
import org.apache.http.params.*;
|
import org.apache.http.params.*;
|
||||||
@ -156,10 +158,23 @@ public class ApiInvoker {
|
|||||||
}
|
}
|
||||||
else if ("POST".equals(method)) {
|
else if ("POST".equals(method)) {
|
||||||
HttpPost post = new HttpPost(url);
|
HttpPost post = new HttpPost(url);
|
||||||
|
|
||||||
if (body != null) {
|
if (body != null) {
|
||||||
post.setHeader("Content-Type", contentType);
|
if("application/x-www-form-urlencoded".equals(contentType)) {
|
||||||
post.setEntity(new StringEntity(serialize(body), "UTF-8"));
|
post.setHeader("Content-Type", contentType);
|
||||||
|
post.setEntity(new UrlEncodedFormEntity((List<NameValuePair>)body));
|
||||||
|
}
|
||||||
|
else if("multipart/form-data".equals(contentType)) {
|
||||||
|
String boundary = "-------------" + System.currentTimeMillis();
|
||||||
|
post.setHeader("Content-type", "multipart/form-data; boundary="+boundary);
|
||||||
|
MultipartEntityBuilder builder = (MultipartEntityBuilder)body;
|
||||||
|
builder.setBoundary(boundary);
|
||||||
|
post.setEntity(builder.build());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
post.setHeader("Content-Type", contentType);
|
||||||
|
post.setEntity(new StringEntity(serialize(body), "UTF-8"));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
for(String key : headers.keySet()) {
|
for(String key : headers.keySet()) {
|
||||||
post.setHeader(key, headers.get(key));
|
post.setHeader(key, headers.get(key));
|
||||||
@ -169,8 +184,21 @@ public class ApiInvoker {
|
|||||||
else if ("PUT".equals(method)) {
|
else if ("PUT".equals(method)) {
|
||||||
HttpPut put = new HttpPut(url);
|
HttpPut put = new HttpPut(url);
|
||||||
if(body != null) {
|
if(body != null) {
|
||||||
put.setHeader("Content-Type", contentType);
|
if("application/x-www-form-urlencoded".equals(contentType)) {
|
||||||
put.setEntity(new StringEntity(serialize(body), "UTF-8"));
|
put.setHeader("Content-Type", contentType);
|
||||||
|
put.setEntity(new UrlEncodedFormEntity((List<NameValuePair>)body));
|
||||||
|
}
|
||||||
|
else if("multipart/form-data".equals(contentType)) {
|
||||||
|
String boundary = "-------------" + System.currentTimeMillis();
|
||||||
|
put.setHeader("Content-type", "multipart/form-data; boundary="+boundary);
|
||||||
|
MultipartEntityBuilder builder = (MultipartEntityBuilder)body;
|
||||||
|
builder.setBoundary(boundary);
|
||||||
|
put.setEntity(builder.build());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
put.setHeader("Content-Type", contentType);
|
||||||
|
put.setEntity(new StringEntity(serialize(body), "UTF-8"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for(String key : headers.keySet()) {
|
for(String key : headers.keySet()) {
|
||||||
put.setHeader(key, headers.get(key));
|
put.setHeader(key, headers.get(key));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user