Skip to content

Commit

Permalink
refactoring rust and py interface of setting timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
skoky committed Mar 3, 2024
1 parent a4bb989 commit 040be15
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion tapo-py/examples/tapo_p100.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async def main():
tapo_password = os.getenv("TAPO_PASSWORD")
ip_address = os.getenv("IP_ADDRESS")

client = ApiClient(tapo_username, tapo_password, timeout_secs=3)
client = ApiClient(tapo_username, tapo_password, timeout_secs=10)
device = await client.p100(ip_address)

print("Turning device on...")
Expand Down
6 changes: 4 additions & 2 deletions tapo-py/src/api_client.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::time::Duration;
use pyo3::prelude::*;
use tapo::ApiClient;

Expand All @@ -15,8 +16,9 @@ pub struct PyApiClient {
#[pymethods]
impl PyApiClient {
#[new]
pub fn new(tapo_username: String, tapo_password: String) -> Result<Self, ErrorWrapper> {
let client = ApiClient::new(tapo_username, tapo_password);
pub fn new(tapo_username: String, tapo_password: String, timeout_secs: Option<u64>) -> Result<Self, ErrorWrapper> {
let client = ApiClient::new(tapo_username, tapo_password)
.with_timeout(Duration::from_secs(timeout_secs.unwrap_or(30)));
Ok(Self { client })
}

Expand Down
2 changes: 1 addition & 1 deletion tapo/examples/tapo_p100.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let ip_address = env::var("IP_ADDRESS")?;

let device = ApiClient::new(tapo_username, tapo_password)
.set_timeout(Duration::from_secs(3))
.with_timeout(Duration::from_secs(3))
.p100(ip_address)
.await?;

Expand Down
2 changes: 1 addition & 1 deletion tapo/src/api/api_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl ApiClient {
/// # Arguments
///
/// * `timeout` - the timeout value
pub fn set_timeout(mut self, timeout: Duration) -> ApiClient {
pub fn with_timeout(mut self, timeout: Duration) -> ApiClient {
self.timeout = Some(timeout);
self
}
Expand Down

0 comments on commit 040be15

Please sign in to comment.