forked from loafle/openapi-generator-original
Java client: move form params handling to ApiClient
This commit is contained in:
parent
55be0330e3
commit
112a7ec8c1
@ -11,7 +11,9 @@ import com.sun.jersey.api.client.config.ClientConfig;
|
||||
import com.sun.jersey.api.client.config.DefaultClientConfig;
|
||||
import com.sun.jersey.api.client.filter.LoggingFilter;
|
||||
import com.sun.jersey.api.client.WebResource.Builder;
|
||||
|
||||
import com.sun.jersey.multipart.FormDataMultiPart;
|
||||
import com.sun.jersey.multipart.file.FileDataBodyPart;
|
||||
|
||||
import javax.ws.rs.core.Response.Status.Family;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
@ -29,6 +31,7 @@ import java.util.TimeZone;
|
||||
import java.net.URLEncoder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.File;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
import java.text.DateFormat;
|
||||
@ -398,7 +401,7 @@ public class ApiClient {
|
||||
* @param authNames The authentications to apply
|
||||
* @return The response body in type of string
|
||||
*/
|
||||
public String invokeAPI(String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, Map<String, String> formParams, String accept, String contentType, String[] authNames) throws ApiException {
|
||||
public String invokeAPI(String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, Map<String, Object> formParams, String accept, String contentType, String[] authNames) throws ApiException {
|
||||
updateParamsForAuth(authNames, queryParams, headerParams);
|
||||
|
||||
Client client = getClient();
|
||||
@ -424,90 +427,90 @@ public class ApiClient {
|
||||
else
|
||||
builder = client.resource(basePath + path + querystring).accept(accept);
|
||||
|
||||
for(String key : headerParams.keySet()) {
|
||||
for (String key : headerParams.keySet()) {
|
||||
builder = builder.header(key, headerParams.get(key));
|
||||
}
|
||||
for(String key : defaultHeaderMap.keySet()) {
|
||||
if(!headerParams.containsKey(key)) {
|
||||
for (String key : defaultHeaderMap.keySet()) {
|
||||
if (!headerParams.containsKey(key)) {
|
||||
builder = builder.header(key, defaultHeaderMap.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
String encodedFormParams = null;
|
||||
if (contentType.startsWith("multipart/form-data")) {
|
||||
FormDataMultiPart mp = new FormDataMultiPart();
|
||||
for (Entry<String, Object> param: formParams.entrySet()) {
|
||||
if (param.getValue() instanceof File) {
|
||||
File file = (File) param.getValue();
|
||||
mp.field(param.getKey(), file.getName());
|
||||
mp.bodyPart(new FileDataBodyPart(param.getKey(), file, MediaType.MULTIPART_FORM_DATA_TYPE));
|
||||
} else {
|
||||
mp.field(param.getKey(), parameterToString(param.getValue()), MediaType.MULTIPART_FORM_DATA_TYPE);
|
||||
}
|
||||
}
|
||||
body = mp;
|
||||
} else if (contentType.startsWith("application/x-www-form-urlencoded")) {
|
||||
encodedFormParams = this.getXWWWFormUrlencodedParams(formParams);
|
||||
}
|
||||
|
||||
ClientResponse response = null;
|
||||
|
||||
if("GET".equals(method)) {
|
||||
if ("GET".equals(method)) {
|
||||
response = (ClientResponse) builder.get(ClientResponse.class);
|
||||
}
|
||||
else if ("POST".equals(method)) {
|
||||
if (contentType.startsWith("application/x-www-form-urlencoded")) {
|
||||
String encodedFormParams = this
|
||||
.getXWWWFormUrlencodedParams(formParams);
|
||||
response = builder.type(contentType).post(ClientResponse.class,
|
||||
encodedFormParams);
|
||||
} else if ("POST".equals(method)) {
|
||||
if (encodedFormParams != null) {
|
||||
response = builder.type(contentType).post(ClientResponse.class, encodedFormParams);
|
||||
} else if (body == null) {
|
||||
response = builder.post(ClientResponse.class, null);
|
||||
} else if(body instanceof FormDataMultiPart) {
|
||||
} else if (body instanceof FormDataMultiPart) {
|
||||
response = builder.type(contentType).post(ClientResponse.class, body);
|
||||
}
|
||||
else
|
||||
} else {
|
||||
response = builder.type(contentType).post(ClientResponse.class, serialize(body));
|
||||
}
|
||||
else if ("PUT".equals(method)) {
|
||||
if ("application/x-www-form-urlencoded".equals(contentType)) {
|
||||
String encodedFormParams = this
|
||||
.getXWWWFormUrlencodedParams(formParams);
|
||||
response = builder.type(contentType).put(ClientResponse.class,
|
||||
encodedFormParams);
|
||||
}
|
||||
} else if ("PUT".equals(method)) {
|
||||
if (encodedFormParams != null) {
|
||||
response = builder.type(contentType).put(ClientResponse.class, encodedFormParams);
|
||||
} else if(body == null) {
|
||||
response = builder.put(ClientResponse.class, serialize(body));
|
||||
} else {
|
||||
response = builder.type(contentType).put(ClientResponse.class, serialize(body));
|
||||
response = builder.type(contentType).put(ClientResponse.class, serialize(body));
|
||||
}
|
||||
}
|
||||
else if ("DELETE".equals(method)) {
|
||||
if ("application/x-www-form-urlencoded".equals(contentType)) {
|
||||
String encodedFormParams = this
|
||||
.getXWWWFormUrlencodedParams(formParams);
|
||||
response = builder.type(contentType).delete(ClientResponse.class,
|
||||
encodedFormParams);
|
||||
} else if ("DELETE".equals(method)) {
|
||||
if (encodedFormParams != null) {
|
||||
response = builder.type(contentType).delete(ClientResponse.class, encodedFormParams);
|
||||
} else if(body == null) {
|
||||
response = builder.delete(ClientResponse.class);
|
||||
} else {
|
||||
response = builder.type(contentType).delete(ClientResponse.class, serialize(body));
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
throw new ApiException(500, "unknown method type " + method);
|
||||
}
|
||||
|
||||
if(response.getStatusInfo() == ClientResponse.Status.NO_CONTENT) {
|
||||
if (response.getStatusInfo() == ClientResponse.Status.NO_CONTENT) {
|
||||
return null;
|
||||
}
|
||||
else if(response.getStatusInfo().getFamily() == Family.SUCCESSFUL) {
|
||||
if(response.hasEntity()) {
|
||||
} else if (response.getStatusInfo().getFamily() == Family.SUCCESSFUL) {
|
||||
if (response.hasEntity()) {
|
||||
return (String) response.getEntity(String.class);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
String message = "error";
|
||||
String respBody = null;
|
||||
if(response.hasEntity()) {
|
||||
try{
|
||||
if (response.hasEntity()) {
|
||||
try {
|
||||
respBody = String.valueOf(response.getEntity(String.class));
|
||||
message = respBody;
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
} catch (RuntimeException e) {
|
||||
// e.printStackTrace();
|
||||
}
|
||||
}
|
||||
throw new ApiException(
|
||||
response.getStatusInfo().getStatusCode(),
|
||||
message,
|
||||
response.getHeaders(),
|
||||
respBody);
|
||||
response.getStatusInfo().getStatusCode(),
|
||||
message,
|
||||
response.getHeaders(),
|
||||
respBody);
|
||||
}
|
||||
}
|
||||
|
||||
@ -527,15 +530,14 @@ public class ApiClient {
|
||||
/**
|
||||
* Encode the given form parameters as request body.
|
||||
*/
|
||||
private String getXWWWFormUrlencodedParams(Map<String, String> formParams) {
|
||||
private String getXWWWFormUrlencodedParams(Map<String, Object> formParams) {
|
||||
StringBuilder formParamBuilder = new StringBuilder();
|
||||
|
||||
for (Entry<String, String> param : formParams.entrySet()) {
|
||||
String keyStr = parameterToString(param.getKey());
|
||||
for (Entry<String, Object> param : formParams.entrySet()) {
|
||||
String keyStr = param.getKey();
|
||||
String valueStr = parameterToString(param.getValue());
|
||||
|
||||
try {
|
||||
formParamBuilder.append(URLEncoder.encode(keyStr, "utf8"))
|
||||
formParamBuilder.append(URLEncoder.encode(param.getKey(), "utf8"))
|
||||
.append("=")
|
||||
.append(URLEncoder.encode(valueStr, "utf8"));
|
||||
formParamBuilder.append("&");
|
||||
@ -543,11 +545,12 @@ public class ApiClient {
|
||||
// move on to next
|
||||
}
|
||||
}
|
||||
|
||||
String encodedFormParams = formParamBuilder.toString();
|
||||
if (encodedFormParams.endsWith("&")) {
|
||||
encodedFormParams = encodedFormParams.substring(0,
|
||||
encodedFormParams.length() - 1);
|
||||
encodedFormParams = encodedFormParams.substring(0, encodedFormParams.length() - 1);
|
||||
}
|
||||
|
||||
return encodedFormParams;
|
||||
}
|
||||
|
||||
|
@ -12,11 +12,6 @@ import java.util.*;
|
||||
{{#imports}}import {{import}};
|
||||
{{/imports}}
|
||||
|
||||
import com.sun.jersey.multipart.FormDataMultiPart;
|
||||
import com.sun.jersey.multipart.file.FileDataBodyPart;
|
||||
|
||||
import javax.ws.rs.core.MediaType;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
@ -64,7 +59,7 @@ public class {{classname}} {
|
||||
// query params
|
||||
List<Pair> queryParams = new ArrayList<Pair>();
|
||||
Map<String, String> headerParams = new HashMap<String, String>();
|
||||
Map<String, String> formParams = new HashMap<String, String>();
|
||||
Map<String, Object> formParams = new HashMap<String, Object>();
|
||||
|
||||
{{#queryParams}}
|
||||
queryParams.addAll(apiClient.parameterToPairs("{{#collectionFormat}}{{{collectionFormat}}}{{/collectionFormat}}", "{{baseName}}", {{paramName}}));
|
||||
@ -74,6 +69,10 @@ public class {{classname}} {
|
||||
headerParams.put("{{baseName}}", apiClient.parameterToString({{paramName}}));
|
||||
{{/headerParams}}
|
||||
|
||||
{{#formParams}}if ({{paramName}} != null)
|
||||
formParams.put("{{baseName}}", {{paramName}});
|
||||
{{/formParams}}
|
||||
|
||||
final String[] accepts = {
|
||||
{{#produces}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/produces}}
|
||||
};
|
||||
@ -84,30 +83,6 @@ public class {{classname}} {
|
||||
};
|
||||
final String contentType = apiClient.selectHeaderContentType(contentTypes);
|
||||
|
||||
if(contentType.startsWith("multipart/form-data")) {
|
||||
boolean hasFields = false;
|
||||
FormDataMultiPart mp = new FormDataMultiPart();
|
||||
{{#formParams}}{{#notFile}}
|
||||
if ({{paramName}} != null) {
|
||||
hasFields = true;
|
||||
mp.field("{{baseName}}", apiClient.parameterToString({{paramName}}), MediaType.MULTIPART_FORM_DATA_TYPE);
|
||||
}
|
||||
{{/notFile}}{{#isFile}}
|
||||
if ({{paramName}} != null) {
|
||||
hasFields = true;
|
||||
mp.field("{{baseName}}", {{paramName}}.getName());
|
||||
mp.bodyPart(new FileDataBodyPart("{{baseName}}", {{paramName}}, MediaType.MULTIPART_FORM_DATA_TYPE));
|
||||
}
|
||||
{{/isFile}}{{/formParams}}
|
||||
if(hasFields)
|
||||
postBody = mp;
|
||||
}
|
||||
else {
|
||||
{{#formParams}}{{#notFile}}if ({{paramName}} != null)
|
||||
formParams.put("{{baseName}}", apiClient.parameterToString({{paramName}}));{{/notFile}}
|
||||
{{/formParams}}
|
||||
}
|
||||
|
||||
try {
|
||||
String[] authNames = new String[] { {{#authMethods}}"{{name}}"{{#hasMore}}, {{/hasMore}}{{/authMethods}} };
|
||||
String response = apiClient.invokeAPI(path, "{{httpMethod}}", queryParams, postBody, headerParams, formParams, accept, contentType, authNames);
|
||||
|
Loading…
x
Reference in New Issue
Block a user