Rust
API टोकन और platform.proto के लिए gRPC मार्गदर्शन देखें। उदाहरण tonic और tonic-prost-build से मूल HTTP/2 क्लाइंट बनाता है। पहले Rust और protoc इंस्टॉल करें।
प्रोजेक्ट बनाकर अनुबंध proto/platform.proto रखें:
cargo new reconstruct-client
cd reconstruct-client
mkdir proto
यह Cargo.toml लें:
[package]
name = "reconstruct-client"
version = "0.1.0"
edition = "2021"
[dependencies]
prost = "0.14"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
tonic = { version = "0.14", features = ["transport", "tls-ring", "tls-native-roots"] }
tonic-prost = "0.14"
[build-dependencies]
tonic-prost-build = "0.14"
मूल डायरेक्टरी में build.rs से बिल्ड के समय क्लाइंट प्रकार बनाएँ:
fn main() -> Result<(), Box<dyn std::error::Error>> {
tonic_prost_build::configure()
.build_server(false)
.compile_protos(&["proto/platform.proto"], &["proto"])?;
Ok(())
}
बना क्लाइंट src/main.rs में लें:
use std::env;
use tonic::{transport::{Channel, ClientTlsConfig}, Request};
pub mod platform {
tonic::include_proto!("reconstruct.platform.v1");
}
use platform::{
platform_service_client::PlatformServiceClient,
GetCatalogRequest, ListCatalogsRequest,
};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let token = env::var("CR_RECONSTRUCT_API_TOKEN")?;
let channel = Channel::from_static("https://reconstruct.dev")
.tls_config(ClientTlsConfig::new().with_native_roots())?
.connect()
.await?;
let mut client = PlatformServiceClient::new(channel);
let mut request = Request::new(ListCatalogsRequest {
page_size: 10,
page_token: String::new(),
});
request.metadata_mut().insert("authorization", format!("Bearer {token}").parse()?);
let page = client.list_catalogs(request).await?.into_inner();
for catalog in page.catalogs {
let mut request = Request::new(GetCatalogRequest { catalog_id: catalog.catalog_id });
request.metadata_mut().insert("authorization", format!("Bearer {token}").parse()?);
let detail = client.get_catalog(request).await?.into_inner();
println!("{}: {}", detail.catalog_id, detail.description);
}
Ok(())
}
cargo run चलाएँ। मूल gRPC HTTPS authority से जुड़ता है; /api/v1/platform केवल REST है।