Suggestion: Add 'iter_all' as iterator version of 'get_all'
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 38/100
Research direction
Start with the existing fetch_all implementation in hubspot/utils/objects.py and review the get_all and do_search entry points described in the issue. Done means the client provides a built-in iterator that handles the differing pagination argument shapes and works for the listed object and association APIs.
Written by the indexing model from the issue text.
Description
get_all reads all objects in memory before return. In most scenarios, an iterator can be a better fit.
My bypass solution:
This is based on fetch_all
PAGE_MAX_SIZE = 100
def iter_all(get_page_api_client, **kwargs):
after = kwargs.pop('after', None)
while True:
page = get_page_api_client.get_page(after=after, limit=PAGE_MAX_SIZE, **kwargs)
yield from page.results
if page.paging is None:
break
after = page.paging.next.after
It can be used like this
from itertools import islice
list(islice(iter_all(hs_client.crm.companies.basic_api, properties=['custom_field_1']), 2))
It can also be monkeypatched into suitable objects
import hubspot
def patch_iter_all():
def iter_all_for_crm(self, **kw):
yield from iter_all(self.basic_api, **kw)
hubspot.discovery.crm.tickets.discovery.Discovery.iter_all = iter_all_for_crm
hubspot.discovery.crm.products.discovery.Discovery.iter_all = iter_all_for_crm
hubspot.discovery.crm.quotes.discovery.Discovery.iter_all = iter_all_for_crm
hubspot.discovery.crm.contacts.discovery.Discovery.iter_all = iter_all_for_crm
hubspot.discovery.crm.deals.discovery.Discovery.iter_all = iter_all_for_crm
hubspot.discovery.crm.line_items.discovery.Discovery.iter_all = iter_all_for_crm
hubspot.discovery.crm.owners.discovery.Discovery.iter_all = iter_all_for_crm
hubspot.discovery.crm.companies.discovery.Discovery.iter_all = iter_all_for_crm
patch_iter_all()
Then the above example can simpler
list(islice(hs_client.crm.companies.iter_all(properties=['custom_field_1']), 2))
It would be nice if hubspot official api can have a 'iter_all' for every object that has 'get_all' function simply forward to fetch_all.
Just found there is another kind of 'get_all()' that returns forward paging object, like hs_client.crm.companies.associations_api.get_all() (where hs_client is the client object).
To iterate all items, it would be nice to have a common tool like this
def iter_from(f, **kwargs):
after = None
while True:
page = f(after=after, limit=100, **kwargs)
yield from page.results
if page.paging is None:
break
after = page.paging.next.after
Then it can be used as following
for item in iter_from(hs_client.crm.companies.associations_api.get_all, company_id='1234567', to_object_type='contact'):
# item is hubspot.crm.companies.models.associated_id.AssociatedId object
....
Edit: for functions like hs_client.crm.contacts.search_api.do_search the after and limit params are included in *Request object like PublicObjectSearchRequest.
Functions like hs_client.crm.companies.associations_api.get_all() have after and limit in function argument.
It would be nice if hubspot client can have a built-in pagination iterator.
- Dominant language
- Python
- Stars
- 434
- Forks
- 126
- PR merge metrics
- No merged PRs in 30d
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from HubSpot/hubspot-api-python
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
HubSpot/hubspot-api-python#497 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 58/100
HubSpot/hubspot-api-python#498 · 1 comment ·
-
Difficulty 4/5 3-5 days Newbie friendliness 52/100
HubSpot/hubspot-api-python#496 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
HubSpot/hubspot-api-python#495 · 1 comment · 2 reactions ·
-
Difficulty 4/5 3-5 days Newbie friendliness 52/100
HubSpot/hubspot-api-python#494 ·
All issues in HubSpot/hubspot-api-python
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
anthropics/skills#1811 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
speaches-ai/speaches#678 ·
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
datalayer/mcp-compose#42 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
conda-forge/spacy-feedstock#177 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
UKGovernmentBEIS/inspect_evals#2523 ·