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.config.DefaultClientConfig;
|
||||||
import com.sun.jersey.api.client.filter.LoggingFilter;
|
import com.sun.jersey.api.client.filter.LoggingFilter;
|
||||||
import com.sun.jersey.api.client.WebResource.Builder;
|
import com.sun.jersey.api.client.WebResource.Builder;
|
||||||
|
|
||||||
import com.sun.jersey.multipart.FormDataMultiPart;
|
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.Response.Status.Family;
|
||||||
import javax.ws.rs.core.MediaType;
|
import javax.ws.rs.core.MediaType;
|
||||||
@ -29,6 +31,7 @@ import java.util.TimeZone;
|
|||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.File;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
|
|
||||||
import java.text.DateFormat;
|
import java.text.DateFormat;
|
||||||
@ -398,7 +401,7 @@ public class ApiClient {
|
|||||||
* @param authNames The authentications to apply
|
* @param authNames The authentications to apply
|
||||||
* @return The response body in type of string
|
* @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);
|
updateParamsForAuth(authNames, queryParams, headerParams);
|
||||||
|
|
||||||
Client client = getClient();
|
Client client = getClient();
|
||||||
@ -433,73 +436,73 @@ public class ApiClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
ClientResponse response = null;
|
||||||
|
|
||||||
if ("GET".equals(method)) {
|
if ("GET".equals(method)) {
|
||||||
response = (ClientResponse) builder.get(ClientResponse.class);
|
response = (ClientResponse) builder.get(ClientResponse.class);
|
||||||
}
|
} else if ("POST".equals(method)) {
|
||||||
else if ("POST".equals(method)) {
|
if (encodedFormParams != null) {
|
||||||
if (contentType.startsWith("application/x-www-form-urlencoded")) {
|
response = builder.type(contentType).post(ClientResponse.class, encodedFormParams);
|
||||||
String encodedFormParams = this
|
|
||||||
.getXWWWFormUrlencodedParams(formParams);
|
|
||||||
response = builder.type(contentType).post(ClientResponse.class,
|
|
||||||
encodedFormParams);
|
|
||||||
} else if (body == null) {
|
} else if (body == null) {
|
||||||
response = builder.post(ClientResponse.class, 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);
|
response = builder.type(contentType).post(ClientResponse.class, body);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
response = builder.type(contentType).post(ClientResponse.class, serialize(body));
|
response = builder.type(contentType).post(ClientResponse.class, serialize(body));
|
||||||
}
|
}
|
||||||
else if ("PUT".equals(method)) {
|
} else if ("PUT".equals(method)) {
|
||||||
if ("application/x-www-form-urlencoded".equals(contentType)) {
|
if (encodedFormParams != null) {
|
||||||
String encodedFormParams = this
|
response = builder.type(contentType).put(ClientResponse.class, encodedFormParams);
|
||||||
.getXWWWFormUrlencodedParams(formParams);
|
|
||||||
response = builder.type(contentType).put(ClientResponse.class,
|
|
||||||
encodedFormParams);
|
|
||||||
} else if(body == null) {
|
} else if(body == null) {
|
||||||
response = builder.put(ClientResponse.class, serialize(body));
|
response = builder.put(ClientResponse.class, serialize(body));
|
||||||
} else {
|
} else {
|
||||||
response = builder.type(contentType).put(ClientResponse.class, serialize(body));
|
response = builder.type(contentType).put(ClientResponse.class, serialize(body));
|
||||||
}
|
}
|
||||||
}
|
} else if ("DELETE".equals(method)) {
|
||||||
else if ("DELETE".equals(method)) {
|
if (encodedFormParams != null) {
|
||||||
if ("application/x-www-form-urlencoded".equals(contentType)) {
|
response = builder.type(contentType).delete(ClientResponse.class, encodedFormParams);
|
||||||
String encodedFormParams = this
|
|
||||||
.getXWWWFormUrlencodedParams(formParams);
|
|
||||||
response = builder.type(contentType).delete(ClientResponse.class,
|
|
||||||
encodedFormParams);
|
|
||||||
} else if(body == null) {
|
} else if(body == null) {
|
||||||
response = builder.delete(ClientResponse.class);
|
response = builder.delete(ClientResponse.class);
|
||||||
} else {
|
} else {
|
||||||
response = builder.type(contentType).delete(ClientResponse.class, serialize(body));
|
response = builder.type(contentType).delete(ClientResponse.class, serialize(body));
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
throw new ApiException(500, "unknown method type " + method);
|
throw new ApiException(500, "unknown method type " + method);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (response.getStatusInfo() == ClientResponse.Status.NO_CONTENT) {
|
if (response.getStatusInfo() == ClientResponse.Status.NO_CONTENT) {
|
||||||
return null;
|
return null;
|
||||||
}
|
} else if (response.getStatusInfo().getFamily() == Family.SUCCESSFUL) {
|
||||||
else if(response.getStatusInfo().getFamily() == Family.SUCCESSFUL) {
|
|
||||||
if (response.hasEntity()) {
|
if (response.hasEntity()) {
|
||||||
return (String) response.getEntity(String.class);
|
return (String) response.getEntity(String.class);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
String message = "error";
|
String message = "error";
|
||||||
String respBody = null;
|
String respBody = null;
|
||||||
if (response.hasEntity()) {
|
if (response.hasEntity()) {
|
||||||
try {
|
try {
|
||||||
respBody = String.valueOf(response.getEntity(String.class));
|
respBody = String.valueOf(response.getEntity(String.class));
|
||||||
message = respBody;
|
message = respBody;
|
||||||
}
|
} catch (RuntimeException e) {
|
||||||
catch (RuntimeException e) {
|
|
||||||
// e.printStackTrace();
|
// e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -527,15 +530,14 @@ public class ApiClient {
|
|||||||
/**
|
/**
|
||||||
* Encode the given form parameters as request body.
|
* 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();
|
StringBuilder formParamBuilder = new StringBuilder();
|
||||||
|
|
||||||
for (Entry<String, String> param : formParams.entrySet()) {
|
for (Entry<String, Object> param : formParams.entrySet()) {
|
||||||
String keyStr = parameterToString(param.getKey());
|
String keyStr = param.getKey();
|
||||||
String valueStr = parameterToString(param.getValue());
|
String valueStr = parameterToString(param.getValue());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
formParamBuilder.append(URLEncoder.encode(keyStr, "utf8"))
|
formParamBuilder.append(URLEncoder.encode(param.getKey(), "utf8"))
|
||||||
.append("=")
|
.append("=")
|
||||||
.append(URLEncoder.encode(valueStr, "utf8"));
|
.append(URLEncoder.encode(valueStr, "utf8"));
|
||||||
formParamBuilder.append("&");
|
formParamBuilder.append("&");
|
||||||
@ -543,11 +545,12 @@ public class ApiClient {
|
|||||||
// move on to next
|
// move on to next
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
String encodedFormParams = formParamBuilder.toString();
|
String encodedFormParams = formParamBuilder.toString();
|
||||||
if (encodedFormParams.endsWith("&")) {
|
if (encodedFormParams.endsWith("&")) {
|
||||||
encodedFormParams = encodedFormParams.substring(0,
|
encodedFormParams = encodedFormParams.substring(0, encodedFormParams.length() - 1);
|
||||||
encodedFormParams.length() - 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return encodedFormParams;
|
return encodedFormParams;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,11 +12,6 @@ import java.util.*;
|
|||||||
{{#imports}}import {{import}};
|
{{#imports}}import {{import}};
|
||||||
{{/imports}}
|
{{/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.io.File;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -64,7 +59,7 @@ public class {{classname}} {
|
|||||||
// query params
|
// query params
|
||||||
List<Pair> queryParams = new ArrayList<Pair>();
|
List<Pair> queryParams = new ArrayList<Pair>();
|
||||||
Map<String, String> headerParams = new HashMap<String, String>();
|
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}}
|
||||||
queryParams.addAll(apiClient.parameterToPairs("{{#collectionFormat}}{{{collectionFormat}}}{{/collectionFormat}}", "{{baseName}}", {{paramName}}));
|
queryParams.addAll(apiClient.parameterToPairs("{{#collectionFormat}}{{{collectionFormat}}}{{/collectionFormat}}", "{{baseName}}", {{paramName}}));
|
||||||
@ -74,6 +69,10 @@ public class {{classname}} {
|
|||||||
headerParams.put("{{baseName}}", apiClient.parameterToString({{paramName}}));
|
headerParams.put("{{baseName}}", apiClient.parameterToString({{paramName}}));
|
||||||
{{/headerParams}}
|
{{/headerParams}}
|
||||||
|
|
||||||
|
{{#formParams}}if ({{paramName}} != null)
|
||||||
|
formParams.put("{{baseName}}", {{paramName}});
|
||||||
|
{{/formParams}}
|
||||||
|
|
||||||
final String[] accepts = {
|
final String[] accepts = {
|
||||||
{{#produces}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/produces}}
|
{{#produces}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/produces}}
|
||||||
};
|
};
|
||||||
@ -84,30 +83,6 @@ public class {{classname}} {
|
|||||||
};
|
};
|
||||||
final String contentType = apiClient.selectHeaderContentType(contentTypes);
|
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 {
|
try {
|
||||||
String[] authNames = new String[] { {{#authMethods}}"{{name}}"{{#hasMore}}, {{/hasMore}}{{/authMethods}} };
|
String[] authNames = new String[] { {{#authMethods}}"{{name}}"{{#hasMore}}, {{/hasMore}}{{/authMethods}} };
|
||||||
String response = apiClient.invokeAPI(path, "{{httpMethod}}", queryParams, postBody, headerParams, formParams, accept, contentType, authNames);
|
String response = apiClient.invokeAPI(path, "{{httpMethod}}", queryParams, postBody, headerParams, formParams, accept, contentType, authNames);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user