Skip to content

Commit 5b6cf6c

Browse files
author
BitsAdmin
committed
Merge branch 'dev/lsx/post-form' into 'integration_2024-07-18_327673148930'
feat: [development task] core-update (722480) See merge request iaasng/volcengine-java-sdk!226
2 parents b361148 + 5b029b5 commit 5b6cf6c

File tree

1 file changed

+53
-66
lines changed

1 file changed

+53
-66
lines changed

volcengine-java-sdk-core/src/main/java/com/volcengine/ApiClient.java

Lines changed: 53 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.volcengine.sign.ServiceInfo;
2626
import com.volcengine.sign.VolcstackSign;
2727
import com.volcengine.version.Version;
28+
import okio.Buffer;
2829
import okio.BufferedSink;
2930
import okio.Okio;
3031
import org.apache.commons.lang.StringUtils;
@@ -692,13 +693,13 @@ public String escapeString(String str) {
692693
* @param <T> Type
693694
* @param response HTTP response
694695
* @param returnType The type of the Java object
695-
* @param isCommon The flag for Universal
696+
* @param isCommon The flag for Universal
696697
* @return The deserialized Java object
697698
* @throws ApiException If fail to deserialize response body, i.e. cannot read response body
698699
* or the Content-Type of the response is not supported.
699700
*/
700701
@SuppressWarnings("unchecked")
701-
public <T> T deserialize(Response response, Type returnType,boolean ...isCommon) throws ApiException {
702+
public <T> T deserialize(Response response, Type returnType, boolean... isCommon) throws ApiException {
702703
if (response == null || returnType == null) {
703704
return null;
704705
}
@@ -730,13 +731,13 @@ public <T> T deserialize(Response response, Type returnType,boolean ...isCommon)
730731
}
731732

732733
StringBuilder builder = new StringBuilder();
733-
Map<String,ResponseMetadata> meta = new HashMap<>();
734+
Map<String, ResponseMetadata> meta = new HashMap<>();
734735
if (isCommon.length == 0 || !isCommon[0]) {
735-
if (!convertResponseBody(respBody, builder,meta)) {
736+
if (!convertResponseBody(respBody, builder, meta)) {
736737
throw new ApiException(
737738
response.code(),
738739
response.headers().toMultimap(),
739-
respBody,meta.get("ResponseMetadata"));
740+
respBody, meta.get("ResponseMetadata"));
740741
} else {
741742
respBody = builder.toString();
742743
}
@@ -749,16 +750,16 @@ public <T> T deserialize(Response response, Type returnType,boolean ...isCommon)
749750
}
750751
if (isJsonMime(contentType)) {
751752
T t = json.deserialize(respBody, returnType);
752-
if (t instanceof AbstractResponse){
753+
if (t instanceof AbstractResponse) {
753754
try {
754-
Method m = t.getClass().getMethod("setResponseMetadata",ResponseMetadata.class);
755-
m.invoke(t,meta.get("ResponseMetadata"));
755+
Method m = t.getClass().getMethod("setResponseMetadata", ResponseMetadata.class);
756+
m.invoke(t, meta.get("ResponseMetadata"));
756757
} catch (Exception e) {
757758
throw new ApiException(
758759
e.getMessage(),
759760
response.code(),
760761
response.headers().toMultimap(),
761-
respBody,meta.get("ResponseMetadata"));
762+
respBody, meta.get("ResponseMetadata"));
762763
}
763764
}
764765
return t;
@@ -770,7 +771,7 @@ public <T> T deserialize(Response response, Type returnType,boolean ...isCommon)
770771
"Content type \"" + contentType + "\" is not supported for type: " + returnType,
771772
response.code(),
772773
response.headers().toMultimap(),
773-
respBody,meta.get("ResponseMetadata"));
774+
respBody, meta.get("ResponseMetadata"));
774775
}
775776
}
776777

