Skip to content

Commit 57347e7

Browse files
authoredFeb 9, 2025
feat(client): add a client port override method (#259)
* feat(client): add a client port override method Signed-off-by: Mike Nguyen <hey@mike.ee> * chore(lint): cargo fmt Signed-off-by: Mike Nguyen <hey@mike.ee> * doc(client): add port method Signed-off-by: mikeee <hey@mike.ee> * release: rc6 Signed-off-by: mikeee <hey@mike.ee> --------- Signed-off-by: Mike Nguyen <hey@mike.ee> Signed-off-by: mikeee <hey@mike.ee>
1 parent 93322c0 commit 57347e7

File tree

5 files changed

+30
-3
lines changed

5 files changed

+30
-3
lines changed
 

‎Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ tonic = "0.12.3"
2626
tonic-build = "0.12.3"
2727

2828
[workspace.package]
29-
version = "0.16.0-rc.5"
29+
version = "0.16.0-rc.6"
3030
authors = [
3131
"Mike Nguyen <hey@mike.ee>",
3232
"The Dapr Authors <dapr@dapr.io>"

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Add the following to your `Cargo.toml` file:
5353

5454
```toml
5555
[dependencies]
56-
dapr = "0.16.0-rc.5"
56+
dapr = "0.16.0-rc.6"
5757
```
5858

5959
Here's a basic example to create a client:

‎dapr/src/client.rs

+20
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,26 @@ impl<T: DaprInterface> Client<T> {
3030
Ok(Client(T::connect(address).await?))
3131
}
3232

33+
/// Connect to the Dapr sidecar with a specific port.
34+
///
35+
/// # Arguments
36+
///
37+
/// * `addr` - Address of gRPC server to connect to.
38+
/// * `port` - Port of the gRPC server to connect to.
39+
pub async fn connect_with_port(addr: String, port: String) -> Result<Self, Error> {
40+
// assert that port is between 1 and 65535
41+
let port: u16 = match port.parse::<u16>() {
42+
Ok(p) => p,
43+
Err(_) => {
44+
panic!("Port must be a number between 1 and 65535");
45+
}
46+
};
47+
48+
let address = format!("{}:{}", addr, port);
49+
50+
Ok(Client(T::connect(address).await?))
51+
}
52+
3353
/// Invoke a method in a Dapr enabled app.
3454
///
3555
/// # Arguments

‎daprdocs/content/en/rust-sdk-docs/rust-client/_index.md

+6
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ let mut client = dapr::Client::<dapr::client::TonicClient>::connect(addr,
4646
port).await?;
4747
```
4848

49+
Alternatively if you would like to specify a custom port, this can be done by using this connect method:
50+
51+
```rust
52+
let mut client = dapr::Client::<dapr::client::TonicClient>::connect_with_port(addr, "3500".to_string()).await?;
53+
```
54+
4955
## Building blocks
5056

5157
The Rust SDK allows you to interface with the

‎examples/src/conversation/main.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
1010

1111
// Set the Dapr address
1212
let address = "https://127.0.0.1".to_string();
13+
let port = "3500".to_string();
1314

14-
let mut client = DaprClient::connect(address).await?;
15+
let mut client = DaprClient::connect_with_port(address, port).await?;
1516

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

0 commit comments

Comments
 (0)