import time


def all_restaurants(client, query):
    """
    Retrieve ALL the restaurants on Yelp for a given query.

    Args:
        query (string): Search term

    Returns:
        results (list): list of yelp.obj.business.Business objects
    """
    results = []
    offset = 0
    
    while True:
        res = client.search(query, **{
            'category_filter': 'restaurants',
            'limit': 20,
            'offset': offset
        })

        total = res.total

        if len(results) >= total:
            break

        new_records = res.businesses
        results += new_records
        offset += len(new_records)

        # yelp api restricts requests to 1000 results
        if offset >= 1000:
            break

        time.sleep(0.2)

    return results