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

Commit 2f9032b

Browse files
author
Zelle97
authored
Merge pull request #13 from Sybit-Education/develop
Develop
2 parents fc69941 + 9a2c003 commit 2f9032b

17 files changed

Lines changed: 488 additions & 28 deletions

README.md

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,14 @@ logging framework at deployment time.
6767
The API of Airtable itself is limited to 5 requests per second. If you exceed this rate, you will receive a 429 status code and will
6868
need to wait 30 seconds before subsequent requests will succeed.
6969

70-
*TODO:*
71-
* How to create an Airtable Object
72-
* How to create an Airtable Base
70+
### Connecting to Airtable
71+
To use this Libraray you will need a Airtable Object. Simply create one! `Airtable airtable = new Airtable();`.
72+
This Object needs an API-Key or it won't work properly so `airtable.configure(AIRTABLE_API_KEY);`.
73+
Now the Airtable Object needs to know on which base you want access. This Method will return a Base Object which will be used in the Future.
74+
`Base base = airtable.base(AIRTABLE_BASE);`
75+
76+
With the Base object you can perform all kind of Operations see more [here](#crud-operations-on-table-records).
77+
7378

7479
## Object Mapping
7580
The Java implementation of the Airtable API provides automatic Object mapping. You can map any Table to your own Java Classes.
@@ -146,7 +151,7 @@ Now our Java Class should look like this:
146151
}
147152
}
148153
```
149-
For each column we give the Java class an attribute with the column name (Be careful! See more about naming in the [Section 'Annotations'](#annotations))
154+
For each column we give the Java class an attribute with the column name (Be careful! See more about naming in the Section [Annotations](#annotations))
150155
and add Getters and Setters for each attribute. The attribute types can be either primitive Java types like `String` and `Float` for Text and Numbers,
151156
`String Array` for references on other Tables or `Attachment` for attached photos and files.
152157

@@ -209,6 +214,21 @@ The airtable.java API will respect these mappings automatically.
209214
@SerializedName("First- & Lastname")
210215
private String name;
211216
```
217+
### Sort
218+
With the integrated Sort element you can retrieve a list of sort objects that specifies how the records will be ordered.
219+
Each sort object must have a field key specifying the name of the field to sort on, and an optional direction key that is either "asc" or "desc".
220+
The default direction is "asc".
221+
222+
For example, to sort records by Name, pass in:
223+
224+
```Java
225+
Sort sort = new Sort("Name", Sort.Direction.desc);
226+
List<Movie> listMovies = movieTable.select(sort);
227+
```
228+
If you set the view parameter, the returned records in that view will be sorted by these fields.
229+
230+
Detailed Example see [TableParameterTest](https://github.com/Sybit-Education/airtable.java/blob/develop/src/test/java/com/sybit/airtable/TableParameterTest.java)
231+
212232

213233
## CRUD-Operations on Table Records
214234

@@ -217,9 +237,11 @@ Select list of items from table:
217237

218238
+ `table(name).select()`: get all records of table `name`
219239
+ `table(name).select(Integer maxRecords)`: get max `maxRecords` records of table `name`
240+
+ `table(name).select(String[] fields)`: get records of table `name` with only the specified `fields`
241+
+ `table(name).select(String view)`: get records of table `name` with the specified `view` (more about [views](https://support.airtable.com/hc/en-us/sections/200644955-Views))
242+
+ `table(name).select(Sort sortation)`: get records of table `name` using `sort` to sort records (More about Sort [here](#sort))
220243
+ `table(name).select(Query query)`: get records of table `name` using `query` to filter
221244

222-
+ `...`
223245

224246
### Example
225247
```Java
@@ -276,6 +298,8 @@ newActor.setName("Neuer Actor");
276298
Actor test = actorTable.create(newActor);
277299
```
278300

301+
Detailed example see [TableDestroyTest.java](https://github.com/Sybit-Education/airtable.java/blob/develop/src/test/java/com/sybit/airtable/TableCreateRecordTest.java)
302+
279303
## Update
280304
Use `update` to update a record of table:
281305

@@ -292,6 +316,8 @@ marlonBrando.setName("Marlon Brando");
292316
Actor updated = actorTable.update(marlonBrando);
293317
```
294318

319+
Detailed example see [TableUpdateTest](https://github.com/Sybit-Education/airtable.java/blob/develop/src/test/java/com/sybit/airtable/TableUpdateTest.java)
320+
295321
# Roadmap
296322

297323
Short overview of features, which are supported:
@@ -304,8 +330,8 @@ Short overview of features, which are supported:
304330
+ [x] Select Records
305331
+ [x] SelectAll
306332
+ [x] Queries (`maxRecords`, `sort` & `view` )
307-
+ [ ] Support of `filterByFormula`
308-
+ [ ] Support of Paging
333+
+ [x] Support of `filterByFormula`
334+
+ [x] Support of Paging
309335

310336
+ [x] Find Record
311337

build.gradle

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import com.smokejumperit.gradle.report.DependencyLicensePlugin
1+
/*
2+
* The MIT License (MIT)
3+
* Copyright (c) 2017 Sybit GmbH
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
*/
27

38
/*
49
* Gets the version name from the latest Git tag
@@ -16,12 +21,6 @@ def getVersionName = { ->
1621
return version;
1722
}
1823
}
19-
/*
20-
* The MIT License (MIT)
21-
* Copyright (c) 2017 Sybit GmbH
22-
*
23-
* Permission is hereby granted, free of charge, to any person obtaining a copy
24-
*/
2524

2625
buildscript {
2726
repositories {
@@ -40,7 +39,6 @@ apply plugin: 'maven'
4039
apply plugin: 'com.palantir.jacoco-coverage'
4140
apply plugin: 'maven-publish'
4241
apply plugin: 'com.jfrog.bintray'
43-
apply plugin: DependencyLicensePlugin
4442

4543
group = 'com.sybit'
4644
version = getVersionName()
@@ -82,18 +80,22 @@ dependencies {
8280
codacy group: 'com.codacy', name: 'codacy-coverage-reporter', version: '1.0.13'
8381
}
8482

85-
86-
// custom tasks for creating source/javadoc jars
83+
// custom tasks for creating source jar
8784
task sourcesJar(type: Jar, dependsOn: classes) {
8885
classifier = 'sources'
8986
from sourceSets.main.allSource
9087
}
91-
88+
// custom tasks for creating javadoc jar
9289
task javadocJar(type: Jar, dependsOn: javadoc) {
9390
classifier = 'javadoc'
9491
from javadoc.destinationDir
9592
}
9693

94+
// add javadoc/source jar tasks as artifacts
95+
artifacts {
96+
archives sourcesJar, javadocJar
97+
}
98+
9799
task sendCoverageToCodacy(type: JavaExec, dependsOn: jacocoTestReport) {
98100
main = "com.codacy.CodacyCoverageReporter"
99101
classpath = configurations.codacy
@@ -105,10 +107,6 @@ task sendCoverageToCodacy(type: JavaExec, dependsOn: jacocoTestReport) {
105107
]
106108
}
107109

108-
// add javadoc/source jar tasks as artifacts
109-
artifacts {
110-
archives sourcesJar, javadocJar
111-
}
112110

113111
publishing {
114112
publications {
@@ -146,4 +144,4 @@ bintray {
146144
released = new Date()
147145
}
148146
}
149-
}
147+
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
*/
1313
@SuppressWarnings("WeakerAccess")
1414
public interface Query {
15+
16+
/**
17+
* @return Fields to be loaded
18+
*/
19+
String [] getFields();
20+
21+
/**
22+
* @return the number of records per page
23+
*/
24+
Integer getPageSize();
1525

1626
/**
1727
* @return number of max rows to load.

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

Lines changed: 110 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
*
3939
* @since 0.1
4040
*/
41-
class Table<T> {
41+
public class Table<T> {
4242

4343
private static final Logger LOG = LoggerFactory.getLogger( Table.class );
4444

@@ -78,6 +78,8 @@ public void setParent(Base parent) {
7878
}
7979

8080
/**
81+
*
82+
* If no Parameter ser all querys to null.
8183
*
8284
* @return
8385
* @throws AirtableException
@@ -103,17 +105,26 @@ public List<Sort> getSort() {
103105
public String filterByFormula() {
104106
return null;
105107
}
108+
109+
@Override
110+
public String[] getFields() {
111+
return null;
112+
}
113+
114+
@Override
115+
public Integer getPageSize() {
116+
return null;
117+
}
106118
});
107119

108120
}
109121

110122
/**
111-
* Select List of data of table.
123+
* Select List of data of table with defined Query Parameters.
112124
*
113125
* @param query
114126
* @return
115127
* @throws AirtableException
116-
* @throws HttpResponseException
117128
*/
118129
@SuppressWarnings("WeakerAccess")
119130
public List<T> select(Query query) throws AirtableException {
@@ -122,6 +133,12 @@ public List<T> select(Query query) throws AirtableException {
122133
GetRequest request = Unirest.get(getTableEndpointUrl())
123134
.header("accept", "application/json")
124135
.header("Authorization", getBearerToken());
136+
if(query.getFields() != null && query.getFields().length > 0){
137+
String[] fields = query.getFields();
138+
for (int i = 0; i < fields.length; i++) {
139+
request.queryString("fields[]",fields[i]);
140+
}
141+
}
125142
if(query.getMaxRecords() != null) {
126143
request.queryString("maxRecords", query.getMaxRecords());
127144
}
@@ -131,6 +148,13 @@ public List<T> select(Query query) throws AirtableException {
131148
if(query.filterByFormula() != null) {
132149
request.queryString("filterByFormula", query.filterByFormula());
133150
}
151+
if(query.getPageSize() != null){
152+
if (query.getPageSize() > 100) {
153+
request.queryString("pageSize",100);
154+
} else {
155+
request.queryString("pageSize",query.getPageSize());
156+
}
157+
}
134158
if(query.getSort() != null) {
135159
int i = 0;
136160
for (Sort sort : query.getSort()) {
@@ -158,7 +182,14 @@ public List<T> select(Query query) throws AirtableException {
158182
return list;
159183
}
160184

161-
185+
/**
186+
* select with Parameter maxRecords
187+
*
188+
* @param maxRecords
189+
* @return
190+
* @throws AirtableException
191+
* @throws HttpResponseException
192+
*/
162193
public List<T> select(Integer maxRecords) throws AirtableException, HttpResponseException {
163194
return select(new Query() {
164195
@Override
@@ -180,6 +211,16 @@ public List<Sort> getSort() {
180211
public String filterByFormula() {
181212
return null;
182213
}
214+
215+
@Override
216+
public String[] getFields() {
217+
return null;
218+
}
219+
220+
@Override
221+
public Integer getPageSize() {
222+
return null;
223+
}
183224
});
184225
}
185226

@@ -211,11 +252,22 @@ public List<Sort> getSort() {
211252
public String filterByFormula() {
212253
return null;
213254
}
255+
256+
@Override
257+
public String[] getFields() {
258+
return null;
259+
}
260+
261+
@Override
262+
public Integer getPageSize() {
263+
return null;
264+
}
214265
});
215266
}
216267

217268
/**
218-
*
269+
*select Table data with defined sortation
270+
*
219271
* @param sortation
220272
* @return
221273
* @throws AirtableException
@@ -245,6 +297,59 @@ public List<Sort> getSort() {
245297
public String filterByFormula() {
246298
return null;
247299
}
300+
301+
@Override
302+
public String[] getFields() {
303+
return null;
304+
}
305+
306+
@Override
307+
public Integer getPageSize() {
308+
return null;
309+
}
310+
});
311+
}
312+
313+
/**
314+
* select only Table data with defined Fields
315+
*
316+
* @param fields
317+
* @return
318+
* @throws AirtableException
319+
* @throws HttpResponseException
320+
*/
321+
public List<T> select(String[] fields) throws AirtableException, HttpResponseException {
322+
323+
return select(new Query() {
324+
@Override
325+
public Integer getMaxRecords() {
326+
return null;
327+
}
328+
329+
@Override
330+
public String getView() {
331+
return null;
332+
}
333+
334+
@Override
335+
public List<Sort> getSort() {
336+
return null;
337+
}
338+
339+
@Override
340+
public String filterByFormula() {
341+
return null;
342+
}
343+
344+
@Override
345+
public String[] getFields() {
346+
return fields;
347+
}
348+
349+
@Override
350+
public Integer getPageSize() {
351+
return null;
352+
}
248353
});
249354
}
250355

0 commit comments

Comments
 (0)