From acee499d7d56d21936e5d47902ade8559abbbd32 Mon Sep 17 00:00:00 2001 From: Timothy Li Date: Thu, 21 Nov 2019 12:32:03 +0800 Subject: [PATCH 1/2] Fix get recent hubspot companies to use since param --- hubspot3/companies.py | 52 ++++++++++++++++++++++++++++++++++--------- hubspot3/deals.py | 8 ++++--- 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/hubspot3/companies.py b/hubspot3/companies.py index 7dfda83..b32a56b 100644 --- a/hubspot3/companies.py +++ b/hubspot3/companies.py @@ -82,7 +82,7 @@ def search_domain( def get_all( self, prettify_output: bool = True, - extra_properties: Union[str, List] = None, + extra_properties: Union[str, List]=None, **options ) -> Optional[List]: """get all companies, including extra properties if they are passed in""" @@ -137,7 +137,13 @@ def get_all( return output - def _get_recent(self, recency_type: str, **options) -> Optional[List]: + def _get_recent( + self, + recency_type: str, + limit: int = 250, + offset: int = 0, + since: int = None, + **options) -> Optional[List]: """ Returns either list of recently modified companies or recently created companies, depending on recency_type passed in. Both API endpoints take identical parameters @@ -148,15 +154,19 @@ def _get_recent(self, recency_type: str, **options) -> Optional[List]: """ finished = False output = [] - offset = 0 - query_limit = 250 # Max value according to docs while not finished: + params = { + "count": query_limit, + "offset": offset, + } + if since: + params["since"] = since batch = self._call( "companies/recent/{}".format(recency_type), method="GET", doseq=True, - params={"count": query_limit, "offset": offset}, + params=params, **options ) output.extend( @@ -171,11 +181,33 @@ def _get_recent(self, recency_type: str, **options) -> Optional[List]: return output - def get_recently_modified(self, **options) -> Optional[List]: - return self._get_recent("modified", **options) - - def get_recently_created(self, **options) -> Optional[List]: - return self._get_recent("created", **options) + def get_recently_modified( + self, + limit: int = 250, + offset: int = 0, + since: int = None, + **options + ) -> Optional[List]: + return self._get_recent( + "modified", + limit=limit, + offset=offset, + since=since, + **options) + + def get_recently_created( + self, + limit: int = 250, + offset: int = 0, + since: int = None, + **options + ) -> Optional[List]: + return self._get_recent( + "created", + limit=limit, + offset=offset, + since=since, + **options) def get_contacts_at_a_company(self, company_id: str, **options) -> Optional[List]: """ diff --git a/hubspot3/deals.py b/hubspot3/deals.py index 325b2d8..73634a6 100644 --- a/hubspot3/deals.py +++ b/hubspot3/deals.py @@ -55,7 +55,8 @@ def delete(self, deal_id: str, **options) -> Dict: def associate(self, deal_id, object_type, object_ids, **options): # Encoding the query string here since HubSpot is expecting the "id" parameter to be - # repeated for each object ID, which is not a standard practice and won't work otherwise. + # repeated for each object ID, which is not a standard practice and + # won't work otherwise. object_ids = [("id", object_id) for object_id in object_ids] query = urllib.parse.urlencode(object_ids) @@ -69,7 +70,7 @@ def associate(self, deal_id, object_type, object_ids, **options): def get_all( self, offset: int = 0, - extra_properties: Union[list, str] = None, + extra_properties: Union[list, str]=None, limit: int = -1, **options ): @@ -126,7 +127,8 @@ def get_all( if not deal["isDeleted"] ] ) - finished = not batch["hasMore"] or (limited and len(output) >= limit) + finished = not batch["hasMore"] or ( + limited and len(output) >= limit) offset = batch["offset"] return output if not limited else output[:limit] From 481ace14df14d94d6befd831ab7b2f2450c90213 Mon Sep 17 00:00:00 2001 From: Timothy Li Date: Thu, 21 Nov 2019 12:57:21 +0800 Subject: [PATCH 2/2] Fix typo --- hubspot3/companies.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hubspot3/companies.py b/hubspot3/companies.py index b32a56b..80b2637 100644 --- a/hubspot3/companies.py +++ b/hubspot3/companies.py @@ -157,7 +157,7 @@ def _get_recent( while not finished: params = { - "count": query_limit, + "count": limit, "offset": offset, } if since: