2

我正在尝试使用 HubSpot CRM API 来获取“所有交易”。

API 端点为:https ://api.hubapi.com/deals/v1/deal/all?hapikey=demo

返回的 JSON 看起来像这样......

{
    "deals": [
        {
            "portalId": 62515,
            "dealId": 18039629,
            "isDeleted": false,
            "associations": {
                "associatedVids": [],
                "associatedCompanyIds": [],
                "associatedDealIds": []
            },
            "properties": {
                "dealname": {
                    "value": "Company",
                    "timestamp": 1457040864519,
                    "source": "API",
                    "sourceId": null
                },
                "amount": {
                    "value": "10",
                    "timestamp": 1457040864519,
                    "source": "API",
                    "sourceId": null
                },
                "closedate": {
                    "value": "",
                    "timestamp": 1457040864519,
                    "source": "API",
                    "sourceId": null
                },
                "hubspot_owner_id": {
                    "value": "11626092",
                    "timestamp": 1457046177648,
                    "source": "SALESFORCE",
                    "sourceId": null
                },
                "hs_lastmodifieddate": {
                    "value": "1457046177662",
                    "timestamp": 1457046177662,
                    "source": "CALCULATED",
                    "sourceId": null
                },
                "hubspot_owner_assigneddate": {
                    "value": "1457046177648",
                    "timestamp": 1457046177648,
                    "source": "SALESFORCE",
                    "sourceId": null
                },
                "num_associated_contacts": {
                    "value": "0",
                    "timestamp": 0,
                    "source": "CALCULATED",
                    "sourceId": null
                },
                "hs_createdate": {
                    "value": "1457040864535",
                    "timestamp": 1457040864535,
                    "source": null,
                    "sourceId": null
                },
                "createdate": {
                    "value": "1457040864535",
                    "timestamp": 1457040864535,
                    "source": null,
                    "sourceId": null
                },
                "hs_salesforceopportunityid": {
                    "value": "00628000007nRyuAAE",
                    "timestamp": 1457046177648,
                    "source": "SALESFORCE",
                    "sourceId": null
                }
            },
            "imports": []
        },
        {
            "portalId": 62515,
            "dealId": 18040854,
            "isDeleted": false,
            "associations": {
                "associatedVids": [],
                "associatedCompanyIds": [],
                "associatedDealIds": []
            },
            "properties": {
                "dealname": {
                    "value": "5678",
                    "timestamp": 1457042290572,
                    "source": "API",
                    "sourceId": null
                },
                "amount": {
                    "value": "750000.0",
                    "timestamp": 1457042290572,
                    "source": "API",
                    "sourceId": null
                },
                "closedate": {
                    "value": "",
                    "timestamp": 1457042290572,
                    "source": "API",
                    "sourceId": null
                },
                "hs_lastmodifieddate": {
                    "value": "1457042290592",
                    "timestamp": 1457042290592,
                    "source": "CALCULATED",
                    "sourceId": null
                },
                "num_associated_contacts": {
                    "value": "0",
                    "timestamp": 0,
                    "source": "CALCULATED",
                    "sourceId": null
                },
                "hs_createdate": {
                    "value": "1457042290592",
                    "timestamp": 1457042290592,
                    "source": null,
                    "sourceId": null
                },
                "createdate": {
                    "value": "1457042290592",
                    "timestamp": 1457042290592,
                    "source": null,
                    "sourceId": null
                }
            },
            "imports": []
        }
    ],
    "hasMore": true,
    "offset": 1467187
}

我知道如果hasMore==true,那么你应该抓住offset并将其包含在另一个 API 调用中,如下所示:https ://api.hubapi.com/deals/v1/deal/all?hapikey=demo&offset=1467187

然后继续这样做直到hasMore==false.

我正在使用以下代码从 API 中提取第一块 JSON:

import requests

url = "https://api.hubapi.com/deals/v1/deal/all"

querystring = {"hapikey":"demo"}

headers = {
    'cache-control': "no-cache"
    }

response = requests.request("GET", url, headers=headers, params=querystring)

print(response.text)

所以......我的问题是,现在我正在获取我的 JSON,我该如何:

1) 读取一大块 JSON
2) If hasMore==truethen go do #1 again
3) ElseIfhasMore==false然后将上面 #1 的所有迭代中的所有 JSON 组合成一个大 JSON
4) 从 #3 返回值

请问有什么帮助吗?

4

1 回答 1

4

工作解决方案

import json
import requests

url = "https://api.hubapi.com/deals/v1/deal/all"

querystring = {"hapikey":"demo"}

headers = {
    'cache-control': "no-cache"
    }

all_deals = []

response = requests.request("GET", url, headers=headers, params=querystring).json()

for deal in response['deals']:
    all_deals.append(deal)

hasMore = response['hasMore']
offset = response['offset']

while hasMore:

    querystring = {
        "hapikey":"demo",
        "offset":offset
        }
    response = requests.request("GET", url, headers=headers, params=querystring).json()

    for deal in response['deals']:
        all_deals.append(deal)

    hasMore = response['hasMore']
    offset = response['offset']

print(json.dumps(all_deals))
于 2016-03-12T02:23:15.717 回答