Turns out the reason this wasn’t working was because my URL encoding was wrong. Using a tip to json.dump
the constraint parameters, I’ve got this working. The Python3x
code is provided below for your enjoyment:
import pandas as pd
import json
import time
session = requests.Session()
#API KEY as defined in the settings tab, so that the script has full access to the data
API_KEY = 'YOUR_SECRET_KEY'
#base endpoint, see https://bubble.io/reference#API.get_api.Endpoint
base_url = 'https://YOU_APP.io/version-test/api/1.1/obj/YOUR_DATA_THING'
#Query initial parameters. We do not send a limit parameter (default is 100)
#we keep making calls till we have no remaining items to fetch
def get_data(constraints = False):
remaining = 1
cursor = 0
dfs = []
while remaining:
#data we send with the search. Search constraints would be here
headers = {"api_token": API_KEY, "cursor": str(cursor)}
if constraints:
payload = {"constraints": json.dumps(constraints)}
response = session.get(base_url, headers = headers, params=payload)
else:
response = session.get(base_url, headers = headers)
print(response.url)
if response.status_code != 200:
print('Error with status code {}'.format(response.status_code))
return "You're a sad boy"
chunk = response.json()['response']
remaining = chunk['remaining'] # Set remaining to check if should repeat next loop
dfs.append(pd.DataFrame(chunk['results']))
cursor += chunk['count']
time.sleep(0.1)
return pd.concat(dfs)
# search_options = [{"key": "quantity","constraint_type": "greater than","value": 2}]
search_options = [{'key': 'Created Date',
'constraint_type': 'greater than',
'value': '2020-01-26T04:32:14.906Z'}]
df = get_data(search_options)