Skip to content
This repository was archived by the owner on Mar 31, 2025. It is now read-only.

Commit 663f49e

Browse files
committed
Tests extended
1 parent 53ab127 commit 663f49e

4 files changed

Lines changed: 185 additions & 122 deletions

File tree

src/main/java/com/sybit/airtable/Table.java

Lines changed: 76 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,17 @@
3030
/**
3131
* Representation Class of Airtable Tables.
3232
*
33+
* @param <T>
3334
* @since 0.1
3435
*/
3536
public class Table<T> {
3637

3738
private static final Logger LOG = LoggerFactory.getLogger(Table.class);
39+
40+
private static final String MIME_TYPE_JSON = "application/json";
41+
42+
private static final String FIELD_ID = "id";
43+
private static final String FIELD_CREATED_TIME = "createdTime";
3844

3945
private final String name;
4046
private final Class<T> type;
@@ -135,9 +141,9 @@ public List<T> select(final Query query) throws AirtableException {
135141
HttpResponse<Records> response;
136142
try {
137143
final GetRequest request = Unirest.get(getTableEndpointUrl())
138-
.header("accept", "application/json")
144+
.header("accept", MIME_TYPE_JSON)
139145
.header("Authorization", getBearerToken())
140-
.header("Content-type" , "application/json");
146+
.header("Content-type" , MIME_TYPE_JSON);
141147

142148
if (query.getFields() != null && query.getFields().length > 0) {
143149
String[] fields = query.getFields();
@@ -198,9 +204,9 @@ public List<T> select(final Query query) throws AirtableException {
198204
}
199205

200206
/**
201-
* Get List with offset.
207+
* Get <code>List</code> by given offset.
202208
*
203-
* @param query
209+
* @param query
204210
* @param offset
205211
* @return
206212
* @throws AirtableException
@@ -245,9 +251,9 @@ public String getOffset() {
245251
}
246252

247253
/**
248-
* select with Parameter maxRecords
254+
* Select with parameter maxRecords
249255
*
250-
* @param maxRecords
256+
* @param maxRecords maximum of records per request.
251257
* @return
252258
* @throws AirtableException
253259
* @throws HttpResponseException
@@ -466,14 +472,14 @@ private List<T> getList(HttpResponse<Records> response) {
466472
* @return searched record.
467473
* @throws AirtableException
468474
*/
469-
public T find(String id) throws AirtableException {
475+
public T find(final String id) throws AirtableException {
470476

471477
RecordItem body;
472478

473479
HttpResponse<RecordItem> response;
474480
try {
475481
response = Unirest.get(getTableEndpointUrl() + "/" + id)
476-
.header("accept", "application/json")
482+
.header("accept", MIME_TYPE_JSON)
477483
.header("Authorization", getBearerToken())
478484
.asObject(RecordItem.class);
479485
} catch (UnirestException e) {
@@ -505,21 +511,21 @@ public T find(String id) throws AirtableException {
505511
* @throws InvocationTargetException
506512
* @throws NoSuchMethodException
507513
*/
508-
public T create(T item) throws AirtableException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
514+
public T create(final T item) throws AirtableException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
509515

510516
RecordItem responseBody = null;
511517

512518
checkProperties(item);
513519

514-
PostRecord body = new PostRecord<T>();
520+
PostRecord body = new PostRecord<>();
515521
body.setFields(item);
516522

517523
HttpResponse<RecordItem> response;
518524
try {
519525
response = Unirest.post(getTableEndpointUrl())
520-
.header("accept", "application/json")
526+
.header("accept", MIME_TYPE_JSON)
521527
.header("Authorization", getBearerToken())
522-
.header("Content-type", "application/json")
528+
.header("Content-type", MIME_TYPE_JSON)
523529
.body(body)
524530
.asObject(RecordItem.class);
525531
} catch (UnirestException e) {
@@ -543,21 +549,31 @@ public T create(T item) throws AirtableException, IllegalAccessException, Invoca
543549
return null;
544550
}
545551

546-
public T update(T item) throws AirtableException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
552+
/**
553+
* Update given <code>item</code> in storage.
554+
*
555+
* @param item Item to update.
556+
* @return updated <code>item</code> returned by airtable.
557+
* @throws AirtableException
558+
* @throws IllegalAccessException
559+
* @throws InvocationTargetException
560+
* @throws NoSuchMethodException
561+
*/
562+
public T update(final T item) throws AirtableException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
547563

548564
RecordItem responseBody = null;
549565

550566
String id = getIdOfItem(item);
551567

552-
PostRecord body = new PostRecord<T>();
568+
PostRecord body = new PostRecord<>();
553569
body.setFields(filterFields(item));
554570

555571
HttpResponse<RecordItem> response;
556572
try {
557573
response = Unirest.patch(getTableEndpointUrl() + "/" + id)
558-
.header("accept", "application/json")
574+
.header("accept", MIME_TYPE_JSON)
559575
.header("Authorization", getBearerToken())
560-
.header("Content-type", "application/json")
576+
.header("Content-type", MIME_TYPE_JSON)
561577
.body(body)
562578
.asObject(RecordItem.class);
563579
} catch (UnirestException e) {
@@ -572,16 +588,22 @@ public T update(T item) throws AirtableException, IllegalAccessException, Invoca
572588
HttpResponseExceptionHandler.onResponse(response);
573589
}
574590

591+
T result;
575592
try {
576-
return transform(responseBody, this.type.newInstance());
593+
result = transform(responseBody, this.type.newInstance());
577594
} catch (InvocationTargetException | IllegalAccessException | InstantiationException e) {
578595
LOG.error(e.getMessage(), e);
596+
result = null;
579597
}
580598

581-
return null;
582-
599+
return result;
583600
}
584601

602+
/**
603+
*
604+
* @param item
605+
* @return
606+
*/
585607
public T replace(T item) {
586608

587609
throw new UnsupportedOperationException("not yet implemented");
@@ -598,12 +620,12 @@ public T replace(T item) {
598620
public boolean destroy(String id) throws AirtableException {
599621

600622

601-
Delete body = null;
623+
boolean isDeleted;
602624

603625
HttpResponse<Delete> response;
604626
try {
605627
response = Unirest.delete(getTableEndpointUrl() + "/" + id)
606-
.header("accept", "application/json")
628+
.header("accept", MIME_TYPE_JSON)
607629
.header("Authorization", getBearerToken())
608630
.asObject(Delete.class);
609631
} catch (UnirestException e) {
@@ -612,16 +634,18 @@ public boolean destroy(String id) throws AirtableException {
612634
int code = response.getStatus();
613635

614636
if (200 == code) {
615-
body = response.getBody();
637+
Delete body = response.getBody();
638+
isDeleted = body.isDeleted();
616639
} else {
640+
isDeleted = false;
617641
HttpResponseExceptionHandler.onResponse(response);
618642
}
619643

620644
// if (!body.isDeleted()) {
621645
// throw new AirtableException("Record id: " + body.getId() + " could not be deleted.");
622646
// }
623647

624-
return body.isDeleted();
648+
return isDeleted;
625649
}
626650

627651
/**
@@ -681,8 +705,8 @@ private T transform(Map<String, Object> record, T retval) throws InvocationTarge
681705
* @throws IllegalAccessException
682706
*/
683707
private T transform(RecordItem record, T retval) throws InvocationTargetException, IllegalAccessException {
684-
setProperty(retval, "id", record.getId());
685-
setProperty(retval, "createdTime", record.getCreatedTime());
708+
setProperty(retval, FIELD_ID, record.getId());
709+
setProperty(retval, FIELD_CREATED_TIME, record.getCreatedTime());
686710

687711
retval = transform(record.getFields(), retval);
688712

@@ -700,7 +724,7 @@ private T transform(RecordItem record, T retval) throws InvocationTargetExceptio
700724
private void setProperty(T retval, String key, Object value) throws IllegalAccessException, InvocationTargetException {
701725
String property = key2property(key);
702726

703-
for (Field f : this.type.getDeclaredFields()) {
727+
for (final Field f : this.type.getDeclaredFields()) {
704728
final SerializedName annotation = f.getAnnotation(SerializedName.class);
705729

706730
if (annotation != null && property.equalsIgnoreCase(annotation.value())) {
@@ -722,7 +746,11 @@ private void setProperty(T retval, String key, Object value) throws IllegalAcces
722746
* @param key
723747
* @return
724748
*/
725-
private String key2property(final String key) {
749+
String key2property(final String key) {
750+
751+
if(key == null || key.isEmpty()) {
752+
throw new IllegalArgumentException("Key was null or empty.");
753+
}
726754

727755
if (key.contains(" ") || key.contains("-")) {
728756
LOG.warn("Annotate columns having special characters by using @SerializedName for property: [" + key + "]");
@@ -756,11 +784,11 @@ private static boolean propertyExists(Object bean, String property) {
756784
*/
757785
private void checkProperties(T item) throws AirtableException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
758786

759-
if (propertyExists(item, "id") || propertyExists(item, "createdTime")) {
787+
if (propertyExists(item, FIELD_ID) || propertyExists(item, FIELD_CREATED_TIME)) {
760788
Field[] attributes = item.getClass().getDeclaredFields();
761789
for (Field attribute : attributes) {
762790
String attrName = attribute.getName();
763-
if ("id".equals(attrName) || "createdTime".equals(attrName)) {
791+
if (FIELD_ID.equals(attrName) || FIELD_CREATED_TIME.equals(attrName)) {
764792
if (BeanUtils.getProperty(item, attribute.getName()) != null) {
765793
throw new AirtableException("Property " + attrName + " should be null!");
766794
}
@@ -773,15 +801,27 @@ private void checkProperties(T item) throws AirtableException, IllegalAccessExce
773801

774802
}
775803

804+
/**
805+
* Check properties of Attachement objects.
806+
*
807+
* @param attachements
808+
* @throws AirtableException
809+
* @throws IllegalAccessException
810+
* @throws InvocationTargetException
811+
* @throws NoSuchMethodException
812+
*/
776813
private void checkPropertiesOfAttachement(List<Attachment> attachements) throws AirtableException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
777814

778815
if (attachements != null) {
779816
for (int i = 0; i < attachements.size(); i++) {
780-
if (propertyExists(attachements.get(i), "id") || propertyExists(attachements.get(i), "size") || propertyExists(attachements.get(i), "type") || propertyExists(attachements.get(i), "filename")) {
781-
Field[] attributesPhotos = attachements.getClass().getDeclaredFields();
817+
if (propertyExists(attachements.get(i), FIELD_ID) || propertyExists(attachements.get(i), "size")
818+
|| propertyExists(attachements.get(i), "type") || propertyExists(attachements.get(i), "filename")) {
819+
820+
final Field[] attributesPhotos = attachements.getClass().getDeclaredFields();
782821
for (Field attributePhoto : attributesPhotos) {
783-
String namePhotoAttribute = attributePhoto.getName();
784-
if (namePhotoAttribute.equals("id") || namePhotoAttribute.equals("size") || namePhotoAttribute.equals("Tpye") || namePhotoAttribute.equals("filename")) {
822+
final String namePhotoAttribute = attributePhoto.getName();
823+
if (FIELD_ID.equals(namePhotoAttribute) || "size".equals(namePhotoAttribute)
824+
|| "type".equals(namePhotoAttribute) || "filename".equals(namePhotoAttribute)) {
785825
if (BeanUtils.getProperty(attachements.get(i), namePhotoAttribute) != null) {
786826
throw new AirtableException("Property " + namePhotoAttribute + " should be null!");
787827
}
@@ -793,7 +833,6 @@ private void checkPropertiesOfAttachement(List<Attachment> attachements) throws
793833
}
794834

795835
/**
796-
*
797836
* Get the String Id from the item.
798837
*
799838
* @param item
@@ -805,8 +844,8 @@ private void checkPropertiesOfAttachement(List<Attachment> attachements) throws
805844
*/
806845
private String getIdOfItem(T item) throws AirtableException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
807846

808-
if (propertyExists(item, "id")) {
809-
final String id = BeanUtils.getProperty(item, "id");
847+
if (propertyExists(item, FIELD_ID)) {
848+
final String id = BeanUtils.getProperty(item, FIELD_ID);
810849
if (id != null) {
811850
return id;
812851
}
@@ -831,7 +870,7 @@ private T filterFields(T item) throws IllegalAccessException, InvocationTargetEx
831870

832871
for (Field attribute : attributes) {
833872
String attrName = attribute.getName();
834-
if (("id".equals(attrName) || "createdTime".equals(attrName))
873+
if ((FIELD_ID.equals(attrName) || FIELD_CREATED_TIME.equals(attrName))
835874
&& (BeanUtils.getProperty(item, attrName) != null)) {
836875
BeanUtilsBean.getInstance().getPropertyUtils().setProperty(item, attrName, null);
837876
}

0 commit comments

Comments
 (0)