9个版本 (破坏性更新)

0.7.0 2024年5月15日
0.6.0 2024年2月28日
0.5.0 2023年12月16日
0.4.0 2023年9月25日
0.0.0 2021年6月19日

#19 in 编程语言

Download history 814/week @ 2024-04-27 906/week @ 2024-05-04 1265/week @ 2024-05-11 1559/week @ 2024-05-18 1491/week @ 2024-05-25 1239/week @ 2024-06-01 956/week @ 2024-06-08 1415/week @ 2024-06-15 1108/week @ 2024-06-22 953/week @ 2024-06-29 729/week @ 2024-07-06 686/week @ 2024-07-13 1125/week @ 2024-07-20 996/week @ 2024-07-27 636/week @ 2024-08-03 726/week @ 2024-08-10

3,560 每月下载量
11 crate(8个直接)中使用

MPL-2.0 许可协议

250KB
5K SLoC

祖先

祖先是一个Rust crate,用于从OpenAPI 3.0.x规范中的API描述生成具有偏见的客户端。它利用Rust futures进行异步API调用,并使用Streams处理分页接口。

它生成一个名为Client的类型,其中包含与OpenAPI文档中指定的操作相对应的方法。

祖先还可以生成一个CLI与OpenAPI服务实例交互,以及httpmock辅助工具来创建OpenAPI服务的强类型模拟。

主要目标是Dropshot生成的API生成的OpenAPI文档,但也可以用于许多其他OpenAPI文档。由于OpenAPI涵盖了广泛的API,因此祖先可能无法处理某些OpenAPI文档。如果您遇到问题,可以通过提交包含导致问题的OpenAPI文档的问题来帮助该项目。

使用祖先

有三种不同的方式使用progenitor crate。您选择的方法将取决于您的用例和偏好。

使用祖先最简单的方法是通过其generate_api!宏。

在源文件中(通常是main.rslib.rsmod.rs)简单调用宏

generate_api!("path/to/openapi_document.json");

您需要将以下内容添加到Cargo.toml

[dependencies]
futures = "0.3"
progenitor = { git = "https://github.com/oxidecomputer/progenitor" }
reqwest = { version = "0.11", features = ["json", "stream"] }
serde = { version = "1.0", features = ["derive"] }

此外,如果OpenAPI文档包含具有format字段设置为datedate-time的字符串类型,请包含

[dependencies]
chrono = { version = "0.4", features = ["serde"] }

同样,如果存在format字段设置为uuid

[dependencies]
uuid = { version = "1.0.0", features = ["serde", "v4"] }

以及任何WebSocket通道端点

[dependencies]
base64 = "0.21"
rand = "0.8"

如果类型包括正则表达式验证

[dependencies]
regress = "0.4.1"

宏有一些额外的复杂选项来控制生成的代码

generate_api!(
    spec = "path/to/openapi_document.json",      // The OpenAPI document
    interface = Builder,                         // Choose positional (default) or builder style
    tags = Separate,                             // Tags may be Merged or Separate (default)
    inner_type = my_client::InnerType,           // Client inner type available to pre and post hooks
    pre_hook = closure::or::path::to::function,  // Hook invoked before issuing the HTTP request
    post_hook = closure::or::path::to::function, // Hook invoked prior to receiving the HTTP response
    derives = [ schemars::JsonSchema ],          // Additional derive macros applied to generated types
);

请注意,当spec OpenAPI文档更改时(当它的mtime更新时),宏将被重新评估。

build.rs

Progenitor包含一个适合在build.rs文件中使用的接口。虽然比宏稍微麻烦一些,但构建器的好处是可以使生成的代码可见。生成CLI和httpmock辅助器的功能仅通过build.rsGenerator函数的clihttpmock分别可用。

build.rs文件可能看起来像这样

fn main() {
    let src = "../sample_openapi/keeper.json";
    println!("cargo:rerun-if-changed={}", src);
    let file = std::fs::File::open(src).unwrap();
    let spec = serde_json::from_reader(file).unwrap();
    let mut generator = progenitor::Generator::default();

    let tokens = generator.generate_tokens(&spec).unwrap();
    let ast = syn::parse2(tokens).unwrap();
    let content = prettyplease::unparse(&ast);

    let mut out_file = std::path::Path::new(&std::env::var("OUT_DIR").unwrap()).to_path_buf();
    out_file.push("codegen.rs");

    std::fs::write(out_file, content).unwrap();
}

在源文件中(通常是main.rslib.rsmod.rs)包含生成的代码

include!(concat!(env!("OUT_DIR"), "/codegen.rs"));

您需要将以下内容添加到Cargo.toml

[dependencies]
futures = "0.3"
progenitor-client = { git = "https://github.com/oxidecomputer/progenitor" }
reqwest = { version = "0.11", features = ["json", "stream"] }
serde = { version = "1.0", features = ["derive"] }

