Skip to main content

nucel_sdk_api/
lib.rs

1//! Auto-generated Rust SDK for the nucel.dev REST API.
2//!
3//! The contents of this crate are generated at build time by [progenitor]
4//! from `openapi/nucel.json` (the OpenAPI spec exported from the
5//! `nucel-server` utoipa annotations).
6//!
7//! # Usage
8//!
9//! ```no_run
10//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
11//! let client = nucel_sdk_api::new("https://nucel.dev", Some("ghp_token".into()))?;
12//! let repo = client.get_repo().owner("acme").repo("widgets").send().await?;
13//! println!("{}", repo.into_inner());
14//! # Ok(())
15//! # }
16//! ```
17//!
18//! [progenitor]: https://github.com/oxidecomputer/progenitor
19
20#![allow(clippy::all, unused_imports, dead_code)]
21
22include!(concat!(env!("OUT_DIR"), "/codegen.rs"));
23
24/// Build a `Client` configured for `base_url` with an optional bearer token.
25///
26/// All requests issued through the returned client will include
27/// `Authorization: Bearer <token>` if a token is provided.
28pub fn new(
29    base_url: impl Into<String>,
30    token: Option<String>,
31) -> Result<Client, reqwest::Error> {
32    use reqwest::header;
33
34    let mut headers = header::HeaderMap::new();
35    if let Some(t) = token {
36        let mut value = header::HeaderValue::from_str(&format!("Bearer {t}"))
37            .expect("token contains invalid header bytes");
38        value.set_sensitive(true);
39        headers.insert(header::AUTHORIZATION, value);
40    }
41    headers.insert(
42        header::USER_AGENT,
43        header::HeaderValue::from_static(concat!("nucel-sdk-api/", env!("CARGO_PKG_VERSION"))),
44    );
45
46    let http = reqwest::Client::builder()
47        .default_headers(headers)
48        .build()?;
49
50    Ok(Client::new_with_client(&base_url.into(), http))
51}