|
| 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 | +} |
0 commit comments