[build-dependencies]
prettyplease = "0.1.25"
progenitor = { git = "https://github.com/oxidecomputer/progenitor" }
serde_json = "1.0"
syn = "1.0"

(如上所述的chronouuidbase64rand)

请注意,虽然progenitorbuild.rs使用,但所需的生成代码需要progenitor-client

静态Crate

Progenitor可以运行以生成用于生成客户端的独立crate。这确保了没有意外的更改(例如,来自对progenitor的更新)。然而,这是使用Progenitor最手动的方法。

用法

cargo progenitor

Options:
    -i INPUT            OpenAPI definition document (JSON or YAML)
    -o OUTPUT           Generated Rust crate directory
    -n CRATE            Target Rust crate name
    -v VERSION          Target Rust crate version

例如

cargo install cargo-progenitor
cargo progenitor -i sample_openapi/keeper.json -o keeper -n keeper -v 0.1.0

... 或在仓库内

cargo run --bin cargo-progenitor -- progenitor -i sample_openapi/keeper.json -o keeper -n keeper -v 0.1.0

这将生成指定目录中的包。

选项--license--registry-name也可以用于在发布静态crate之前改进元数据。

如果Progenitor是从发布版本构建的,则默认情况下将使用发布的progenitor-client crate。但是,当使用从仓库构建的Progenitor时,默认情况下将内联progenitor-client到静态crate中。可以使用命令行标志--include-client来覆盖默认行为。

为了确保输出没有对Progenitor的持久依赖,请启用--include-client

以下是发出的Cargo.toml的摘录

[dependencies]
bytes = "1.3.0"
chrono = { version = "0.4.23", default-features=false, features = ["serde"] }
futures-core = "0.3.25"
percent-encoding = "2.2.0"
reqwest = { version = "0.11.13", default-features=false, features = ["json", "stream"] }
serde = { version = "1.0.152", features = ["derive"] }
serde_urlencoded = "0.7.1"

在生成的Cargo.toml中的依赖项版本与构建Progenitor时使用的版本相同。

请注意,有一个对percent-encoding的依赖项,宏和build.rs生成的客户端包括来自progenitor-client

生成样式

Progenitor可以生成两种不同的接口样式:位置和构建器(如下所述)。选择只是个人偏好的问题,许多API和品味各不相同。

位置(当前默认值)

"位置"样式生成接受按顺序传递的参数的Client方法,例如

impl Client {
    pub async fn instance_create<'a>(
        &'a self,
        organization_name: &'a types::Name,
        project_name: &'a types::Name,
        body: &'a types::InstanceCreate,
    ) -> Result<ResponseValue<types::Instance>, Error<types::Error>> {
        // ...
    }
}

调用者通过指定按位置参数来调用此接口

let result = client.instance_create(org, proj, body).await?;

请注意,每个参数的类型必须完全匹配--没有隐式转换。

构建器

"构建器"样式生成生成构建器结构的Client方法。API参数应用于该构建器,然后执行构建器(通过send方法)。代码更复杂,更复杂,以使消费者更简单、更易读

impl Client
    pub fn instance_create(&self) -> builder::InstanceCreate {
        builder::InstanceCreate::new(self)
    }
}

mod builder {
    pub struct InstanceCreate<'a> {
        client: &'a super::Client,
        organization_name: Result<types::Name, String>,
        project_name: Result<types::Name, String>,
        body: Result<types::InstanceCreate, String>,
    }

    impl<'a> InstanceCreate<'a> {
        pub fn new(client: &'a super::Client) -> Self {
            // ...
        }

        pub fn organization_name<V>(mut self, value: V) -> Self
        where
            V: TryInto<types::Name>,
        {
            // ...
        }

        pub fn project_name<V>(mut self, value: V) -> Self
        where
            V: TryInto<types::Name>,
        {
            // ...
        }

        pub fn body<V>(mut self, value: V) -> Self
        where
            V: TryInto<types::InstanceCreate>,
        {
            // ...
        }

        pub async fn send(self) ->
            Result<ResponseValue<types::Instance>, Error<types::Error>>
        {
            // ...
        }
    }
}

请注意,与位置生成不同,消费者可以提供兼容的(而不是不可变的)参数

let result = client
    .instance_create()
    .organization_name("org")
    .project_name("proj")
    .body(body)
    .send()
    .await?;

字符串参数将隐式调用 TryFrom::try_from()。失败的转换或缺失的必需参数将导致 Errorsend() 调用返回。

生成的 struct 类型也有构建器,以便可以就地构造 body 参数。

let result = client
    .instance_create()
    .organization_name("org")
    .project_name("proj")
    .body(types::InstanceCreate::builder()
        .name("...")
        .description("...")
        .hostname("...")
        .ncpus(types::InstanceCpuCount(4))
        .memory(types::ByteCount(1024 * 1024 * 1024)),
    )
    .send()
    .await?;

消费者不需要指定不必要或API指定默认值的参数和结构属性。真方便!

依赖项

~12–26MB
~412K SLoC