Skip to content

Commit f2b027c

Browse files
committed
feat: support universal client
1 parent 58e6c39 commit f2b027c

File tree

24 files changed

+300
-58
lines changed

24 files changed

+300
-58
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
<dependency>
5555
<groupId>com.volcengine</groupId>
5656
<artifactId>volcengine-java-sdk-bom</artifactId>
57-
<version>0.1.29</version>
57+
<version>0.1.30</version>
5858
<type>pom</type>
5959
<scope>import</scope>
6060
</dependency>
@@ -70,12 +70,12 @@
7070
<dependency>
7171
<groupId>com.volcengine</groupId>
7272
<artifactId>volcengine-java-sdk-vpc</artifactId>
73-
<version>0.1.29</version>
73+
<version>0.1.30</version>
7474
</dependency>
7575
<dependency>
7676
<groupId>com.volcengine</groupId>
7777
<artifactId>volcengine-java-sdk-ecs</artifactId>
78-
<version>0.1.29</version>
78+
<version>0.1.30</version>
7979
</dependency>
8080
</dependencies>
8181
```

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<groupId>com.volcengine</groupId>
66
<artifactId>volcengine-java-sdk</artifactId>
77
<packaging>pom</packaging>
8-
<version>0.1.29</version>
8+
<version>0.1.30</version>
99
<name>volcengine-java-sdk</name>
1010
<url>https://open.volcengineapi.com</url>
1111
<description>The Java SDK For Volcengine</description>

volcengine-java-sdk-alb/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<artifactId>volcengine-java-sdk</artifactId>
55
<groupId>com.volcengine</groupId>
6-
<version>0.1.29</version>
6+
<version>0.1.30</version>
77
<relativePath>../pom.xml</relativePath>
88
</parent>
99
<modelVersion>4.0.0</modelVersion>

volcengine-java-sdk-autoscaling/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<artifactId>volcengine-java-sdk</artifactId>
55
<groupId>com.volcengine</groupId>
6-
<version>0.1.29</version>
6+
<version>0.1.30</version>
77
<relativePath>../pom.xml</relativePath>
88
</parent>
99
<modelVersion>4.0.0</modelVersion>

volcengine-java-sdk-bom/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<artifactId>volcengine-java-sdk</artifactId>
55
<groupId>com.volcengine</groupId>
6-
<version>0.1.29</version>
6+
<version>0.1.30</version>
77
<relativePath>../pom.xml</relativePath>
88
</parent>
99
<modelVersion>4.0.0</modelVersion>

volcengine-java-sdk-cen/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<artifactId>volcengine-java-sdk</artifactId>
55
<groupId>com.volcengine</groupId>
6-
<version>0.1.29</version>
6+
<version>0.1.30</version>
77
<relativePath>../pom.xml</relativePath>
88
</parent>
99
<modelVersion>4.0.0</modelVersion>

volcengine-java-sdk-clb/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<artifactId>volcengine-java-sdk</artifactId>
55
<groupId>com.volcengine</groupId>
6-
<version>0.1.29</version>
6+
<version>0.1.30</version>
77
<relativePath>../pom.xml</relativePath>
88
</parent>
99
<modelVersion>4.0.0</modelVersion>

volcengine-java-sdk-core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<groupId>com.volcengine</groupId>
55
<artifactId>volcengine-java-sdk</artifactId>
6-
<version>0.1.29</version>
6+
<version>0.1.30</version>
77
<relativePath>../pom.xml</relativePath>
88
</parent>
99
<modelVersion>4.0.0</modelVersion>

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

Lines changed: 65 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -690,12 +690,13 @@ public String escapeString(String str) {
690690
* @param <T> Type
691691
* @param response HTTP response
692692
* @param returnType The type of the Java object
693+
* @param isCommon The flag for Universal
693694
* @return The deserialized Java object
694695
* @throws ApiException If fail to deserialize response body, i.e. cannot read response body
695696
* or the Content-Type of the response is not supported.
696697
*/
697698
@SuppressWarnings("unchecked")
698-
public <T> T deserialize(Response response, Type returnType) throws ApiException {
699+
public <T> T deserialize(Response response, Type returnType,boolean ...isCommon) throws ApiException {
699700
if (response == null || returnType == null) {
700701
return null;
701702
}
@@ -728,16 +729,17 @@ public <T> T deserialize(Response response, Type returnType) throws ApiException
728729

729730
StringBuilder builder = new StringBuilder();
730731

731-
if (!convertResponseBody(respBody,builder)){
732-
throw new ApiException(
733-
response.code(),
734-
response.headers().toMultimap(),
735-
respBody);
736-
}else {
737-
respBody = builder.toString();
732+
if (isCommon.length == 0 || !isCommon[0]) {
733+
if (!convertResponseBody(respBody, builder)) {
734+
throw new ApiException(
735+
response.code(),
736+
response.headers().toMultimap(),
737+
respBody);
738+
} else {
739+
respBody = builder.toString();
740+
}
738741
}
739742

740-
741743
String contentType = response.headers().get("Content-Type");
742744
if (contentType == null) {
743745
// ensuring a default content type
@@ -866,15 +868,16 @@ public <T> ApiResponse<T> execute(Call call) throws ApiException {
866868
* @param returnType The return type used to deserialize HTTP response body
867869
* @param <T> The return type corresponding to (same with) returnType
868870
* @param call Call
871+
* @param isCommon The flag for Universal
869872
* @return ApiResponse object containing response status, headers and
870873
* data, which is a Java object deserialized from response body and would be null
871874
* when returnType is null.
872875
* @throws ApiException If fail to execute the call
873876
*/
874-
public <T> ApiResponse<T> execute(Call call, final Type returnType) throws ApiException {
877+
public <T> ApiResponse<T> execute(Call call, final Type returnType,boolean ...isCommon) throws ApiException {
875878
try {
876879
Response response = call.execute();
877-
T data = handleResponse(response, returnType);
880+
T data = handleResponse(response, returnType,isCommon);
878881
return new ApiResponse<T>(response.code(), response.headers().toMultimap(), data);
879882
} catch (IOException e) {
880883
throw new ApiException(e);
@@ -929,11 +932,12 @@ public void onResponse(Response response) throws IOException {
929932
* @param <T> Type
930933
* @param response Response
931934
* @param returnType Return type
935+
* @param isCommon The flag for Universal
932936
* @return Type
933937
* @throws ApiException If the response has a unsuccessful status code or
934938
* fail to deserialize the response body
935939
*/
936-
public <T> T handleResponse(Response response, Type returnType) throws ApiException {
940+
public <T> T handleResponse(Response response, Type returnType, boolean ...isCommon) throws ApiException {
937941
if (response.isSuccessful()) {
938942
if (returnType == null || response.code() == 204) {
939943
// returning null if the returnType is not defined,
@@ -947,7 +951,7 @@ public <T> T handleResponse(Response response, Type returnType) throws ApiExcept
947951
}
948952
return null;
949953
} else {
950-
return deserialize(response, returnType);
954+
return deserialize(response, returnType,isCommon);
951955
}
952956
} else {
953957
String respBody = null;
@@ -974,11 +978,12 @@ public <T> T handleResponse(Response response, Type returnType) throws ApiExcept
974978
* @param formParams The form parameters
975979
* @param authNames The authentications to apply
976980
* @param progressRequestListener Progress request listener
981+
* @param isCommon The flag for Universal
977982
* @return The HTTP call
978983
* @throws ApiException If fail to serialize the request body object
979984
*/
980-
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) throws ApiException {
981-
Request request = buildRequest(path, method, queryParams, collectionQueryParams, body, headerParams, formParams, authNames, progressRequestListener);
985+
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 {
986+
Request request = buildRequest(path, method, queryParams, collectionQueryParams, body, headerParams, formParams, authNames, progressRequestListener,isCommon);
982987

983988
return httpClient.newCall(request);
984989
}
@@ -1012,7 +1017,7 @@ private boolean isPostBody(Map<String, String> headerParams) {
10121017
return false;
10131018
}
10141019

1015-
private void updateQueryParams(List<Pair> queryParams,String[] param){
1020+
private void updateQueryParams(List<Pair> queryParams, String[] param) {
10161021
queryParams.add(new Pair("Action", param[1]));
10171022
queryParams.add(new Pair("Version", param[2]));
10181023
}
@@ -1021,7 +1026,7 @@ private ServiceInfo addPairAndGetServiceInfo(String path, List<Pair> queryParams
10211026
String[] param = path.split("/");
10221027

10231028
if (!isApplicationJsonBody(headerParams) && !isPostBody(headerParams)) {
1024-
updateQueryParams(queryParams,param);
1029+
updateQueryParams(queryParams, param);
10251030
}
10261031
return new ServiceInfo(param[3], param[4]);
10271032
}
@@ -1036,7 +1041,7 @@ private String getTruePath(String path, Map<String, String> headerParams) {
10361041
}
10371042

10381043
@SuppressWarnings("all")
1039-
private boolean convertResponseBody(String source,StringBuilder stringBuilder) {
1044+
private boolean convertResponseBody(String source, StringBuilder stringBuilder) {
10401045
Type t = new TypeToken<Map<String, ?>>() {
10411046
}.getType();
10421047
Map<String, ?> temp = json.deserialize(source, t);
@@ -1047,13 +1052,35 @@ private boolean convertResponseBody(String source,StringBuilder stringBuilder) {
10471052
return false;
10481053
}
10491054

1050-
private void buildSimpleRequest(Object body, List<Pair> queryParams, Map<String, String> headerParams, StringBuilder builder, String chain) throws Exception {
1055+
private void buildSimpleRequest(Object body, List<Pair> queryParams, Map<String, String> headerParams, StringBuilder builder, String chain, boolean... isCommon) throws Exception {
10511056
if (isApplicationJsonBody(headerParams)) {
10521057
builder.append(json.serialize(body));
10531058
return;
10541059
}
10551060
Class<?> clazz = body.getClass();
10561061

1062+
if (isCommon.length > 0 && isCommon[0]) {
1063+
try {
1064+
@SuppressWarnings("unchecked")
1065+
Map<String, Object> map = (Map<String, Object>) body;
1066+
if (isPostBody(headerParams)) {
1067+
for (Entry<String, Object> entry : map.entrySet()) {
1068+
builder.append(entry.getKey());
1069+
builder.append("=");
1070+
builder.append(entry.getValue().toString());
1071+
builder.append("&");
1072+
}
1073+
} else {
1074+
for (Entry<String, Object> entry : map.entrySet()) {
1075+
Pair pair = new Pair(entry.getKey(), entry.getValue().toString());
1076+
queryParams.add(pair);
1077+
}
1078+
}
1079+
} catch (Exception e) {
1080+
throw new ApiException(e);
1081+
}
1082+
}
1083+
10571084
if (!clazz.getName().startsWith("com.volcengine")) {
10581085
if (isPostBody(headerParams)) {
10591086
builder.append(chain);
@@ -1086,19 +1113,19 @@ private void buildSimpleRequest(Object body, List<Pair> queryParams, Map<String,
10861113
}
10871114
} else {
10881115
if (!field.getType().getName().startsWith("com.volcengine")) {
1089-
buildBodyOrParameter(field,value,queryParams,headerParams,builder,chain);
1116+
buildBodyOrParameter(field, value, queryParams, headerParams, builder, chain);
10901117
} else if (field.getType().isEnum()) {
10911118
try {
10921119
Method method = field.getType().getDeclaredMethod("getValue");
10931120
Object v = method.invoke(value);
1094-
if (v!= null ){
1095-
buildBodyOrParameter(field,v,queryParams,headerParams,builder,chain);
1121+
if (v != null) {
1122+
buildBodyOrParameter(field, v, queryParams, headerParams, builder, chain);
10961123
}
10971124
} catch (NoSuchMethodException e) {
10981125
throw new ApiException("sdk internal error,please contract us in github,ErrorCode is EnumNotGetValueMethod");
10991126
}
11001127

1101-
}else {
1128+
} else {
11021129
String key = chain + getMethodName(field.getName());
11031130
buildSimpleRequest(value, queryParams, headerParams, builder, key);
11041131
}
@@ -1107,17 +1134,17 @@ private void buildSimpleRequest(Object body, List<Pair> queryParams, Map<String,
11071134
}
11081135
}
11091136

1110-
private void buildBodyOrParameter(Field field,Object v,List<Pair> queryParams, Map<String, String> headerParams, StringBuilder builder, String chain)throws Exception{
1137+
private void buildBodyOrParameter(Field field, Object v, List<Pair> queryParams, Map<String, String> headerParams, StringBuilder builder, String chain) throws Exception {
11111138
String name;
11121139
String defaultName = getMethodName(field.getName());
1113-
if (field.getAnnotation(SerializedName.class) != null){
1140+
if (field.getAnnotation(SerializedName.class) != null) {
11141141
SerializedName s = field.getAnnotation(SerializedName.class);
1115-
if (!s.value().equals(defaultName)){
1142+
if (!s.value().equals(defaultName)) {
11161143
name = s.value();
1117-
}else{
1118-
name =defaultName;
1144+
} else {
1145+
name = defaultName;
11191146
}
1120-
}else{
1147+
} else {
11211148
name = defaultName;
11221149
}
11231150

@@ -1127,7 +1154,7 @@ private void buildBodyOrParameter(Field field,Object v,List<Pair> queryParams, M
11271154
builder.append("=");
11281155
builder.append(v);
11291156
builder.append("&");
1130-
}else {
1157+
} else {
11311158
Pair pair = new Pair(chain + name, v.toString());
11321159
queryParams.add(pair);
11331160
}
@@ -1169,10 +1196,11 @@ private char toUpperCase(char c) {
11691196
* @param formParams The form parameters
11701197
* @param authNames The authentications to apply
11711198
* @param progressRequestListener Progress request listener
1199+
* @param isCommon The flag for Universal
11721200
* @return The HTTP request
11731201
* @throws ApiException If fail to serialize the request body object
11741202
*/
1175-
public Request buildRequest(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) throws ApiException {
1203+
public Request buildRequest(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 {
11761204

11771205
getDefaultContentType(headerParams);
11781206

@@ -1182,7 +1210,7 @@ public Request buildRequest(String path, String method, List<Pair> queryParams,
11821210
StringBuilder bodyBuilder = new StringBuilder();
11831211

11841212
try {
1185-
buildSimpleRequest(body, queryParams, headerParams, bodyBuilder, "");
1213+
buildSimpleRequest(body, queryParams, headerParams, bodyBuilder, "", isCommon);
11861214
} catch (Exception e) {
11871215
throw new ApiException(e);
11881216
}
@@ -1202,12 +1230,12 @@ public Request buildRequest(String path, String method, List<Pair> queryParams,
12021230
reqBody = buildRequestBodyFormEncoding(formParams);
12031231
// fix action & version
12041232
queryParams.clear();
1205-
updateQueryParams(queryParams,path.split("/"));
1233+
updateQueryParams(queryParams, path.split("/"));
12061234
} else if ("multipart/form-data".equals(contentType)) {
12071235
reqBody = buildRequestBodyMultipart(formParams);
12081236
// fix action & version
12091237
queryParams.clear();
1210-
updateQueryParams(queryParams,path.split("/"));
1238+
updateQueryParams(queryParams, path.split("/"));
12111239
} else if (body == null) {
12121240
if ("DELETE".equals(method)) {
12131241
// allow calling DELETE without sending a request body
@@ -1218,12 +1246,12 @@ public Request buildRequest(String path, String method, List<Pair> queryParams,
12181246
}
12191247
// fix action & version
12201248
queryParams.clear();
1221-
updateQueryParams(queryParams,path.split("/"));
1249+
updateQueryParams(queryParams, path.split("/"));
12221250
} else {
12231251
reqBody = serialize(body, contentType);
12241252
// fix action & version
12251253
queryParams.clear();
1226-
updateQueryParams(queryParams,path.split("/"));
1254+
updateQueryParams(queryParams, path.split("/"));
12271255
}
12281256
//sign
12291257
updateParamsForAuth(authNames, queryParams, headerParams, serviceInfo, getPayload(contentType,
@@ -1339,7 +1367,7 @@ public void updateParamsForAuth(String[] authNames, List<Pair> queryParams, Map<
13391367
if (volcengineSign.getCredentials() == null) {
13401368
throw new RuntimeException("Credentials must set when ApiClient init");
13411369
}
1342-
if (StringUtils.isEmpty(credentials.getAccessKey()) || StringUtils.isEmpty(credentials.getSecretKey())){
1370+
if (StringUtils.isEmpty(credentials.getAccessKey()) || StringUtils.isEmpty(credentials.getSecretKey())) {
13431371
throw new RuntimeException("AccessKey and SecretKey must set when ApiClient init Credentials");
13441372
}
13451373
if (StringUtils.isEmpty(volcengineSign.getRegion())) {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.volcengine;
2+
3+
/**
4+
5+
* 2022/12/23 15:38
6+
*/
7+
public enum ContentType {
8+
Default(""),ApplicationJSON("application/json"),FormUrlencoded("x-www-form-urlencoded");
9+
private final String type;
10+
private ContentType(String type){
11+
this.type =type;
12+
}
13+
public String GetType(){
14+
return this.type;
15+
}
16+
}

0 commit comments

Comments
 (0)