#Getting started with the Rust Client SDK

1 messages · Page 1 of 1 (latest)

frozen whale
vague topaz
#

sorry about that @frozen whale , the example is outdated and I will work on getting it updated. In the meantime, can you update dependencies to add viam-rust-utils

[dependencies]
viam = {path = "../"} #this path should point to the Rust SDK repository
viam-rust-utils = "0.0.18"
anyhow = { version = "1.0", features = ["backtrace"]}
tokio = { version = "1.0", features = [ "rt-multi-thread", "time", "fs", "macros", "net",] }
tonic = {version = "0.6.2",features = ["tls", "compression", "tls-roots"]}

and the full sample code will then be

use viam::gen::proto::robot::v1::{robot_service_client, ResourceNamesRequest};
use viam_rust_utils::rpc::dial;

#[tokio::main]
async fn main() -> Result<()> {
    println!("Hello, world!");

    let creds = dial::RPCCredentials::new(
        None,
        "robot-location-secret".to_string(),
        "<SECRET HERE>".to_string(),
    );

    let c = dial::DialOptions::builder()
        .uri("<ROBOT ADDRESS HERE>") // Robot address
        .with_credentials(creds) // credentials
        .connect()
        .await?; // if the connection complete you will have a channel otherwise an error

    let mut service = robot_service_client::RobotServiceClient::new(c);
    let _rsp = service
        .resource_names(ResourceNamesRequest {})
        .await?;
    println!("Rsp {:?}", _rsp);
    Ok(())
}```
frozen whale
#

got farther this time!

#

I used the code

use dotenv;

use anyhow::Result;
use viam::gen::proto::robot::v1::{robot_service_client, ResourceNamesRequest};
use viam_rust_utils::rpc::dial;

#[tokio::main]
async fn main() -> Result<()> {
    println!("Hello, world!");
    dotenv::dotenv().ok();

    let secret = std::env::var("SECRET").expect("SECRET must be set.");
    let robot_address = std::env::var("ROBOT_ADDRESS").expect("ROBOT_ADDRESS must be set.");

    let creds = dial::RPCCredentials::new(
        None,
        "robot-location-secret".to_string(),
        secret,
    );

    let c = dial::DialOptions::builder()
        .uri(&robot_address) // Robot address
        .with_credentials(creds) // credentials
        .connect()
        .await?; // if the connection complete you will have a channel otherwise an error

    let mut service = robot_service_client::RobotServiceClient::new(c);
    let _rsp = service
        .resource_names(ResourceNamesRequest {})
        .await?;
    println!("Rsp {:?}", _rsp);
    Ok(())
}
#

to connect to Try Viam

#

and I got the error

#

cpaliqaw@cpaliqaw-robontik-gf63:~/dev/rust/hello-rover$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.61s
Running target/debug/hello-rover
Hello, world!
Error: status: Unimplemented, message: "unknown service viam.robot.v1.RobotService", details: [], metadata: MetadataMap { headers: {"content-type": "application/grpc", "strict-transport-security": "max-age=63072000; includeSubDomains; preload", "trailer": "Grpc-Status", "trailer": "Grpc-Message", "trailer": "Grpc-Status-Details-Bin", "vary": "Origin", "x-content-type-options": "nosniff", "x-frame-options": "SAMEORIGIN", "date": "Tue, 16 May 2023 19:02:41 GMT", "server": "Google Frontend", "traceparent": "00-fac9256c88167622c0a33bf818546b9e-42fe2c678ae05699-00", "x-cloud-trace-context": "fac9256c88167622c0a33bf818546b9e/4827344673810962073", "content-length": "0"} }

vague topaz
#

thanks for trying that out! we've seen variants of this issue and fixed it in our most recent rust-utils release (which unfortunately isn't on crates.io), but try updating rust-utils to 0.0.19 by changing the dependency to viam-rust-utils = { git = "https://github.com/viamrobotics/rust-utils.git", rev = "e177376c084bdadf8d67d077629fb16b3cd72097" }. if that doesn't work, can you set debug mode to on by setting RUST_LOG="debug" and sending us the output?

frozen whale
#

worked like a charm, thanks Cheuk!

frozen whale
#

can you give a quick example of how to connect to get connected to the base, so I can drive using the SDK?

vague topaz
#

I'm a total amateur at rust, so bear with me if anything is wrong here. also worth noting that the rust SDK is in a very primitive state and we will be interacting with the raw generated protos, so some of the nicer wrapper functionality in Python and TypeScript is missing.

    let mut base_service = base_service_client::BaseServiceClient::new(c);
    let _rsp = base_service
        .is_moving(IsMovingRequest {name:"<BASE_NAME_HERE>".to_string()})
        .await?;
    println!("Rsp {:?}", _rsp);

because of language reasons, if you're just adding this block underneath the previous script, you'll have to clone c since it has moved. The thing to keep in mind is that you'll have to remember to add name as a parameter to every request. Let me know if this works and if you have more questions!