Created
September 2, 2022 16:43
-
-
Save krummja/fc17dffbc5598d4f03185ff913157fe5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def inspect_companies( | |
start: int = 0, | |
stop: int = 100 | |
) -> Generator[Dict[str, Any]]: | |
"""Generator function to page through Hubspot contacts. | |
: param int start : The start page of the | |
: param int stop : | |
""" | |
if start > stop: | |
raise Exception(f"Expected start value less than stop. " + | |
f"Got {start} > {stop}") | |
url = "https://api.hubapi.com/crm/v3/objects/companies/search?" | |
url_params = {'hapikey': API_KEY} | |
headers = {'Content-Type': 'application/json'} | |
payload = {'properties': [], 'after': f"{start}"} | |
total = 0 | |
pages = 0 | |
while True: | |
parameters = urlencode(url_params) | |
post_url = url + parameters | |
r = requests.post(url=post_url, headers=headers, data=json.dumps(payload)) | |
response = r.json() | |
total += len(response['results']) | |
pages += 10 | |
yield response | |
if total >= stop or response.get('paging') is None: | |
final_count = min(start + pages, stop) | |
print(f"{total} records from pages {start} to {final_count}.") | |
break | |
after = int(response['paging']['next']['after']) | |
payload['after'] = str(after) | |
results = [c for c in inspect_companies(20, 40)] | |
pprint(results) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment