Skip to content

Instantly share code, notes, and snippets.

@krummja
Created April 5, 2022 20:09
Show Gist options
  • Save krummja/1ff7ad915d64a262d57de0cb8ca8dcb4 to your computer and use it in GitHub Desktop.
Save krummja/1ff7ad915d64a262d57de0cb8ca8dcb4 to your computer and use it in GitHub Desktop.
hubspot company request loop
def inspect_companies(
start: int = 0,
stop: int = 500
) -> Generator[Dict[str, Any]]:
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)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment