Airtable.java
Java API for Airtable (http://www.airtable.com)
Install / Use
/learn @Sybit-Education/Airtable.javaREADME
⚠️ The project is no longer maintained, as we now use NocoDB for our educational projects. ⚠️
Airtable.java - The Java API for Airtable
This is a Java API client for Airtable (http://www.airtable.com).
The Airtable API provides a simple way of accessing data within Java projects.
More information about the Airtable API could be found at https://airtable.com/api. The documentation will provide detailed information about your created base.
Usage
Gradle
For Gradle add compile com.sybit:airtable.java:[version] to compile dependencies.
Also add repository to dependencies:
repositories {
maven {
url = uri("https://maven.pkg.github.com/Sybit-Education/airtable.java")
...
}
}
Initializing
It is required to initialize the Java API before it is used. At leased you have to pass your API-Key to get access to Airtable:
Airtable airtable = new Airtable().configure();
Personal Access Token
The personal access token could be passed to the app in different ways:
- Defining Java property
AIRTABLE_TOKEN(e.g.-DAIRTABLE_TOKEN=foo). - Defining OS environment variable
AIRTABLE_TOKEN(e.g.export AIRTABLE_TOKEN=foo). - Defining property file
credentials.propertiesin root classpath containing key/valueAIRTABLE_TOKEN=foo. - On the other hand the token could also be added by using the method
Airtable.configure(String token).
How to get Personal Access Token
See: https://support.airtable.com//docs/creating-personal-access-tokens-
Proxy Support
The API supports environment variable http_proxy. If the variable is set, it is used automatically.
- On Windows:
set http_proxy=http://your_proxy:your_port - On Unix/OS X:
export http_proxy=http://your_proxy:your_port
If endpointUrl contains localhost or 127.0.0.1 proxy settings are ignored automatically.
Logging
The Simple Logging Facade for Java SLF4J serves as a simple facade or abstraction for various logging frameworks (e.g. java.util.logging, logback, log4j) allowing the end user to plug in the desired logging framework at deployment time.
Request Limits
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 need to wait 30 seconds before subsequent requests will succeed.
Connecting to Airtable
To use this libraray you will need an Airtable object. Simply create one: Airtable airtable = new Airtable();.
This object needs an API-Key or it won't work properly so airtable.configure(AIRTABLE_API_KEY);.
Now the Airtable object needs to know which base you want to access. This method will return a Base object which will be used in the future:
Base base = airtable.base(AIRTABLE_BASE);
With the Base object you can perform all kind of operations see more at CRUD Operations on Table Records.
Object Mapping
The Java implementation of the Airtable API provides automatic object mapping. You can map any table to your own Java classes. But first you need to specify those classes.
Create a Object
The Java objects represent records or 'values' in Airtable. So the class attributes need to be adjusted to the Airtable Base.
Example
In Airtable we got a table 'Actor'. The columns represent the class attributes.
This is how our 'Actor' table looks like:
| Index | Name | Photo | Biography | Filmography | | :---: | :-----------: | :---------: | :-------: | :--------------------------: | | 1 | Marlon Brando | Some Photos | Long Text | Reference to the Movie Table | | 2 | Bill Murray | Some Photos | Long Text | Reference to the Movie Table | | 3 | Al Pacino | Some Photos | Long Text | Reference to the Movie Table | | ... | ... | ... | ... | ... |
Now our Java class should look like this:
public class Actor {
private String id;
@SerializedName("Name")
private String name;
@SerializedName("Photo")
private List<Attachment> photo;
@SerializedName("Biography")
private String biography;
@SerializedName("Filmography")
private String[] filmography;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getFilmography() {
return filmography;
}
public void setFilmography(String[] filmography) {
this.filmography = filmography;
}
public List<Attachment> getPhoto() {
return photo;
}
public void setPhoto(List<Attachment> photo) {
this.photo = photo;
}
public String getBiography() {
return biography;
}
public void setBiography(String biography) {
this.biography = biography;
}
}
For each column we give the Java class an attribute with the column name (Be careful! See more about naming in the Section Annotations)
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,
String Array for references on other Tables or Attachment for attached photos and files.
Now we got everything we need to create our first Airtable table object.
We use the Java class we just wrote to specify what kind of Object should be saved in our table. Then we tell our base-object which table we want to access.
All the records saved in our Airtable Base now should be in our local Table<JAVA CLASS> Object.
Example:
Base base = airtable.base('AIRTABLE_API_KEY');
Table<JAVA CLASS> actorTable = base.table("NAME OF THE TABLE", <JAVA_CLASS>);
//Example with the Actor Table
Table<Actor> actorTable = base.table("Actors", Actor.class);
Basic Objects
The Java implementation of the Airtable-API provides an implementation of basic Airtable objects such as attachments and thumbnails.
Photos and attached files in Airtable are retrieved as Attachments. Photos furthermore contain Thumbnail-Objects for different sizes.
Attachment
All the Attachment-objects got the following attributes:
- String
id - String
url - String
filename - Float
size - String
type
Photos additionally have:
- Map<String,Thumbnail>
thumbnails
Thumbnails
A Thumbnail is generated for image files in Airtable. Thumbnails are bound to an Attachment-object as a key/value Map.
The keys are small and large for the different sizes. The value is a Thumbnail-object.
A Thumbnail-object got the following Attributes:
- String
name - String
url - Float
width - Float
height
Note: The name of a Thumbnail Object is identical with it´s key ( small or large ).
Annotations
Use the annotation @SerializedName of Gson to annotate column names containing -, empty characters or other not in Java mappable characters.
The airtable.java API will respect these mappings automatically.
Example
import com.google.gson.annotations.SerializedName;
//Column in Airtable is named "First- & Lastname", which is mapped to field "name".
@SerializedName("First- & Lastname")
private String name;
Sort
With the integrated Sort element you can retrieve a list of sorted objects that specifies how the records will be ordered.
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".
The default direction is "asc".
For example, to sort records by Name, pass in:
Sort sort = new Sort("Name", Sort.Direction.desc);
List<Movie> listMovies = movieTable.select(sort);
If you set the view parameter, the returned records in that view will be sorted by these fields.
Detailed example see TableParameterTest
CRUD-Operations on Table Records
Select
Select list of items from table:
table(name).select(): get all records of tablenametable(name).select(Integer maxRecords): get maxmaxRecordsrecords of tablenametable(name).select(String[] fields): get records of tablenamewith only the specifiedfieldstable(name).select(String view): get records of tablenamewith the specifiedview(more about views)table(name).select(Sort sortation): get records of tablenameusingsortto sort records (More about Sort here)table(name).select(Query query): get records of tablenameusingqueryto filter
Example
Base base = new Airtable().base("AIRTABLE_BASE");
List<Movie> retval = base.table("Movies", Movie.class).select();
Detailed example see TableSelectTest.java
API Result Limitation
The REST-API of Airtable is limited to return max. 100 records. If the select has more than 100 records in result an offest is added to
returned data. The Airtable.java client will solve this and tries to load the offset data automatically.
Find
Use find to get specific records of table:
table(name).find(String id): get record withidof tablename
Example
Base base = new Airtable().base("AIRTABLE_BASE");
Table<Actor> actorTable = base.table("Actors", Actor.class);
Actor actor = actorTable.find("rec514228ed76ced1");
Detailed example see [TableFindTest.java](https
