APIs & Integrations

adimoglis
メンバー

Deals API 'hasMore' response (getting all deals)

解決

Hello all,

In API Documentation, I see that there are two 'variebles' in the response:

  1. hasMore
  2. Offset

In the deault code provided by the documentation, I removed the 'max_results' condition.

 

    import requests
    import json
    import urllib

    #max_results = 500
    hapikey = 'demo'
    limit = 250
    deal_list = []
    get_all_deals_url = "https://api.hubapi.com/deals/v1/deal/paged?"
    parameter_dict = {'hapikey': hapikey, 'limit': limit}
    headers = {}

    # Paginate your request using offset
    has_more = True
    while has_more:
        parameters = urllib.parse.urlencode(parameter_dict)
        get_url = get_all_deals_url + parameters
        r = requests.get(url= get_url, headers = headers)
        response_dict = json.loads(r.text)
        has_more = response_dict['hasMore']
        deal_list.extend(response_dict['deals'])
        parameter_dict['Offset']= response_dict['offset']
        print(response_dict['offset'])
        print(response_dict['hasMore'])


    print('loop finished')

    list_length = len(deal_list)

    print("You've succesfully parsed through {} deal records and added them to a list".format(list_length))

So, I expect the script to finish when 'has_more' attribute is False. But this will never happen, neither in the demo, nor in our company deals.

The API answers 'True' and the same 'Offset' for all iterations.

Is this the default behaviour?

0 いいね!
1件の承認済みベストアンサー
LeifInouye
解決策
元HubSpot社員
元HubSpot社員

Deals API 'hasMore' response (getting all deals)

解決

Hi adimoglis! 

 

I've figured out the issue. In your code, 

parameter_dict[Offset]

needs to be:

parameter_dict[offset]

(lowercase o)

This is a documentation error on my part, that I will now correct. Thank you for bringing it to my attention!

元の投稿で解決策を見る

2件の返信
LeifInouye
解決策
元HubSpot社員
元HubSpot社員

Deals API 'hasMore' response (getting all deals)

解決

Hi adimoglis! 

 

I've figured out the issue. In your code, 

parameter_dict[Offset]

needs to be:

parameter_dict[offset]

(lowercase o)

This is a documentation error on my part, that I will now correct. Thank you for bringing it to my attention!

LeifInouye
元HubSpot社員
元HubSpot社員

Deals API 'hasMore' response (getting all deals)

解決

Hi adimoglis,

 

Interesting-- the intended behavior of that script is to operate even when a `max-results` is omitted. Let me look into this -- I will reach back out shortly. 

0 いいね!