Suggestion: Add 'iter_all' as iterator version of 'get_all'
还没有人认领这个 Issue。
评估
调研方向
从 hubspot/utils/objects.py 中现有的 fetch_all 实现开始,并检查 issue 中描述的 get_all 和 do_search 入口点。当客户端提供一个内置迭代器,能够处理不同形式的分页参数,并且适用于列出的对象 API 和关联 API 时,即表示完成。
由索引模型根据 Issue 内容生成。
描述
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.
- 主要语言
- Python
- 星标
- 434
- 派生
- 126
- PR 合并指标
- 30 天内没有已合并 PR
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
HubSpot/hubspot-api-python 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 65/100
HubSpot/hubspot-api-python#497 ·
-
难度 3/5 1-2 天 新手友好度 58/100
HubSpot/hubspot-api-python#498 · 1 条评论 ·
-
难度 4/5 3-5 天 新手友好度 52/100
HubSpot/hubspot-api-python#496 ·
-
难度 4/5 3-5 天 新手友好度 35/100
HubSpot/hubspot-api-python#495 · 1 条评论 · 2 个 reaction ·
-
难度 4/5 3-5 天 新手友好度 52/100
HubSpot/hubspot-api-python#494 ·
查看 HubSpot/hubspot-api-python 的全部 Issue
相似的 Issue
-
bug confirmed issue
难度 2/5 1-3 小时 新手友好度 75/100
open-webui/open-webui#30750 · 1 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 75/100
-
enhancement
难度 2/5 1-3 小时 新手友好度 75/100
OpenwaterHealth/openmotion-bloodflow-app#604 · 1 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 70/100
-
good first issue
难度 1/5 1 小时以内 新手友好度 90/100