Skip to content

Commit 25a880a

Browse files
committed
feat(cp): refine wedoc doc and smartsheet helpers
1 parent 1b62b6e commit 25a880a

File tree

7 files changed

+108
-15
lines changed

7 files changed

+108
-15
lines changed

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/oa/doc/WxCpDocData.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public class WxCpDocData extends WxCpBaseResp implements Serializable {
3636
@SerializedName("next_cursor")
3737
private String nextCursor;
3838

39+
public JsonElement getEffectiveContent() {
40+
return this.content != null ? this.content : this.docContent;
41+
}
42+
3943
public static WxCpDocData fromJson(String json) {
4044
return WxCpGsonBuilder.create().fromJson(json, WxCpDocData.class);
4145
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/oa/doc/WxCpDocGetDataRequest.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package me.chanjar.weixin.cp.bean.oa.doc;
22

3+
import com.google.gson.JsonElement;
34
import com.google.gson.JsonObject;
45
import com.google.gson.annotations.SerializedName;
56
import lombok.AllArgsConstructor;
@@ -42,10 +43,37 @@ public String toJson() {
4243
JsonObject jsonObject = new JsonObject();
4344
jsonObject.addProperty("docid", this.docId);
4445
if (this.extra != null) {
45-
for (Map.Entry<String, com.google.gson.JsonElement> entry : this.extra.entrySet()) {
46+
for (Map.Entry<String, JsonElement> entry : this.extra.entrySet()) {
4647
jsonObject.add(entry.getKey(), entry.getValue());
4748
}
4849
}
4950
return WxCpGsonBuilder.create().toJson(jsonObject);
5051
}
52+
53+
public WxCpDocGetDataRequest addExtra(String key, String value) {
54+
ensureExtra().addProperty(key, value);
55+
return this;
56+
}
57+
58+
public WxCpDocGetDataRequest addExtra(String key, Number value) {
59+
ensureExtra().addProperty(key, value);
60+
return this;
61+
}
62+
63+
public WxCpDocGetDataRequest addExtra(String key, Boolean value) {
64+
ensureExtra().addProperty(key, value);
65+
return this;
66+
}
67+
68+
public WxCpDocGetDataRequest addExtra(String key, JsonElement value) {
69+
ensureExtra().add(key, value);
70+
return this;
71+
}
72+
73+
private JsonObject ensureExtra() {
74+
if (this.extra == null) {
75+
this.extra = new JsonObject();
76+
}
77+
return this.extra;
78+
}
5179
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/oa/doc/WxCpDocImageUploadResult.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,16 @@ public class WxCpDocImageUploadResult extends WxCpBaseResp implements Serializab
2626
@SerializedName("imageid")
2727
private String imageId;
2828

29+
@SerializedName("media_id")
30+
private String mediaId;
31+
2932
@SerializedName("md5")
3033
private String md5;
3134

35+
public String getEffectiveUrl() {
36+
return this.imageUrl != null ? this.imageUrl : this.url;
37+
}
38+
3239
public static WxCpDocImageUploadResult fromJson(String json) {
3340
return WxCpGsonBuilder.create().fromJson(json, WxCpDocImageUploadResult.class);
3441
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/oa/doc/WxCpDocModifyRequest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package me.chanjar.weixin.cp.bean.oa.doc;
22

3+
import com.google.gson.JsonArray;
34
import com.google.gson.JsonElement;
45
import com.google.gson.JsonObject;
56
import com.google.gson.annotations.SerializedName;
@@ -58,4 +59,40 @@ public String toJson() {
5859
}
5960
return WxCpGsonBuilder.create().toJson(jsonObject);
6061
}
62+
63+
public WxCpDocModifyRequest addRequest(JsonObject request) {
64+
JsonArray requestArray = this.requests != null && this.requests.isJsonArray()
65+
? this.requests.getAsJsonArray()
66+
: new JsonArray();
67+
requestArray.add(request);
68+
this.requests = requestArray;
69+
return this;
70+
}
71+
72+
public WxCpDocModifyRequest addExtra(String key, String value) {
73+
ensureExtra().addProperty(key, value);
74+
return this;
75+
}
76+
77+
public WxCpDocModifyRequest addExtra(String key, Number value) {
78+
ensureExtra().addProperty(key, value);
79+
return this;
80+
}
81+
82+
public WxCpDocModifyRequest addExtra(String key, Boolean value) {
83+
ensureExtra().addProperty(key, value);
84+
return this;
85+
}
86+
87+
public WxCpDocModifyRequest addExtra(String key, JsonElement value) {
88+
ensureExtra().add(key, value);
89+
return this;
90+
}
91+
92+
private JsonObject ensureExtra() {
93+
if (this.extra == null) {
94+
this.extra = new JsonObject();
95+
}
96+
return this.extra;
97+
}
6198
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/oa/doc/WxCpDocSmartSheetAuth.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ public class WxCpDocSmartSheetAuth extends WxCpBaseResp implements Serializable
3333
@SerializedName("record_auth")
3434
private JsonElement recordAuth;
3535

36+
public JsonElement getEffectiveAuthInfo() {
37+
if (this.authInfo != null) {
38+
return this.authInfo;
39+
}
40+
if (this.fieldAuth != null) {
41+
return this.fieldAuth;
42+
}
43+
return this.recordAuth;
44+
}
45+
3646
public static WxCpDocSmartSheetAuth fromJson(String json) {
3747
return WxCpGsonBuilder.create().fromJson(json, WxCpDocSmartSheetAuth.class);
3848
}

weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpOaWeDocServiceImplTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void testNewApisUseExpectedPaths() throws WxErrorException {
5151
when(cpService.post(eq("https://api.test/mod_doc"), anyString()))
5252
.thenReturn("{\"errcode\":0,\"errmsg\":\"ok\"}");
5353
when(cpService.upload(eq("https://api.test/upload_doc_image"), any()))
54-
.thenReturn("{\"errcode\":0,\"errmsg\":\"ok\",\"url\":\"https://img.test/a.png\"}");
54+
.thenReturn("{\"errcode\":0,\"errmsg\":\"ok\",\"image_url\":\"https://img.test/a.png\",\"media_id\":\"media-1\"}");
5555
when(cpService.post(eq("https://api.test/smartsheet/get_sheet_auth"), anyString()))
5656
.thenReturn("{\"errcode\":0,\"errmsg\":\"ok\",\"docid\":\"doc1\",\"sheet_id\":\"sheet1\"}");
5757
when(cpService.post(eq("https://api.test/smartsheet/mod_sheet_auth"), anyString()))
@@ -82,7 +82,8 @@ public void testNewApisUseExpectedPaths() throws WxErrorException {
8282
verify(cpService).post(eq("https://api.test/mod_doc"), anyString());
8383

8484
WxCpDocImageUploadResult uploadResult = service.docUploadImage(new File("demo.png"));
85-
assertThat(uploadResult.getUrl()).isEqualTo("https://img.test/a.png");
85+
assertThat(uploadResult.getEffectiveUrl()).isEqualTo("https://img.test/a.png");
86+
assertThat(uploadResult.getMediaId()).isEqualTo("media-1");
8687
verify(cpService).upload(eq("https://api.test/upload_doc_image"), any());
8788

8889
WxCpDocSmartSheetAuthRequest authRequest = WxCpDocSmartSheetAuthRequest.builder()

weixin-java-cp/src/test/java/me/chanjar/weixin/cp/bean/oa/doc/WxCpOaWeDocJsonTest.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package me.chanjar.weixin.cp.bean.oa.doc;
22

3-
import com.google.gson.JsonArray;
43
import com.google.gson.JsonObject;
54
import me.chanjar.weixin.cp.api.WxCpService;
65
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
@@ -157,29 +156,23 @@ public void testFormStatisticAndAnswerFromJson() {
157156

158157
@Test
159158
public void testDocGetDataAndModifyJson() {
160-
JsonObject extra = new JsonObject();
161-
extra.addProperty("start", 0);
162-
extra.addProperty("limit", 20);
163-
164159
WxCpDocGetDataRequest getDataRequest = WxCpDocGetDataRequest.builder()
165160
.docId("doc123")
166-
.extra(extra)
167161
.build();
162+
getDataRequest.addExtra("start", 0).addExtra("limit", 20);
168163
assertThat(getDataRequest.toJson()).contains("\"docid\":\"doc123\"");
169164
assertThat(getDataRequest.toJson()).contains("\"limit\":20");
170165

171-
JsonArray requests = new JsonArray();
172166
JsonObject insertRequest = new JsonObject();
173167
insertRequest.addProperty("op", "insert_text");
174168
insertRequest.addProperty("text", "hello");
175-
requests.add(insertRequest);
176-
177169
WxCpDocModifyRequest modifyRequest = WxCpDocModifyRequest.builder()
178170
.docId("doc123")
179-
.requests(requests)
180171
.build();
172+
modifyRequest.addRequest(insertRequest).addExtra("client_token", "token-1");
181173
assertThat(modifyRequest.toJson()).contains("\"requests\"");
182174
assertThat(modifyRequest.toJson()).contains("\"insert_text\"");
175+
assertThat(modifyRequest.toJson()).contains("\"client_token\":\"token-1\"");
183176

184177
String json = "{"
185178
+ "\"errcode\":0,"
@@ -192,22 +185,34 @@ public void testDocGetDataAndModifyJson() {
192185
WxCpDocData result = WxCpDocData.fromJson(json);
193186
assertThat(result.getDocId()).isEqualTo("doc123");
194187
assertThat(result.getContent().getAsJsonObject().getAsJsonArray("blocks")).hasSize(1);
188+
assertThat(result.getEffectiveContent().getAsJsonObject().getAsJsonArray("blocks")).hasSize(1);
195189
assertThat(result.getHasMore()).isTrue();
196190
assertThat(result.getNextCursor()).isEqualTo("cursor-1");
191+
192+
String docContentJson = "{"
193+
+ "\"errcode\":0,"
194+
+ "\"errmsg\":\"ok\","
195+
+ "\"docid\":\"doc123\","
196+
+ "\"doc_content\":{\"blocks\":[{\"block_id\":\"blk2\"}]}"
197+
+ "}";
198+
WxCpDocData docContentResult = WxCpDocData.fromJson(docContentJson);
199+
assertThat(docContentResult.getEffectiveContent().getAsJsonObject().getAsJsonArray("blocks")).hasSize(1);
197200
}
198201

199202
@Test
200203
public void testDocUploadImageAndSmartSheetAuthJson() {
201204
String uploadJson = "{"
202205
+ "\"errcode\":0,"
203206
+ "\"errmsg\":\"ok\","
204-
+ "\"url\":\"https://wedoc.test/image.png\","
207+
+ "\"image_url\":\"https://wedoc.test/image.png\","
205208
+ "\"imageid\":\"img123\","
209+
+ "\"media_id\":\"media123\","
206210
+ "\"md5\":\"abc\""
207211
+ "}";
208212
WxCpDocImageUploadResult uploadResult = WxCpDocImageUploadResult.fromJson(uploadJson);
209-
assertThat(uploadResult.getUrl()).isEqualTo("https://wedoc.test/image.png");
213+
assertThat(uploadResult.getEffectiveUrl()).isEqualTo("https://wedoc.test/image.png");
210214
assertThat(uploadResult.getImageId()).isEqualTo("img123");
215+
assertThat(uploadResult.getMediaId()).isEqualTo("media123");
211216

212217
JsonObject smartExtra = new JsonObject();
213218
smartExtra.addProperty("view_type", "field");
@@ -241,5 +246,6 @@ public void testDocUploadImageAndSmartSheetAuthJson() {
241246
assertThat(smartSheetAuth.getDocId()).isEqualTo("doc456");
242247
assertThat(smartSheetAuth.getAuthInfo().getAsJsonObject().get("mode").getAsString()).isEqualTo("custom");
243248
assertThat(smartSheetAuth.getFieldAuth().getAsJsonObject().getAsJsonArray("columns")).hasSize(1);
249+
assertThat(smartSheetAuth.getEffectiveAuthInfo().getAsJsonObject().get("mode").getAsString()).isEqualTo("custom");
244250
}
245251
}

0 commit comments

Comments
 (0)