Skip to content

Instantly share code, notes, and snippets.

@schroederapps
Created June 28, 2018 21:54
Show Gist options
  • Save schroederapps/87dd50b2f99f8e915723a16ce33ffba9 to your computer and use it in GitHub Desktop.
Save schroederapps/87dd50b2f99f8e915723a16ce33ffba9 to your computer and use it in GitHub Desktop.
Corona AirTable Fetch Module - converts the contents of an AirTable base into a Lua table
local _M = {}
local json = require 'json'
local base_id = 'foo'
local api_key = 'bar'
local parse_values, get_table
local table_index = 1
local offset = ''
local data = {}
local tables = {
'list',
'of',
'your',
'airtable',
'table',
'names',
}
function parse_values(event)
if event.isError or event.response == nil then return end
local tbl_name = tables[table_index]
data[tbl_name] = data[tbl_name] or {}
local tbl = data[tbl_name]
tbl.fields = tbl.fields or {}
tbl._keys = tbl._keys or {}
local response = json.decode(event.response)
for i = 1, #response.records do
local record = response.records[i]
tbl[record.id] = record.fields
tbl._keys[#tbl._keys + 1] = record.id
end
offset = response.offset or ''
if offset == '' then
table_index = table_index + 1
end
timer.performWithDelay(300, get_table)
end
function get_table()
if table_index > #tables then
print(json.prettify(data))
_M.listener(data)
return true
end
local url = 'https://api.airtable.com/v0/' .. base_id .. '/' .. tables[table_index] .. '?offset=' .. offset
local params = {
headers = {
['Authorization'] = 'Bearer ' .. api_key,
},
}
network.request(url, 'GET', parse_values, params)
end
function _M.pull(listener)
_M.listener = listener or function() end
data = {}
table_index = 1
offset = ''
get_table()
end
return _M
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment