//! Example: Knafeh RPC client in Rust.
//!
//! Run with (after starting the server):
//!     cargo run --example rust_client

use knafeh::client::retry::RetryPolicy;
use knafeh::client::Client;
use knafeh::codec::JsonCodec;
use knafeh::transport::tls::TlsConfig;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    tracing_subscriber::fmt::init();

    let tls = TlsConfig::client_insecure();

    let client = Client::builder()
        .endpoint("localhost:4433")
        .tls(tls)
        .codec(JsonCodec::new())
        .pool_size(4)
        .retry(RetryPolicy::exponential(3))
        .build()
        .await?;

    // Unary RPC call.
    let request = serde_json::json!({ "name": "Knafeh" });
    let body = serde_json::to_vec(&request)?;

    match client.call("greeter/say_hello", body).await {
        Ok(response) => {
            let value: serde_json::Value = serde_json::from_slice(&response.body)?;
            println!("say_hello: {value}");
        }
        Err(e) => {
            eprintln!("RPC error: {e}");
        }
    }

    Ok(())
}
