Skip to content

Commit f110aa8

Browse files
committed
feat: add interface: create, delete field and create datasheet.
1 parent 8ff011e commit f110aa8

7 files changed

Lines changed: 342 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package cn.vika.client.api;
2+
3+
import cn.vika.client.api.exception.ApiException;
4+
import cn.vika.client.api.http.AbstractApi;
5+
import cn.vika.client.api.http.ApiHttpClient;
6+
import cn.vika.client.api.model.CreateDatasheetRequest;
7+
import cn.vika.client.api.model.CreateDatasheetResponse;
8+
import cn.vika.client.api.model.HttpResult;
9+
import cn.vika.core.http.GenericTypeReference;
10+
import cn.vika.core.http.HttpHeader;
11+
import cn.vika.core.utils.StringUtil;
12+
13+
/**
14+
* @author tao
15+
*/
16+
public class DatasheetApi extends AbstractApi {
17+
18+
private static final String POST_DATASHEET_PATH = "/spaces/%s/datasheets";
19+
20+
public DatasheetApi(ApiHttpClient apiHttpClient) {
21+
super(apiHttpClient);
22+
}
23+
24+
public CreateDatasheetResponse addDatasheet(String spaceId,
25+
CreateDatasheetRequest datasheet) {
26+
27+
checkPostDatasheetPathArgs(spaceId);
28+
29+
final String path = String.format(POST_DATASHEET_PATH, spaceId);
30+
31+
HttpResult<CreateDatasheetResponse> result = getDefaultHttpClient().post(
32+
path, new HttpHeader(), datasheet,
33+
new GenericTypeReference<HttpResult<CreateDatasheetResponse>>() {});
34+
return result.getData();
35+
}
36+
37+
private void checkPostDatasheetPathArgs(String spaceId) {
38+
39+
if (!StringUtil.hasText(spaceId)) {
40+
throw new ApiException("space id must be not null");
41+
}
42+
43+
}
44+
45+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package cn.vika.client.api;
2+
3+
import cn.vika.client.api.exception.ApiException;
4+
import cn.vika.client.api.http.AbstractApi;
5+
import cn.vika.client.api.http.ApiHttpClient;
6+
import cn.vika.client.api.model.CreateFieldRequest;
7+
import cn.vika.client.api.model.CreateFieldResponse;
8+
import cn.vika.client.api.model.HttpResult;
9+
import cn.vika.client.api.model.field.property.BaseFieldProperty;
10+
import cn.vika.core.http.GenericTypeReference;
11+
import cn.vika.core.http.HttpHeader;
12+
import cn.vika.core.utils.StringUtil;
13+
14+
/**
15+
* @author wuyitao
16+
*/
17+
public class FieldApi extends AbstractApi {
18+
19+
private static final String POST_FIELD_PATH = "/spaces/%s/datasheets/%s/fields";
20+
21+
private static final String DELETE_FIELD_PATH = "/spaces/%s/datasheets/%s/fields/%s";
22+
23+
24+
public FieldApi(ApiHttpClient apiHttpClient) {
25+
super(apiHttpClient);
26+
}
27+
28+
public CreateFieldResponse addField(String spaceId, String datasheetId,
29+
CreateFieldRequest<? extends BaseFieldProperty> field) throws ApiException {
30+
31+
checkPostFieldPathArgs(spaceId, datasheetId);
32+
33+
// TODO: 校验CreateFieldRequest对象
34+
35+
final String path = String.format(POST_FIELD_PATH, spaceId, datasheetId);
36+
HttpResult<CreateFieldResponse> result = getDefaultHttpClient().post(
37+
path, new HttpHeader(), field,
38+
new GenericTypeReference<HttpResult<CreateFieldResponse>>() {});
39+
return result.getData();
40+
}
41+
42+
private void checkPostFieldPathArgs(String spaceId, String datasheetId) {
43+
44+
if (!StringUtil.hasText(spaceId)) {
45+
throw new ApiException("space id must be not null");
46+
}
47+
48+
if (!StringUtil.hasText(datasheetId)) {
49+
throw new ApiException("datasheet id must be not null");
50+
}
51+
52+
}
53+
54+
public void deleteField(String spaceId, String datasheetId, String fieldId) {
55+
56+
checkDeleteFieldPathArgs(spaceId, datasheetId, fieldId);
57+
58+
final String path = String.format(DELETE_FIELD_PATH, spaceId, datasheetId, fieldId);
59+
60+
getDefaultHttpClient().delete(path, new HttpHeader(), Void.class);
61+
62+
}
63+
64+
private void checkDeleteFieldPathArgs(String spaceId, String datasheetId, String fieldId) {
65+
if (!StringUtil.hasText(spaceId)) {
66+
throw new ApiException("space id must be not null");
67+
}
68+
69+
if (!StringUtil.hasText(datasheetId)) {
70+
throw new ApiException("datasheet id must be not null");
71+
}
72+
73+
if (!StringUtil.hasText(fieldId)) {
74+
throw new ApiException("fieldId id must be not null");
75+
}
76+
}
77+
}

client/src/main/java/cn/vika/client/api/VikaApiClient.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ public class VikaApiClient {
4949

5050
private NodeApi nodeApi;
5151

52+
private volatile FieldApi fieldApi;
53+
54+
private volatile DatasheetApi datasheetApi;
55+
5256
public VikaApiClient(ApiCredential credential) {
5357
this(ApiVersion.V1, DEFAULT_HOST, credential);
5458
}
@@ -159,4 +163,26 @@ public NodeApi getNodeApi() {
159163
}
160164
return this.nodeApi;
161165
}
166+
167+
public FieldApi getFieldApi() {
168+
if (this.fieldApi == null) {
169+
synchronized (this) {
170+
if (this.fieldApi == null) {
171+
this.fieldApi = new FieldApi(this.apiHttpClient);
172+
}
173+
}
174+
}
175+
return fieldApi;
176+
}
177+
178+
public DatasheetApi getDatasheetApi() {
179+
if (this.datasheetApi == null) {
180+
synchronized (this) {
181+
if (this.datasheetApi == null) {
182+
this.datasheetApi = new DatasheetApi(this.apiHttpClient);
183+
}
184+
}
185+
}
186+
return datasheetApi;
187+
}
162188
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package cn.vika.client.api.model;
2+
3+
import java.util.List;
4+
5+
/**
6+
* @author tao
7+
*/
8+
public class CreateDatasheetRequest {
9+
10+
private String name;
11+
12+
private String description;
13+
14+
private String folderId;
15+
16+
private String preNodeId;
17+
18+
private List<CreateFieldRequest<?>> fields;
19+
20+
public String getName() {
21+
return name;
22+
}
23+
24+
public void setName(String name) {
25+
this.name = name;
26+
}
27+
28+
public String getDescription() {
29+
return description;
30+
}
31+
32+
public void setDescription(String description) {
33+
this.description = description;
34+
}
35+
36+
public String getFolderId() {
37+
return folderId;
38+
}
39+
40+
public void setFolderId(String folderId) {
41+
this.folderId = folderId;
42+
}
43+
44+
public String getPreNodeId() {
45+
return preNodeId;
46+
}
47+
48+
public void setPreNodeId(String preNodeId) {
49+
this.preNodeId = preNodeId;
50+
}
51+
52+
public List<CreateFieldRequest<?>> getFields() {
53+
return fields;
54+
}
55+
56+
public void setFields(List<CreateFieldRequest<?>> fields) {
57+
this.fields = fields;
58+
}
59+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package cn.vika.client.api.model;
2+
3+
import java.util.List;
4+
5+
/**
6+
* @author tao
7+
*/
8+
public class CreateDatasheetResponse {
9+
10+
private String id;
11+
12+
private long createdAt;
13+
14+
private List<CreateFieldResponse> fields;
15+
16+
public String getId() {
17+
return id;
18+
}
19+
20+
public void setId(String id) {
21+
this.id = id;
22+
}
23+
24+
public long getCreatedAt() {
25+
return createdAt;
26+
}
27+
28+
public void setCreatedAt(long createdAt) {
29+
this.createdAt = createdAt;
30+
}
31+
32+
public List<CreateFieldResponse> getFields() {
33+
return fields;
34+
}
35+
36+
public void setFields(List<CreateFieldResponse> fields) {
37+
this.fields = fields;
38+
}
39+
40+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package cn.vika.client.api.model;
2+
3+
import cn.vika.client.api.model.field.property.BaseFieldProperty;
4+
5+
/**
6+
* @author tao
7+
*/
8+
public class CreateFieldRequest <T extends BaseFieldProperty> {
9+
10+
/**
11+
* field type
12+
* @see cn.vika.client.api.model.field.FieldType
13+
*/
14+
private String type;
15+
16+
/**
17+
* field name
18+
*/
19+
private String name;
20+
21+
/**
22+
* field property
23+
* @see cn.vika.client.api.model.field.property.BaseFieldProperty
24+
* value: BaseFieldProperty or it's subtype class
25+
*/
26+
private T property;
27+
28+
public String getType() {
29+
return type;
30+
}
31+
32+
public void setType(String type) {
33+
this.type = type;
34+
}
35+
36+
public String getName() {
37+
return name;
38+
}
39+
40+
public void setName(String name) {
41+
this.name = name;
42+
}
43+
44+
public T getProperty() {
45+
return property;
46+
}
47+
48+
public void setProperty(T property) {
49+
this.property = property;
50+
}
51+
52+
@Override
53+
public String toString() {
54+
return "CreateFieldRequest{" +
55+
"type='" + type + '\'' +
56+
", name='" + name + '\'' +
57+
", property=" + property +
58+
'}';
59+
}
60+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package cn.vika.client.api.model;
2+
3+
/**
4+
* @author tao
5+
*/
6+
public class CreateFieldResponse {
7+
8+
private String id;
9+
10+
private String name;
11+
12+
public String getId() {
13+
return id;
14+
}
15+
16+
public void setId(String id) {
17+
this.id = id;
18+
}
19+
20+
public String getName() {
21+
return name;
22+
}
23+
24+
public void setName(String name) {
25+
this.name = name;
26+
}
27+
28+
@Override
29+
public String toString() {
30+
return "CreateFieldResponse{" +
31+
"id='" + id + '\'' +
32+
", name='" + name + '\'' +
33+
'}';
34+
}
35+
}

0 commit comments

Comments
 (0)