Skip to content

Commit cbe16b8

Browse files
authored
Merge pull request #16 from vikadata/fix-field-key
add fieldKey request support
2 parents a6caaf1 + 61ac117 commit cbe16b8

File tree

10 files changed

+254
-43
lines changed

10 files changed

+254
-43
lines changed

README.md

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![LGPL-2.1](https://img.shields.io/badge/License-LGPL--2.1-blue.svg)](https://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt)
44
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/vikadata/vika-sdk-java/badge.svg)](https://search.maven.org/artifact/cn.vika/vika-client)
55
[![Build](https://www.travis-ci.com/vikadata/vika.java.svg?branch=master)](https://www.travis-ci.com/github/vikadata/vika.java)
6-
[![JavaDoc](https://javadoc.io/badge2/cn.vika/vika.java/javadoc.io.svg)](https://javadoc.io/doc/cn.vika/vika.java)
6+
[![JavaDoc](https://javadoc.io/badge2/cn.vika/vika.java/javadoc.io.svg)](https://javadoc.io/doc/cn.vika/vika-client)
77

88
[github_issues]:https://github.com/vikadata/vika.java/issues
99
[github_issues_new]:https://github.com/vikadata/vika.java/issues/new
@@ -15,7 +15,7 @@
1515
Vikadata™ Java API (*vika.java*) provides a full featured and easy to consume Java
1616
library for working with vikadata via the Vikadata OpenAPI.<br/>
1717

18-
---
18+
---
1919

2020
## Usage
2121

@@ -56,7 +56,7 @@ simple as:
5656
First, you need to set api credential which belong your personal api key.
5757

5858
```java
59-
ApiCredential credential=new ApiCredential("Your API Key");
59+
ApiCredential credential = new ApiCredential("Your API Key");
6060
```
6161

6262
Then, Init client instance
@@ -65,7 +65,7 @@ Then, Init client instance
6565
VikaApiClient vikaApiClient = new VikaApiClient(credential);
6666
```
6767

68-
By default, the API client has been added for setting connect and read timeouts, you can also change:
68+
By default, the API client has been added for setting connect and read timeouts, you can also change:
6969

7070
```java
7171
// Set the connect timeout to 8 second and the read timeout to 9 seconds
@@ -142,6 +142,9 @@ Pager<Record> pager = vikaApiClient.getRecordApi().getRecords("datasheet_id", qu
142142

143143
Class ``RecordMap`` is a key-value structure like ``Map<String, Object>``, all thing you do is converting json to map, you can use convert util from sdk provide which is named ``JacksonConverter``, you also can use jackson api build json structure data, more detail please reference unit test.
144144

145+
you can add record through two difference way, `id` or `name`, default is `name` fieldKey.
146+
147+
using default fieldKey `name` example:
145148
```java
146149
// Build Record Map by jackson api
147150
ObjectNode fieldMap = JsonNodeFactory.instance.objectNode()
@@ -157,26 +160,74 @@ ArrayNode arrayNode = JsonNodeFactory.instance.arrayNode().add(fields);
157160
// convert json to Map List
158161
List<RecordMap> recordMaps = JacksonConverter.unmarshalToList(RecordMap.class, arrayNode);
159162
// create record request
160-
CreateRecordRequest recordRequest = new CreateRecordRequest().withRecords(recordMaps);
163+
CreateRecordRequest recordRequest = new CreateRecordRequest()
164+
.withRecords(recordMaps);
165+
// ok
166+
List<Record> newRecords = vikaApiClient.getRecordApi().addRecords("datasheet_id", recordRequest);
167+
```
168+
169+
using fieldKey `id` example:
170+
```java
171+
// Build Record Map by jackson api
172+
ObjectNode fieldMap = JsonNodeFactory.instance.objectNode()
173+
// simple data
174+
.put("fld_id", "string")
175+
.put("fld_id", 1234);
176+
// sub tree node
177+
.set("fld_id", JsonNodeFactory.instance.arrayNode().add("NewYork").add("Bejing"));
178+
// put record map into fields key
179+
ObjectNode fields = JsonNodeFactory.instance.objectNode().set("fields", fieldMap);
180+
// only one record, warp record into array node
181+
ArrayNode arrayNode = JsonNodeFactory.instance.arrayNode().add(fields);
182+
// convert json to Map List
183+
List<RecordMap> recordMaps = JacksonConverter.unmarshalToList(RecordMap.class, arrayNode);
184+
// create record request
185+
CreateRecordRequest recordRequest = new CreateRecordRequest()
186+
.withRecords(recordMaps)
187+
.withFieldKey(FieldKey.ID);
161188
// ok
162189
List<Record> newRecords = vikaApiClient.getRecordApi().addRecords("datasheet_id", recordRequest);
163190
```
164191

165192
#### **Update Record**
166193

194+
update record also provide two difference way to modifying data, using fieldKey `id` or `name`, default is `name` fieldKey.
195+
196+
using default fieldKey `name` example:
197+
167198
```java
168199
// Build update record model
169200
UpdateRecord record = new UpdateRecord()
170201
// row record id from query result or add record result
171202
.withRecordId("recXXXXX")
172-
// single-text type field cell
203+
// single-text type field cell
173204
.withField("SingleText", "ABC")
174-
// single-select type field cell,
205+
// single-select type field cell,
175206
// it can be set null or empty array if you want to clear field value: withField("Options", null)
176207
.withField("Options", Arrays.asList("LL", "NN"));
177208
// new Request model
178209
UpdateRecordRequest updateRecordRequest = new UpdateRecordRequest()
179-
.withRecords(Collections.singletonList(record));
210+
.withRecords(Collections.singletonList(record));
211+
// request send
212+
List<Record> updateRecords = vikaApiClient.getRecordApi().updateRecords("datasheet_id", updateRecordRequest);
213+
```
214+
215+
using fieldKey `id` example:
216+
217+
```java
218+
// Build update record model
219+
UpdateRecord record = new UpdateRecord()
220+
// row record id from query result or add record result
221+
.withRecordId("recXXXXX")
222+
// single-text type field cell
223+
.withField("fld_id", "ABC")
224+
// single-select type field cell,
225+
// it can be set null or empty array if you want to clear field value: withField("Options", null)
226+
.withField("fld_id", Arrays.asList("LL", "NN"));
227+
// new Request model
228+
UpdateRecordRequest updateRecordRequest = new UpdateRecordRequest()
229+
.withRecords(Collections.singletonList(record))
230+
.withFieldKey(FieldKey.ID);
180231
// request send
181232
List<Record> updateRecords = vikaApiClient.getRecordApi().updateRecords("datasheet_id", updateRecordRequest);
182233
```

client/src/main/java/cn/vika/client/api/model/CreateRecordRequest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,29 @@
2020

2121
import java.util.List;
2222

23+
import com.fasterxml.jackson.annotation.JsonInclude;
24+
import com.fasterxml.jackson.annotation.JsonInclude.Include;
25+
2326
/**
2427
* Create Record Model
2528
* @author Shawn Deng
2629
* @date 2021-02-19 15:41:12
2730
*/
2831
public class CreateRecordRequest {
2932

33+
@JsonInclude(Include.NON_NULL)
34+
private FieldKey fieldKey;
35+
3036
private List<RecordMap> records;
3137

38+
public FieldKey getFieldKey() {
39+
return fieldKey;
40+
}
41+
42+
public void setFieldKey(FieldKey fieldKey) {
43+
this.fieldKey = fieldKey;
44+
}
45+
3246
public List<RecordMap> getRecords() {
3347
return records;
3448
}
@@ -41,4 +55,9 @@ public CreateRecordRequest withRecords(List<RecordMap> records) {
4155
this.records = records;
4256
return this;
4357
}
58+
59+
public CreateRecordRequest withFieldKey(FieldKey fieldKey) {
60+
this.fieldKey = fieldKey;
61+
return this;
62+
}
4463
}

client/src/main/java/cn/vika/client/api/model/FieldKey.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
package cn.vika.client.api.model;
2020

21+
import com.fasterxml.jackson.annotation.JsonValue;
22+
2123
/**
2224
* FieldKey enum
2325
*
@@ -28,9 +30,20 @@ public enum FieldKey {
2830
/**
2931
* find fields with name
3032
*/
31-
Name,
33+
Name("name"),
3234
/**
3335
* find fields with id
3436
*/
35-
ID
37+
ID("id");
38+
39+
private final String value;
40+
41+
FieldKey(String value) {
42+
this.value = value;
43+
}
44+
45+
@JsonValue
46+
public String getValue() {
47+
return value;
48+
}
3649
}

client/src/main/java/cn/vika/client/api/model/UpdateRecordRequest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,29 @@
2020

2121
import java.util.List;
2222

23+
import com.fasterxml.jackson.annotation.JsonInclude;
24+
import com.fasterxml.jackson.annotation.JsonInclude.Include;
25+
2326
/**
2427
* Create Record Model
2528
* @author Shawn Deng
2629
* @date 2021-02-19 15:41:12
2730
*/
2831
public class UpdateRecordRequest {
2932

33+
@JsonInclude(Include.NON_NULL)
34+
private FieldKey fieldKey;
35+
3036
private List<UpdateRecord> records;
3137

38+
public FieldKey getFieldKey() {
39+
return fieldKey;
40+
}
41+
42+
public void setFieldKey(FieldKey fieldKey) {
43+
this.fieldKey = fieldKey;
44+
}
45+
3246
public List<UpdateRecord> getRecords() {
3347
return records;
3448
}
@@ -41,4 +55,9 @@ public UpdateRecordRequest withRecords(List<UpdateRecord> records) {
4155
this.records = records;
4256
return this;
4357
}
58+
59+
public UpdateRecordRequest withFieldKey(FieldKey fieldKey) {
60+
this.fieldKey = fieldKey;
61+
return this;
62+
}
4463
}

0 commit comments

Comments
 (0)