I am using Airtable to get some records and I have issues getting all the records I have.
Airtable limits you to 100 records per API call, so if the records are more than 100, they give you an offset field in the results.
What I need to do is to call the API, check if the offset is there, if it's the case, make another call using this offset, and at the end, get all the data and merge them into one single JSON dictionnary.
No matter what I do, I don't get the objects I need, so maybe I have an issue in my code, that I couldn't notice:
Here is the function to make the call:
def get_all(table):
url = f"{endpoint}/{table}"
r = requests.get(url = url, headers = headers)
return r.json()
And here is the function to make the call using an offset:
def get_all_w_offset(table, offset):
url = f"{endpoint}/{table}"
r = requests.get(url = url, headers = headers, params={'offset': offset})
return r.json()
And this is the function that needs to collect all objects from the API:
def get_all_advances():
rsp = get_all(REGISTRE_TBL) # get all registry items
data = json.loads(json.dumps(rsp)) # convert our items into json format
while True:
data = json.loads(json.dumps(rsp))
records = data.get("records", [])
offset = data.get("offset")
if not offset:
return records
else:
rsp = get_all_w_offset(REGISTRE_TBL, offset)
data = json.loads(json.dumps(rsp))
But I do not get all the objects I have on Airtable.
I use a similar way in another function and it works with no issues, I copied the same code and did not work, that is why I changed it and still, does not work. Let me show you the function that actually works:
def validateUser(cin, tphone):
data = get_all(EMPLOYEES_TBL)
data = json.loads(json.dumps(data))
cin = cin.upper()
try:
offset = data["offset"]
except KeyError:
offset = None
while offset:
for i in data["records"]:
try:
if tphone == i["fields"]["Téléphone"] and cin == i["fields"]["CIN"]:
return i
else:
continue
except KeyError:
continue
data = get_all_w_offset(EMPLOYEES_TBL, offset)
return False