
# Rust {/* #rust */}

Platform API 토큰을 만들고 `platform.proto`를 받으려면 [gRPC 가이드](/docs/ko/api/grpc)부터 시작하세요. 이 예시는 [tonic 및 tonic-prost-build](https://docs.rs/tonic-build/latest/tonic_build/)로 네이티브 HTTP/2 클라이언트를 생성합니다. 먼저 Rust와 `protoc`를 설치하세요.

프로젝트를 만들고 다운로드한 계약을 `proto/platform.proto`에 넣으세요:

```bash
cargo new reconstruct-client
cd reconstruct-client
mkdir proto
```

다음 `Cargo.toml`을 사용하세요:

```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`로 빌드 중 클라이언트 형식을 생성하세요:

```rust
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`에서 사용하세요:

```rust
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 전용입니다.
