Sample Python code to poll a collection

from rockset import Client
import rockset, os, sys
from dotenv import load_dotenv
from waiter import wait

collection_name = "yourCollectionName"

#get rocksetapikey
ROCKSETAPIKEY = os.environ.get('ROCKSETAPIKEY')

# connect securely to Rockset production API servers
rs = Client(api_server='api.rs2.usw2.rockset.com', api_key=ROCKSETAPIKEY)

def create_collection():
  """create sample collection"""

  # create collection
  collection_created = rs.Collection.create(collection_name)

  # poll the collection until it's the status has changed from CREATED to READY
  waits = wait(1) * 2

  for attempt in waits[:20]:
    describe_collection = collection_created.describe()
    check_status = describe_collection["data"]["status"]
    print(check_status)
    if check_status == 'READY':
        break
  return True

def drop_collection():
  """ drop sample collection"""

  # check to see if the collection exists
  try:
    get_collection = rs.Collection.retrieve(collection_name)
    print(get_collection)
  except rockset.InputError:
    print("collection doesn't exist")
    return False

  # if collection exists, drop it & poll to see when it's finally dropped
  get_collection.drop()
  print ("dropping the collection...")

  # let's poll to see when the collection has dropped
  waits = wait(1) * 2
  for attempt in waits[:20]:
    try:
      collection_exists = rs.Collection.retrieve(collection_name)
      print(collection_exists)
    except rockset.InputError:
      print("collection has successfully dropped")
      break
  return True


def insert_data():
  """insert data into a collection"""
  print("inserting docs")

  doc = [{
    "id":"t1", "price": "$125.00 ",  "latitude": "37.78097",  "longitude": "-122.40305"
    }]

  rs.Collection.add_docs(collection_name, doc)

def main():
  """ main function """

  # check to see if the collection exists,
  # if it does, drop, re-create it, add docs to collecion
  # if collection doesn't exists, just create it and add docs to it

  try:
    dropped = drop_collection()
    if not dropped:
      print("collection doesn't exist")
    created = create_collection()

    if not created:
      print("unable to create collection")

    insert_data()
  except:
      print("got an unexpected error: ", sys.exc_info()[0])
      raise
  print("yay!")

if __name__ == "__main__":
  main()