add support to multipart form data and urlencoded for android client

This commit is contained in:
Ido Shamun 2014-10-21 01:58:31 +03:00
parent 12eba440ae
commit 5e4e838a48
2 changed files with 84 additions and 7 deletions

View File

@ -8,6 +8,14 @@ import {{invokerPackage}}.ApiInvoker;
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}}";
@ -29,12 +37,20 @@ public class {{classname}} {
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}}) {
@ -56,10 +72,43 @@ public class {{classname}} {
{{#headerParams}}headerParams.put("{{baseName}}", {{paramName}});
{{/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 {
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){
return {{#returnType}}({{{returnType}}}) ApiInvoker.deserialize(response, "{{returnContainer}}", {{returnBaseType}}.class){{/returnType}};
}

View File

@ -12,7 +12,9 @@ import org.apache.http.client.methods.*;
import org.apache.http.conn.*;
import org.apache.http.conn.scheme.*;
import org.apache.http.conn.ssl.*;
import org.apache.http.entity.mime.*;
import org.apache.http.entity.StringEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.impl.client.*;
import org.apache.http.impl.conn.*;
import org.apache.http.params.*;
@ -156,11 +158,24 @@ public class ApiInvoker {
}
else if ("POST".equals(method)) {
HttpPost post = new HttpPost(url);
if (body != null) {
if("application/x-www-form-urlencoded".equals(contentType)) {
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()) {
post.setHeader(key, headers.get(key));
}
@ -169,9 +184,22 @@ public class ApiInvoker {
else if ("PUT".equals(method)) {
HttpPut put = new HttpPut(url);
if(body != null) {
if("application/x-www-form-urlencoded".equals(contentType)) {
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()) {
put.setHeader(key, headers.get(key));
}