Skip to content

Commit

Permalink
feat(client): add a client port override method (#259)
Browse files Browse the repository at this point in the history
* feat(client): add a client port override method

Signed-off-by: Mike Nguyen <[email protected]>

* chore(lint): cargo fmt

Signed-off-by: Mike Nguyen <[email protected]>

* doc(client): add port method

Signed-off-by: mikeee <[email protected]>

* release: rc6

Signed-off-by: mikeee <[email protected]>

---------

Signed-off-by: Mike Nguyen <[email protected]>
Signed-off-by: mikeee <[email protected]>
  • Loading branch information
mikeee authored Feb 9, 2025
1 parent 93322c0 commit 57347e7
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ tonic = "0.12.3"
tonic-build = "0.12.3"

[workspace.package]
version = "0.16.0-rc.5"
version = "0.16.0-rc.6"
authors = [
"Mike Nguyen <[email protected]>",
"The Dapr Authors <[email protected]>"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Add the following to your `Cargo.toml` file:

```toml
[dependencies]
dapr = "0.16.0-rc.5"
dapr = "0.16.0-rc.6"
```

Here's a basic example to create a client:
Expand Down
20 changes: 20 additions & 0 deletions dapr/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@ impl<T: DaprInterface> Client<T> {
Ok(Client(T::connect(address).await?))
}

/// Connect to the Dapr sidecar with a specific port.
///
/// # Arguments
///
/// * `addr` - Address of gRPC server to connect to.
/// * `port` - Port of the gRPC server to connect to.
pub async fn connect_with_port(addr: String, port: String) -> Result<Self, Error> {
// assert that port is between 1 and 65535
let port: u16 = match port.parse::<u16>() {
Ok(p) => p,
Err(_) => {
panic!("Port must be a number between 1 and 65535");
}
};

let address = format!("{}:{}", addr, port);

Ok(Client(T::connect(address).await?))
}

/// Invoke a method in a Dapr enabled app.
///
/// # Arguments
Expand Down
6 changes: 6 additions & 0 deletions daprdocs/content/en/rust-sdk-docs/rust-client/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ let mut client = dapr::Client::<dapr::client::TonicClient>::connect(addr,
port).await?;
```

Alternatively if you would like to specify a custom port, this can be done by using this connect method:

```rust
let mut client = dapr::Client::<dapr::client::TonicClient>::connect_with_port(addr, "3500".to_string()).await?;
```

## Building blocks

The Rust SDK allows you to interface with the
Expand Down
3 changes: 2 additions & 1 deletion examples/src/conversation/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

// Set the Dapr address
let address = "https://127.0.0.1".to_string();
let port = "3500".to_string();

let mut client = DaprClient::connect(address).await?;
let mut client = DaprClient::connect_with_port(address, port).await?;

let input = ConversationInputBuilder::new("hello world").build();

Expand Down

0 comments on commit 57347e7

Please sign in to comment.