cnb
非官方 Rust SDK,覆盖 CNB (Cloud Native Build) 平台 公开的 OpenAPI 全部 241 个端点(28 个资源 模块、271 个数据模型)。0.2.0 是破坏性升级——请阅读 CHANGELOG 中的迁移指南。
特性
- 🔐 Bearer Token 鉴权 +
CNB_TOKEN环境变量自动回退 - 🧱 全部端点类型化(请求 DTO + 响应 struct,仅 spec 缺失 schema 时降级为
serde_json::Value) - 🧰 查询参数 Builder(告别
Some(1), None, None, None, None…) - 💥 结构化错误:保留状态码、原始响应体、
code/message/request_id - 🔁 幂等方法的指数退避重试,自动尊重
429 Retry-After - ⚡ 完全异步(基于
tokio+reqwest 0.12),TLS 默认rustls - 📊 可选
tracing:记录每次调用的 method / path / status / 耗时(绝不打印 token) - 📈 可选
stream:通用分页 helper,把list_*端点变成Stream - 🚦 资源模块按 feature 拆分,按需开启
安装
[]
= "0.2"
= { = "1", = ["macros", "rt-multi-thread"] }
最小依赖(仅启用需要的资源):
[]
= { = "0.2", = false,
features = ["rustls-tls", "retry", "repositories", "issues"] }
快速开始
use ApiClient;
async
鉴权
| 优先级(高 → 低) | 方式 |
|---|---|
| 1 | ApiClient::builder().token("ghp_xxx").build()? |
| 2 | 环境变量 CNB_TOKEN (默认行为) |
| 3 | 匿名(ApiClient::builder().no_token().build()? 或环境无 token) |
let client = builder
.token // 显式 token,优先级最高
.timeout
.user_agent
.build?;
调用风格:类型化 + Builder
每个查询参数被打包成对应的 XxxQuery Builder,配合类型化响应:
use GetReposQuery;
let query = new
.page
.page_size;
let repos: =
client.repositories.get_repos.await?;
写请求体使用类型化 DTO:
use PostIssueForm;
let body = PostIssueForm ;
let issue = client.issues.create_issue.await?;
错误处理
use ;
match client.repositories.get_by_id.await
重试
默认对 GET/HEAD/PUT/DELETE/OPTIONS 的 5xx / 408 / 429 / 网络错误进行最多 3 次
指数退避重试,并尊重 Retry-After。需要自定义:
use ;
use Duration;
let client = builder
.retry
.build?;
分页(stream feature)
= { = "0.2", = ["stream"] }
use ;
use StreamExt;
let client = new?;
let repos = client.repositories;
let stream = paginate;
pin_mut!;
while let Some = stream.next.await
Tracing(tracing feature)
= { = "0.2", = ["tracing"] }
fmt
.with_env_filter
.init;
let client = new?;
let _ = client.users.get_user_info.await?;
// DEBUG cnb api call method=GET path=/user status=200 elapsed_ms=87
Token 永远不会出现在日志里——
Authorization头被标记为 sensitive。
Cargo Features
| Feature | 默认 | 说明 |
|---|---|---|
rustls-tls |
✓ | 使用 rustls 作为 TLS 后端(无需系统 OpenSSL) |
native-tls |
使用系统 TLS(OpenSSL / Secure Transport) | |
retry |
✓ | 启用自动重试逻辑 |
tracing |
启用 tracing instrumentation |
|
stream |
启用 pagination::paginate 与 Stream adaptors |
|
all-resources |
✓ | 启用所有资源模块 |
<resource> |
✓ | 单独启用某个资源(repositories、issues、git …) |
rustls-tls 与 native-tls 互斥,需要切换时设置 default-features = false。
资源模块速览
activities · ai · artifactory · assets · badge · build · charge ·
event · followers · git · git_settings · issues · knowledge_base ·
members · missions · organizations · pulls · rank · registries ·
releases · repo_code_issue · repo_contributor · repo_labels ·
repositories · search · security · starring · users · wiki ·
workspace
完整方法签名见 docs.rs/cnb。
示例
examples/ 目录提供 7 个可运行示例:
CNB_TOKEN=xxx
CNB_TOKEN=xxx
CNB_TOKEN=xxx
CNB_TOKEN=xxx
CNB_TOKEN=xxx
CNB_TOKEN=xxx RUST_LOG=cnb=debug \
CNB_TOKEN=xxx
测试
集成测试基于 wiremock,覆盖鉴权、错误体解析、
重试、Retry-After、默认头、典型资源 happy path:
重新生成 SDK
整个 SDK(src/models/data.rs + 28 个资源模块)由 codegen/cnb-codegen 工具
从 codegen/spec/swagger.json 生成。
当上游 spec 更新时:
MSRV
最低支持 Rust 1.75。CI 在 stable + 1.75 双轨道上运行。
升级到 0.2.0
0.2.0 不向后兼容 0.1.x。完整迁移指南见 CHANGELOG.md。