Skip to content

Commit 26ce408

Browse files
committed
feat(http): support response codes
1 parent ec84d36 commit 26ce408

18 files changed

+217
-14
lines changed

samples/GenerationStreamCall.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,23 @@ public static void streamCall()
1818
Generation gen = new Generation();
1919
GenerationParam param = GenerationParam.builder()
2020
.model("qwen3-max")
21-
.prompt("就当前的海洋污染的情况,写一份限塑的倡议书提纲,需要有理有据地号召大家克制地使用塑料制品")
21+
.prompt("你好")
2222
.topP(0.8)
2323
.incrementalOutput(false)
2424
.build();
2525
Flowable<GenerationResult> result = gen.streamCall(param);
2626
result.blockingForEach(message -> {
27-
System.out.println(JsonUtils.toJson(message));
27+
System.out.println("generation_result:");
28+
System.out.println(message);
29+
System.out.println(JsonUtils.toJson(message) + "\n");
2830
});
2931
}
3032

3133
public static void streamCallWithCallback()
3234
throws NoApiKeyException, ApiException, InputRequiredException,InterruptedException {
3335
Generation gen = new Generation();
3436
GenerationParam param = GenerationParam.builder().model(Generation.Models.QWEN_PLUS)
35-
.prompt("就当前的海洋污染的情况,写一份限塑的倡议书提纲,需要有理有据地号召大家克制地使用塑料制品").topP(0.8).build();
37+
.prompt("你好").topP(0.8).build();
3638
Semaphore semaphore = new Semaphore(0);
3739
gen.streamCall(param, new ResultCallback<GenerationResult>() {
3840

@@ -86,17 +88,17 @@ public static void main(String[] args) {
8688
System.out.println(e.getMessage());
8789
}
8890

89-
try {
90-
streamCallWithCallback();
91-
} catch (ApiException | NoApiKeyException | InputRequiredException | InterruptedException e) {
92-
System.out.println(e.getMessage());
93-
}
94-
95-
try {
96-
streamCallWithSearchOptions();
97-
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
98-
System.out.println(e.getMessage());
99-
}
91+
// try {
92+
// streamCallWithCallback();
93+
// } catch (ApiException | NoApiKeyException | InputRequiredException | InterruptedException e) {
94+
// System.out.println(e.getMessage());
95+
// }
96+
//
97+
// try {
98+
// streamCallWithSearchOptions();
99+
// } catch (ApiException | NoApiKeyException | InputRequiredException e) {
100+
// System.out.println(e.getMessage());
101+
// }
100102

101103
System.exit(0);
102104
}

src/main/java/com/alibaba/dashscope/aigc/generation/GenerationResult.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.alibaba.dashscope.common.DashScopeResult;
55
import com.alibaba.dashscope.utils.JsonUtils;
66
import com.google.gson.JsonObject;
7+
import com.google.gson.annotations.SerializedName;
78
import lombok.Data;
89
import lombok.extern.slf4j.Slf4j;
910

@@ -13,12 +14,19 @@ public final class GenerationResult {
1314
private String requestId;
1415
private GenerationUsage usage;
1516
private GenerationOutput output;
17+
@SerializedName("status_code")
18+
private Integer statusCode;
19+
private String code;
20+
private String message;
1621

1722
private GenerationResult() {}
1823

1924
public static GenerationResult fromDashScopeResult(DashScopeResult dashScopeResult) {
2025
GenerationResult result = new GenerationResult();
2126
result.setRequestId(dashScopeResult.getRequestId());
27+
result.setStatusCode(dashScopeResult.getStatusCode());
28+
result.setCode(dashScopeResult.getCode());
29+
result.setMessage(dashScopeResult.getMessage());
2230
if (dashScopeResult.getUsage() != null) {
2331
result.setUsage(
2432
JsonUtils.fromJsonObject(

src/main/java/com/alibaba/dashscope/aigc/imagesynthesis/ImageSynthesisListResult.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,22 @@ public class ImageSynthesisListResult {
2727
@SerializedName("page_size")
2828
private Integer pageSize;
2929

30+
@SerializedName("status_code")
31+
private Integer statusCode;
32+
33+
private String code;
34+
35+
private String message;
36+
3037
public static ImageSynthesisListResult fromDashScopeResult(DashScopeResult dashScopeResult) {
3138
if (dashScopeResult.getOutput() != null) {
3239
ImageSynthesisListResult rs =
3340
(JsonUtils.fromJsonObject(
3441
(JsonObject) dashScopeResult.getOutput(), ImageSynthesisListResult.class));
3542
rs.requestId = dashScopeResult.getRequestId();
43+
rs.statusCode = dashScopeResult.getStatusCode();
44+
rs.code = dashScopeResult.getCode();
45+
rs.message = dashScopeResult.getMessage();
3646
return rs;
3747
} else {
3848
log.error(String.format("Result no output: %s", dashScopeResult));

src/main/java/com/alibaba/dashscope/aigc/imagesynthesis/ImageSynthesisResult.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,21 @@ public class ImageSynthesisResult {
1717
private ImageSynthesisOutput output;
1818
private ImageSynthesisUsage usage;
1919

20+
@SerializedName("status_code")
21+
private Integer statusCode;
22+
23+
private String code;
24+
25+
private String message;
26+
2027
private ImageSynthesisResult() {}
2128

2229
public static ImageSynthesisResult fromDashScopeResult(DashScopeResult dashScopeResult) {
2330
ImageSynthesisResult result = new ImageSynthesisResult();
2431
result.requestId = dashScopeResult.getRequestId();
32+
result.statusCode = dashScopeResult.getStatusCode();
33+
result.code = dashScopeResult.getCode();
34+
result.message = dashScopeResult.getMessage();
2535
if (dashScopeResult.getUsage() != null) {
2636
result.setUsage(
2737
JsonUtils.fromJsonObject(

src/main/java/com/alibaba/dashscope/aigc/multimodalconversation/MultiModalConversationResult.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.alibaba.dashscope.common.DashScopeResult;
44
import com.alibaba.dashscope.utils.JsonUtils;
55
import com.google.gson.JsonObject;
6+
import com.google.gson.annotations.SerializedName;
67
import lombok.Data;
78
import lombok.extern.slf4j.Slf4j;
89

@@ -13,11 +14,20 @@ public class MultiModalConversationResult {
1314
private MultiModalConversationUsage usage;
1415
private MultiModalConversationOutput output;
1516

17+
@SerializedName("status_code")
18+
private Integer statusCode;
19+
20+
private String code;
21+
private String message;
22+
1623
private MultiModalConversationResult() {}
1724

1825
public static MultiModalConversationResult fromDashScopeResult(DashScopeResult dashScopeResult) {
1926
MultiModalConversationResult result = new MultiModalConversationResult();
2027
result.setRequestId(dashScopeResult.getRequestId());
28+
result.setStatusCode(dashScopeResult.getStatusCode());
29+
result.setCode(dashScopeResult.getCode());
30+
result.setMessage(dashScopeResult.getMessage());
2131
if (dashScopeResult.getUsage() != null) {
2232
result.setUsage(
2333
JsonUtils.fromJsonObject(

src/main/java/com/alibaba/dashscope/aigc/videosynthesis/VideoSynthesisListResult.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,22 @@ public class VideoSynthesisListResult {
2727
@SerializedName("page_size")
2828
private Integer pageSize;
2929

30+
@SerializedName("status_code")
31+
private Integer statusCode;
32+
33+
private String code;
34+
35+
private String message;
36+
3037
public static VideoSynthesisListResult fromDashScopeResult(DashScopeResult dashScopeResult) {
3138
if (dashScopeResult.getOutput() != null) {
3239
VideoSynthesisListResult rs =
3340
(JsonUtils.fromJsonObject(
3441
(JsonObject) dashScopeResult.getOutput(), VideoSynthesisListResult.class));
3542
rs.requestId = dashScopeResult.getRequestId();
43+
rs.statusCode = dashScopeResult.getStatusCode();
44+
rs.code = dashScopeResult.getCode();
45+
rs.message = dashScopeResult.getMessage();
3646
return rs;
3747
} else {
3848
log.error("Result no output: {}", dashScopeResult);

src/main/java/com/alibaba/dashscope/aigc/videosynthesis/VideoSynthesisResult.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,21 @@ public class VideoSynthesisResult {
1717
private VideoSynthesisOutput output;
1818
private VideoSynthesisUsage usage;
1919

20+
@SerializedName("status_code")
21+
private Integer statusCode;
22+
23+
private String code;
24+
25+
private String message;
26+
2027
private VideoSynthesisResult() {}
2128

2229
public static VideoSynthesisResult fromDashScopeResult(DashScopeResult dashScopeResult) {
2330
VideoSynthesisResult result = new VideoSynthesisResult();
2431
result.requestId = dashScopeResult.getRequestId();
32+
result.statusCode = dashScopeResult.getStatusCode();
33+
result.code = dashScopeResult.getCode();
34+
result.message = dashScopeResult.getMessage();
2535
if (dashScopeResult.getUsage() != null) {
2636
result.setUsage(
2737
JsonUtils.fromJsonObject(

src/main/java/com/alibaba/dashscope/app/ApplicationResult.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,24 @@ public class ApplicationResult {
3232
@SerializedName("usage")
3333
private ApplicationUsage usage;
3434

35+
/** HTTP status code from server response */
36+
@SerializedName("status_code")
37+
private Integer statusCode;
38+
39+
/** Error code from server response */
40+
@SerializedName("code")
41+
private String code;
42+
43+
/** Message from server response */
44+
@SerializedName("message")
45+
private String message;
46+
3547
public static ApplicationResult fromDashScopeResult(DashScopeResult dashScopeResult) {
3648
ApplicationResult result = new ApplicationResult();
3749
result.setRequestId(dashScopeResult.getRequestId());
50+
result.setStatusCode(dashScopeResult.getStatusCode());
51+
result.setCode(dashScopeResult.getCode());
52+
result.setMessage(dashScopeResult.getMessage());
3853
if (dashScopeResult.getUsage() != null) {
3954
result.setUsage(
4055
JsonUtils.fromJsonObject(

src/main/java/com/alibaba/dashscope/common/DashScopeResult.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ protected <T extends Result> T fromResponse(Protocol protocol, NetworkResponse r
5757
}
5858
} else {
5959
JsonObject jsonObject = JsonUtils.parse(response.getMessage());
60+
// Set HTTP status code if available
61+
if (response.getHttpStatusCode() != null) {
62+
this.setStatusCode(response.getHttpStatusCode());
63+
}
6064
if (jsonObject.has(ApiKeywords.OUTPUT)) {
6165
this.output =
6266
jsonObject.get(ApiKeywords.OUTPUT).isJsonNull()
@@ -72,6 +76,30 @@ protected <T extends Result> T fromResponse(Protocol protocol, NetworkResponse r
7276
if (jsonObject.has(ApiKeywords.REQUEST_ID)) {
7377
this.setRequestId(jsonObject.get(ApiKeywords.REQUEST_ID).getAsString());
7478
}
79+
if (jsonObject.has(ApiKeywords.STATUS_CODE)) {
80+
this.setStatusCode(
81+
jsonObject.get(ApiKeywords.STATUS_CODE).isJsonNull()
82+
? null
83+
: jsonObject.get(ApiKeywords.STATUS_CODE).getAsInt());
84+
}
85+
if (jsonObject.has(ApiKeywords.CODE)) {
86+
this.setCode(
87+
jsonObject.get(ApiKeywords.CODE).isJsonNull()
88+
? ""
89+
: jsonObject.get(ApiKeywords.CODE).getAsString());
90+
} else {
91+
// Set default empty string for successful responses
92+
this.setCode("");
93+
}
94+
if (jsonObject.has(ApiKeywords.MESSAGE)) {
95+
this.setMessage(
96+
jsonObject.get(ApiKeywords.MESSAGE).isJsonNull()
97+
? ""
98+
: jsonObject.get(ApiKeywords.MESSAGE).getAsString());
99+
} else {
100+
// Set default empty string for successful responses
101+
this.setMessage("");
102+
}
75103
if (jsonObject.has(ApiKeywords.DATA)) {
76104
if (jsonObject.has(ApiKeywords.REQUEST_ID)) {
77105
jsonObject.remove(ApiKeywords.REQUEST_ID);
@@ -116,6 +144,10 @@ public <T extends Result> T fromResponse(
116144
if ((response.getHeaders().containsKey("X-DashScope-OutputEncrypted".toLowerCase())
117145
|| req.isEncryptRequest())
118146
&& protocol == Protocol.HTTP) {
147+
// Set HTTP status code if available
148+
if (response.getHttpStatusCode() != null) {
149+
this.setStatusCode(response.getHttpStatusCode());
150+
}
119151
JsonObject jsonObject = JsonUtils.parse(response.getMessage());
120152
String encryptedOutput =
121153
jsonObject.get(ApiKeywords.OUTPUT).isJsonNull()
@@ -140,6 +172,30 @@ public <T extends Result> T fromResponse(
140172
if (jsonObject.has(ApiKeywords.REQUEST_ID)) {
141173
this.setRequestId(jsonObject.get(ApiKeywords.REQUEST_ID).getAsString());
142174
}
175+
if (jsonObject.has(ApiKeywords.STATUS_CODE)) {
176+
this.setStatusCode(
177+
jsonObject.get(ApiKeywords.STATUS_CODE).isJsonNull()
178+
? null
179+
: jsonObject.get(ApiKeywords.STATUS_CODE).getAsInt());
180+
}
181+
if (jsonObject.has(ApiKeywords.CODE)) {
182+
this.setCode(
183+
jsonObject.get(ApiKeywords.CODE).isJsonNull()
184+
? ""
185+
: jsonObject.get(ApiKeywords.CODE).getAsString());
186+
} else {
187+
// Set default empty string for successful responses
188+
this.setCode("");
189+
}
190+
if (jsonObject.has(ApiKeywords.MESSAGE)) {
191+
this.setMessage(
192+
jsonObject.get(ApiKeywords.MESSAGE).isJsonNull()
193+
? ""
194+
: jsonObject.get(ApiKeywords.MESSAGE).getAsString());
195+
} else {
196+
// Set default empty string for successful responses
197+
this.setMessage("");
198+
}
143199
if (jsonObject.has(ApiKeywords.DATA)) {
144200
if (jsonObject.has(ApiKeywords.REQUEST_ID)) {
145201
jsonObject.remove(ApiKeywords.REQUEST_ID);

src/main/java/com/alibaba/dashscope/common/Result.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ public abstract class Result {
2626
/** The headers of response */
2727
private Map<String, String> headers;
2828

29+
/** The HTTP status code from server response */
30+
private Integer statusCode;
31+
32+
/** The error code from server response */
33+
private String code;
34+
35+
/** The message from server response */
36+
private String message;
37+
2938
/**
3039
* Load data from the server output.
3140
*

0 commit comments

Comments
 (0)