nowkey
DataApi 平台的官方 Python SDK。
用一组简单的 Python 方法调用 DataApi 数据集:查询、新增、修改、删除、聚合统计,以及增量同步。鉴权、签名、分页与重试都由 SDK 处理,你只需要关注业务字段。
- 签名鉴权(AccessKey + Secret,HMAC-SHA256),凭证不随请求明文传输
- 查询结果保持服务端原始结构,分页、超时、重试统一封装
- 支持标签化过滤、聚合求和、逐页迭代与增量拉取
- 兼容 Python 3.8+,仅依赖
requests
安装
pip install nowkey
快速开始
1. 获取凭证
在 DataApi 控制台创建数据集访问凭证(AccessKey / Secret)。Secret 只在创建时展示一次,请妥善保存,不要提交到代码仓库。
2. 配置凭证
推荐通过环境变量注入:
export DATAAPI_BASE_URL=https://your-dataapi-host
export DATAAPI_ACCESS_KEY=sk_xxxxxxxxxxxxxxxx
export DATAAPI_SECRET=your-secret
也可以显式传参:
from nowkey import DataApi
api = DataApi(
base_url='https://your-dataapi-host',
access_key='sk_xxxxxxxxxxxxxxxx',
secret='your-secret',
)
3. 调用
from nowkey import DataApi
with DataApi() as api:
# 查询:默认返回服务端结构 {'code', 'msg', 'data'}
res = api.get_list('orders', size=20, state=1)
if res['code'] == 0:
for row in res['data']:
print(row['id'], row.get('amount'))
# 计数
total = api.count('orders', state=1)
# 新增
api.add('orders', order_no='A20261125001', amount=99.5, state=1)
# 修改(必须带 id)
api.update('orders', id=1001, state=2)
# 删除
api.remove('orders', id=1001)
方法一览
| 方法 | 说明 |
|---|---|
get_list(dataset, **filters) |
分页查询,返回 {code, msg, data, total, size, p} |
get_info(dataset, id) |
按主键查单条 |
count(dataset, **filters) |
条件计数 |
add(dataset, **fields) |
新增一条,返回自增 id |
update(dataset, id=..., **fields) |
按 id 更新 |
remove(dataset, id=...) |
按 id 删除 |
add_or_update(dataset, **fields) |
有 id 且记录存在则更新,否则新增 |
add_or_remove(dataset, where, **fields) |
where 命中则删除,否则新增 |
iter_list(dataset, size=200, **filters) |
逐页拉取生成器 |
func(dataset, **param) |
调用自定义函数 |
aggregate(dataset, **param) / sum / inc / query |
聚合统计 |
pull(dataset, **param) / iter_pull(...) |
增量拉取(需开放网关 API Token) |
所有方法返回服务端原始结构 {'code', 'msg', 'data'};code == 0 表示成功。网络异常在重试耗尽后返回 code 17。
增量同步
适合把数据集按时间增量同步到本地库。增量拉取走开放网关接口,需要控制台签发的 API Token:
api = DataApi(base_url='https://your-dataapi-host', api_token='<api_token>')
for row in api.iter_pull('orders', mode='updatetime', since='2026-11-01 00:00:00'):
# at-least-once:被并发更新的行可能重复投递,请按主键幂等写入
upsert(row)
返回值与错误码
| code | 含义 |
|---|---|
| 0 | 成功 |
| 1 | 参数错误 |
| 3 | 数据集或记录不存在 |
| 5 | 字段校验失败 |
| 13 | 未授权 / 凭证无效 |
| 14 | 签名校验失败(含时间戳过期、nonce 重放) |
| 17 | 网络异常,重试耗尽 |
更多
- 接口契约与完整字段说明见项目仓库
docs/sdk.md。 - 可运行示例见仓库
sdk/python/examples/quickstart.py。
开发
pip install -e .
python -m unittest discover -s tests -v
Metadata
Release files for nowkey 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| nowkey-0.2.1.tar.gz | 8.7 kB | Without provenance | Details for nowkey-0.2.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| nowkey-0.2.1-py3-none-any.whl | Python 3 | none | any | Details for nowkey-0.2.1-py3-none-any.whl |
Total release size: 15.1 kB
Release files / nowkey-0.2.1.tar.gz
| Download URL | nowkey-0.2.1.tar.gz |
|---|---|
| Size | 8.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b2b01be08cee6e36727965303380c3cb4f02811a23cb2b501dd8b0f4f37727ed
|
|
BLAKE2b-256 checksum How to use checksums |
d62b60236f8b99fe67e88714083848a7f4af1ccf7a41ec5135864597cf616181
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.7
|
Release files / nowkey-0.2.1-py3-none-any.whl
| Download URL | nowkey-0.2.1-py3-none-any.whl |
|---|---|
| Size | 6.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
28bea09075444a9e19cb624aa845d6a7668eefed54a9ec4c98dc413b4f7c39f4
|
|
BLAKE2b-256 checksum How to use checksums |
dd876e9a13e2da707d0728d772e51e2d5d8be2da662dc13e2124ad4bd8d2d041
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.7
|
Security for nowkey 0.2.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.