跳转到主要内容

HubSpot API的Python包装器,用于Python 3。最初基于hapipy构建,但进行了大量修改。

项目描述

PyPI version Code style: black Documentation Status Python 3.8+ supported

A python wrapper around HubSpot's APIs, for python 3.8+.

Built initially around hapipy, but heavily modified.

查看[此处](https://hubspot3.readthedocs.io/en/latest/)的文档! (感谢readthedocs)

快速入门

注意:我建议您考虑使用[官方HubSpot Python API](https://github.com/HubSpot/hubspot-api-python)。我不再在一家使用HubSpot的公司工作,并且这个库在许多API版本上落后。我将开放接受小的PR和可用性修复,但可能不会有进一步的重大工作

安装

# install hubspot3
pip install hubspot3

基本用法

from hubspot3 import Hubspot3

API_KEY = "your-api-key"

client = Hubspot3(api_key=API_KEY)

# all of the clients are accessible as attributes of the main Hubspot3 Client
contact = client.contacts.get_contact_by_email('testingapis@hubspot.com')
contact_id = contact['vid']

all_companies = client.companies.get_all()

# new usage limit functionality - keep track of your API calls
client.usage_limits
# <Hubspot3UsageLimits: 28937/1000000 (0.028937%) [reset in 22157s, cached for 299s]>

client.usage_limits.calls_remaining
# 971063

单个客户端

from hubspot3.companies import CompaniesClient

API_KEY = "your-api-key"

client = CompaniesClient(api_key=API_KEY)

for company in client.get_all():
    print(company)

传递参数

import json
from hubspot3.deals import DealsClient

deal_id = "12345"
API_KEY = "your_api_key"

deals_client = DealsClient(api_key=API_KEY)

params = {
    "includePropertyVersions": "true"
}  # Note values are camelCase as they appear in the Hubspot Documentation!

deal_data = deals_client.get(deal_id, params=params)
print(json.dumps(deal_data))

命令行界面

此外,还有一个命令行工具可用。通过

pip install hubspot3[cli]

安装该工具的额外要求,然后您可以使用它作为命令

hubspot3 --help

有关更多详细信息,请参阅Sphinx文档。

速率限制

请注意,这直接使用HubSpot API,因此您将受到HubSpot设置的所有指南的限制。

在编写本文时,HubSpot对API请求实施了以下限制

免费 & 初级

  • 每秒10个请求
  • 每天250,000个请求。

专业 & 企业

  • 每秒10个请求
  • 每天500,000个请求。

这个每日限制基于HubSpot账户的时间区设置在午夜重置。还有额外的附加组件可供购买以获取更多请求。

重试API调用

默认情况下,hubspot3会在失败时尝试重试所有API调用最多2次。

如果您想覆盖此行为,您可以将 number_retries 关键字参数添加到任何Client构造函数,或添加到单个API调用。

扩展BaseClient - 感谢 @Guysoft

一些API尚未完成!如果您想使用此仓库中尚未提供的API,可以扩展BaseClient类!

import json
from hubspot3.base import BaseClient


PIPELINES_API_VERSION = "1"


class PipelineClient(BaseClient):
    """
    Lets you extend to non-existing clients, this example extends pipelines
    """

    def __init__(self, *args, **kwargs):
        super(PipelineClient, self).__init__(*args, **kwargs)

    def get_pipelines(self, **options):
        params = {}

        return self._call("pipelines", method="GET", params=params)

    def _get_path(self, subpath):
        return f"deals/v{self.options.get('version') or PIPELINES_API_VERSION}/{subpath}"


if __name__ == "__main__":
    API_KEY = "your_api_key"
    a = PipelineClient(api_key=API_KEY)
    print(json.dumps(a.get_pipelines()))

高级oauth2令牌存储 - 感谢 @sangaline

这是一个示例,展示您如何使用客户端上的oauth2_token_getteroauth2_token_setter kwargs来使用自定义存储(在这种情况下为redis),以便多个客户端可以共享由oauth2请求生成的相同的访问/刷新令牌。

import aioredis
from hubspot3 import Hubspot3


redis_client = await aioredis.create_redis_pool(url, db=db, encoding='utf-8', timeout=10)

def oauth2_token_getter(token_type: str, client_id: str) -> str:
    loop = asyncio.get_event_loop()
    key = f'hubspot-oauth2-tokens:{token_type}:{client_id}'
    return loop.run_until_complete(redis_client.get(key))

def oauth2_token_setter(token_type: str, client_id: str, token: str) -> None:
    loop = asyncio.get_event_loop()
    key = f'hubspot-oauth2-tokens:{token_type}:{client_id}'
    # Token expiration is six hours, so match that when we store the tokens.
    # See: https://developers.hubspot.com/docs/methods/oauth2/refresh-access-token
    expire_in_seconds = 6 * 60 * 60
    loop.run_until_complete(redis_client.set(key, token, expire=expire_in_seconds))

# This client will share oauth2 credentials with other clients configured in the same way.
hubspot3_client = Hubspot3(
    access_token=access_token,
    client_id=client_id,
    client_secret=client_secret,
    refresh_token=refresh_token,
    oauth2_token_getter=oauth2_token_getter,
    oauth2_token_setter=oauth2_token_setter,
)

测试

我目前正在使用pytest重写许多测试,以便针对公共API运行并确保我们得到正确的mock数据类型。这些测试目前处于一个非常早期的状态 - 我将很快开始构建它们。

# Install required test packages
pip install pytest pytest-cov
# or
pip install -r requirements-dev.txt

# run tests
make
# or
make test_all

项目详情


发布历史 发布通知 | RSS源

下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分发

hubspot3-3.2.54.tar.gz (42.6 kB 查看哈希)

上传时间

构建分发

hubspot3-3.2.54-py3-none-any.whl (57.5 kB 查看哈希)

上传时间 Python 3

支持