@@ -883,16 +884,16 @@ public <T> ApiResponse<T> execute(Call call) throws ApiException {
883884
* @param returnType The return type used to deserialize HTTP response body
884885
* @param <T> The return type corresponding to (same with) returnType
885886
* @param call Call
886-
* @param isCommon The flag for Universal
887+
* @param isCommon The flag for Universal
887888
* @return ApiResponse object containing response status, headers and
888889
* data, which is a Java object deserialized from response body and would be null
889890
* when returnType is null.
890891
* @throws ApiException If fail to execute the call
891892
*/
892-
public <T> ApiResponse<T> execute(Call call, final Type returnType,boolean ...isCommon) throws ApiException {
893+
public <T> ApiResponse<T> execute(Call call, final Type returnType, boolean... isCommon) throws ApiException {
893894
try {
894895
Response response = call.execute();
895-
T data = handleResponse(response, returnType,isCommon);
896+
T data = handleResponse(response, returnType, isCommon);
896897
return new ApiResponse<T>(response.code(), response.headers().toMultimap(), data);
897898
} catch (IOException e) {
898899
throw new ApiException(e);
@@ -947,12 +948,12 @@ public void onResponse(Response response) throws IOException {
947948
* @param <T> Type
948949
* @param response Response
949950
* @param returnType Return type
950-
* @param isCommon The flag for Universal
951+
* @param isCommon The flag for Universal
951952
* @return Type
952953
* @throws ApiException If the response has a unsuccessful status code or
953954
* fail to deserialize the response body
954955
*/
955-
public <T> T handleResponse(Response response, Type returnType, boolean ...isCommon) throws ApiException {
956+
public <T> T handleResponse(Response response, Type returnType, boolean... isCommon) throws ApiException {
956957
if (response.isSuccessful()) {
957958
if (returnType == null || response.code() == 204) {
958959
// returning null if the returnType is not defined,
@@ -966,7 +967,7 @@ public <T> T handleResponse(Response response, Type returnType, boolean ...isCom
966967
}
967968
return null;
968969
} else {
969-
return deserialize(response, returnType,isCommon);
970+
return deserialize(response, returnType, isCommon);
970971
}
971972
} else {
972973
String respBody = null;
@@ -998,7 +999,7 @@ public <T> T handleResponse(Response response, Type returnType, boolean ...isCom
998999
* @throws ApiException If fail to serialize the request body object
9991000
*/
10001001
public Call buildCall(String path, String method, List<Pair> queryParams, List<Pair> collectionQueryParams, Object body, Map<String, String> headerParams, Map<String, Object> formParams, String[] authNames, ProgressRequestBody.ProgressRequestListener progressRequestListener, boolean... isCommon) throws ApiException {
1001-
Request request = buildRequest(path, method, queryParams, collectionQueryParams, body, headerParams, formParams, authNames, progressRequestListener,isCommon);
1002+
Request request = buildRequest(path, method, queryParams, collectionQueryParams, body, headerParams, formParams, authNames, progressRequestListener, isCommon);
10021003

10031004
return httpClient.newCall(request);
10041005
}
@@ -1040,8 +1041,8 @@ private void updateQueryParams(List<Pair> queryParams, String[] param) {
10401041
private ServiceInfo addPairAndGetServiceInfo(String path, List<Pair> queryParams, Map<String, String> headerParams) {
10411042
String[] param = path.split("/");
10421043

1043-
if (param.length >= 6){
1044-
headerParams.put("Content-Type",param[5].replaceAll("_","/"));
1044+
if (param.length >= 6) {
1045+
headerParams.put("Content-Type", param[5].replaceAll("_", "/"));
10451046
}
10461047

10471048
if (!isApplicationJsonBody(headerParams) && !isPostBody(headerParams)) {
@@ -1062,22 +1063,23 @@ private String getTruePath(String path, Map<String, String> headerParams) {
10621063
}
10631064

10641065
@SuppressWarnings("all")
1065-
private boolean convertResponseBody(String source, StringBuilder stringBuilder,Map<String, ResponseMetadata> m) {
1066+
private boolean convertResponseBody(String source, StringBuilder stringBuilder, Map<String, ResponseMetadata> m) {
10661067
Type t = new TypeToken<Map<String, ?>>() {
10671068
}.getType();
10681069
Map<String, ?> temp = json.deserialize(source, t);
1069-
if (temp.containsKey("ResponseMetadata")) {
1070-
ResponseMetadata meta = json.deserialize(json.serialize(temp.get("ResponseMetadata")),new TypeToken<ResponseMetadata>(){}.getType());
1071-
m.put("ResponseMetadata",meta);
1070+
if (temp.containsKey("ResponseMetadata")) {
1071+
ResponseMetadata meta = json.deserialize(json.serialize(temp.get("ResponseMetadata")), new TypeToken<ResponseMetadata>() {
1072+
}.getType());
1073+
m.put("ResponseMetadata", meta);
10721074

1073-
if (meta.getError()!= null){
1075+
if (meta.getError() != null) {
10741076
return false;
10751077
}
10761078

1077-
if (temp.containsKey("Result")){
1079+
if (temp.containsKey("Result")) {
10781080
stringBuilder.append(json.serialize(temp.get("Result")));
1079-
}else{
1080-
stringBuilder.append(json.serialize(new HashMap<String,Object>()));
1081+
} else {
1082+
stringBuilder.append(json.serialize(new HashMap<String, Object>()));
10811083
}
10821084

10831085
return true;
@@ -1086,8 +1088,8 @@ private boolean convertResponseBody(String source, StringBuilder stringBuilder,M
10861088
return false;
10871089
}
10881090

1089-
private void buildSimpleRequest(Object body, List<Pair> queryParams, Map<String, String> headerParams, StringBuilder builder, String chain, boolean... isCommon) throws Exception {
1090-
if (body ==null) {
1091+
private void buildSimpleRequest(Object body, List<Pair> queryParams, Map<String, String> headerParams, StringBuilder builder, FormEncodingBuilder formBuilder, String chain, boolean... isCommon) throws Exception {
1092+
if (body == null) {
10911093
return;
10921094
}
10931095
if (isApplicationJsonBody(headerParams)) {
@@ -1102,10 +1104,7 @@ private void buildSimpleRequest(Object body, List<Pair> queryParams, Map<String,
11021104
Map<String, Object> map = (Map<String, Object>) body;
11031105
if (isPostBody(headerParams)) {
11041106
for (Entry<String, Object> entry : map.entrySet()) {
1105-
builder.append(entry.getKey());
1106-
builder.append("=");
1107-
builder.append(entry.getValue().toString());
1108-
builder.append("&");
1107+
formBuilder.add(entry.getKey(), entry.getValue().toString());
11091108
}
11101109
} else {
11111110
for (Entry<String, Object> entry : map.entrySet()) {
@@ -1120,10 +1119,7 @@ private void buildSimpleRequest(Object body, List<Pair> queryParams, Map<String,
11201119

11211120
if (!clazz.getName().startsWith("com.volcengine")) {
11221121
if (isPostBody(headerParams)) {
1123-
builder.append(chain);
1124-
builder.append("=");
1125-
builder.append(body);
1126-
builder.append("&");
1122+
formBuilder.add(chain, body.toString());
11271123
} else {
11281124
Pair pair = new Pair(chain, body.toString());
11291125
queryParams.add(pair);
@@ -1146,32 +1142,32 @@ private void buildSimpleRequest(Object body, List<Pair> queryParams, Map<String,
11461142
int count = 0;
11471143
for (Object o : (List<?>) value) {
11481144
String key = chain + getMethodName(field.getName()) + "." + (++count);
1149-
buildSimpleRequest(o, queryParams, headerParams, builder, key);
1145+
buildSimpleRequest(o, queryParams, headerParams, builder, formBuilder, key);
11501146
}
11511147
} else {
11521148
if (!field.getType().getName().startsWith("com.volcengine")) {
1153-
buildBodyOrParameter(field, value, queryParams, headerParams, builder, chain);
1149+
buildBodyOrParameter(field, value, queryParams, headerParams, builder, formBuilder, chain);
11541150
} else if (field.getType().isEnum()) {
11551151
try {
11561152
Method method = field.getType().getDeclaredMethod("getValue");
11571153
Object v = method.invoke(value);
11581154
if (v != null) {
1159-
buildBodyOrParameter(field, v, queryParams, headerParams, builder, chain);
1155+
buildBodyOrParameter(field, v, queryParams, headerParams, builder, formBuilder, chain);
11601156
}
11611157
} catch (NoSuchMethodException e) {
11621158
throw new ApiException("sdk internal error,please contract us in github,ErrorCode is EnumNotGetValueMethod");
11631159
}
11641160

11651161
} else {
11661162
String key = chain + getMethodName(field.getName());
1167-
buildSimpleRequest(value, queryParams, headerParams, builder, key);
1163+
buildSimpleRequest(value, queryParams, headerParams, builder, formBuilder, key);
11681164
}
11691165
}
11701166
}
11711167
}
11721168
}
11731169

1174-
private void buildBodyOrParameter(Field field, Object v, List<Pair> queryParams, Map<String, String> headerParams, StringBuilder builder, String chain) throws Exception {
1170+
private void buildBodyOrParameter(Field field, Object v, List<Pair> queryParams, Map<String, String> headerParams, StringBuilder builder, FormEncodingBuilder formBuilder, String chain) throws Exception {
11751171
String name;
11761172
String defaultName = getMethodName(field.getName());
11771173
if (field.getAnnotation(SerializedName.class) != null) {
@@ -1186,28 +1182,13 @@ private void buildBodyOrParameter(Field field, Object v, List<Pair> queryParams,
11861182
}
11871183

11881184
if (isPostBody(headerParams)) {
1189-
builder.append(chain);
1190-
builder.append(name);
1191-
builder.append("=");
1192-
builder.append(v);
1193-
builder.append("&");
1185+
formBuilder.add(chain + name, v.toString());
11941186
} else {
11951187
Pair pair = new Pair(chain + name, v.toString());
11961188
queryParams.add(pair);
11971189
}
11981190
}
11991191

1200-
private String getPayload(String contentType, String source) {
1201-
if (source.equals("")) {
1202-
return "";
1203-
}
1204-
if (contentType.equals("application/json")) {
1205-
return source;
1206-
} else {
1207-
return source.substring(0, source.length() - 1);
1208-
}
1209-
}
1210-
12111192
private String getMethodName(String fieldName) throws Exception {
12121193
char[] chars = fieldName.toCharArray();
12131194
chars[0] = toUpperCase(chars[0]);
@@ -1247,9 +1228,10 @@ public Request buildRequest(String path, String method, List<Pair> queryParams,
12471228
String truePath = getTruePath(path, headerParams);
12481229
String contentType = headerParams.get("Content-Type");
12491230
StringBuilder bodyBuilder = new StringBuilder();
1231+
FormEncodingBuilder formBuilder = new FormEncodingBuilder();
12501232

12511233
try {
1252-
buildSimpleRequest(body, queryParams, headerParams, bodyBuilder, "", isCommon);
1234+
buildSimpleRequest(body, queryParams, headerParams, bodyBuilder, formBuilder, "", isCommon);
12531235
} catch (Exception e) {
12541236
throw new ApiException(e);
12551237
}
@@ -1258,15 +1240,12 @@ public Request buildRequest(String path, String method, List<Pair> queryParams,
12581240
final String url = buildUrl(truePath, queryParams, collectionQueryParams);
12591241
final Request.Builder reqBuilder = new Request.Builder().url(url);
12601242

1261-
12621243
RequestBody reqBody;
12631244
if (!HttpMethod.permitsRequestBody(method)) {
12641245
reqBody = null;
12651246
} else if ("application/x-www-form-urlencoded".equals(contentType)) {
1266-
for (Pair pair : queryParams) {
1267-
formParams.put(pair.getName(), pair.getValue());
1268-
}
1269-
reqBody = buildRequestBodyFormEncoding(formParams);
1247+
reqBody = formBuilder.build();
1248+
12701249
// fix action & version
12711250
queryParams.clear();
12721251
updateQueryParams(queryParams, path.split("/"));
@@ -1292,9 +1271,17 @@ public Request buildRequest(String path, String method, List<Pair> queryParams,
12921271
queryParams.clear();
12931272
updateQueryParams(queryParams, path.split("/"));
12941273
}
1274+
12951275
//sign
1296-
updateParamsForAuth(authNames, queryParams, headerParams, serviceInfo, getPayload(contentType,
1297-
bodyBuilder.toString()));
1276+
final Buffer buffer = new Buffer();
1277+
try {
1278+
if(reqBody != null) {
1279+
reqBody.writeTo(buffer);
1280+
}
1281+
} catch (IOException e) {
1282+
throw new ApiException(e);
1283+
}
1284+
updateParamsForAuth(authNames, queryParams, headerParams, serviceInfo, buffer.readUtf8());
12981285

12991286
processHeaderParams(headerParams, reqBuilder);
13001287

0 commit comments

Comments
 (0)