用于neverbunce api的官方python sdk
neverbounce-sdk的Python项目详细描述
欢迎使用Neverbunce的Python SDK!我们希望它能帮助你消费 我们的服务。请向github问题跟踪程序报告任何错误,并确保 使用前请阅读文档。
安装
首选的安装方法是在虚拟环境中使用pip:
pip install neverbounce_sdk
如果必须使用easy_install,则可以。如果您想安装本地 开发:
git clone git@github.com:NeverBounce/NeverBounceApi-Python.git cd NeverBounceApi-Python pip install -e .
这将在可编辑的活动环境中安装neverbounce_sdk。 模式。
用法
The API username and secret key used to authenticate V3 API requests will not work to authenticate V4 API requests. If you are attempting to authenticate your request with the 8 character username or 12-16 character secret key the request will return anauth_failure
error. The API key used for the V4 API will look like the following:secret_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
. To create new V4 API credentials please go [here](https://app.neverbounce.com/apps/custom-integration/new).
neverbunce python sdk提供了一个简单的接口,通过它可以进行交互 使用Neverbunce的电子邮件验证API版本4。起来跑步,确保 您手头有API令牌:
import neverbounce_sdk api_key = 'secret_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' client = neverbounce_sdk.client(api_key=api_key)
现在你可以使用客户端了。你可以检查你的帐户 信息:
info = client.account_info() # info is {'billing_type': 'default', 'credits': 12345, ... }
您可以验证单个电子邮件:
resp = client.single_check('test@example.com') resp['result'] # 'invalid' resp['execution_time'] # 285
您可以创建、查询和控制电子邮件验证批量 工作:
emails = [ {'email': 'tomato@veggies.com'}, # must have an 'email' key {'email': 'cucumber@veggies.com', 'best_when': 'cold'}, # may contain "metadata" ] job = client.jobs_create(emails) # all state-changing methods return a status object resp = client.jobs_parse(job['id'], auto_start=False) assert resp['status'] == 'success' client.jobs_start(job['id']) progress = client.jobs_status(job['id']) print(progress) # dict with keys 'job_status', 'started', 'percent_complete', etc
创建作业时,可以附加“元数据”作为附加键 到作业输入列表中包含的每个对象(pythondict)。请注意 这些额外的键将是broadcasted;即结果集的每一行 如果密钥的值是 未在输入中指定,它将是作业处理的 输出。
所有api操作都返回字典,其中包含有关 手术或手术结果,以较合适者为准。 唯一的例外是client.search和client.results函数。 这些api端点生成的响应被分页;因此 函数返回自定义迭代器,允许您在api的 分页:
all_my_jobs = client.jobs_search() type(all_my_jobs) # neverbounce_sdk.bulk.ResultIter for job in all_my_jobs: # process job # this loop will make API calls behind the scenes, so be careful! if all_my_jobs.page > 10: break
ResultIter会在幕后拉下页面,所以要小心!一个 ResultIter将原始api响应公开为一个data属性, 当前页码为page,总页数为total_pages, 所以您可以使用这些属性来实现对结果的细粒度控制 迭代。另外,方法raw_search和raw_results的 客户端对象将返回原始api响应(这与^{tt9}相同$ ResultIter对象的属性)。
客户机在幕后使用requests,如果您愿意 显式地提供requests.Session,您可以这样做:
from requests import Session api_key = 'secret_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' session = Session() client = neverbounce_sdk.client(api_key=api_key, session=session)
所有传出的http请求都将通过会话对象的 request方法,利用requests.Session的连接池。 您可以提供任何提供request接口的自定义对象 与requests.Session和close方法提供的签名相同。
最后,客户端可以使用上下文管理器。如果提供了会话, 它将用于with块中的所有连接;否则,会话将 被创造。不管怎样,与客户端关联的会话总是always 在上下文块的末尾关闭。
with neverbounce_sdk.client() as client: client.api_key = 'secret_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' # the client creates a session behind the scenes assert client.session is not None # do other stuff with the client # and then removes it at the end of the block assert client.session is None
另请参见
客户机对象的每个功能的文档可以通过 python的内置help函数,例如:
>>> help(client.create) # brings up a ton of information about the create ... # function's arguments and options
客户端对象的函数的许多输入和输出相当
接近neverbunce的原始v4 api,阅读official API
docs<https://developers.neverbounce.com/v4.0/reference#account>
与使用内置联机帮助结合使用时很有价值。