Skip to content

Commit

Permalink
adding connection timeout in secs with default value 10 secs (#179)
Browse files Browse the repository at this point in the history
Allow for the setting of a custom timeout for all requests
---------

Co-authored-by: Dinculescu <[email protected]>
  • Loading branch information
skoky and mihai-dinculescu authored Mar 29, 2024
1 parent d0fd53a commit 8baead0
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ file. This change log follows the conventions of

- The implementation of `ApiClient::new` has been improved to allow for the return of `ApiClient` instead of `Result<ApiClient, Error>`.
- The default timeout for all requests has been reduced to 30 seconds from 300 seconds.
- `ApiClient::with_timeout` has been added to allow for the setting of a custom timeout for all requests (thanks to @skoky).

## [Python Unreleased][Unreleased]

### Changed

- The default timeout for all requests has been reduced to 30 seconds from 300 seconds.
- The `timeout_s` optional parameter has been added to the `ApiClient` constructor to allow for the setting of a custom timeout for all requests (thanks to @skoky).

## [Rust v0.7.9][v0.7.9] - 2024-01-27

### Changed
Expand Down
14 changes: 12 additions & 2 deletions tapo-py/src/api_client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use pyo3::prelude::*;
use std::time::Duration;
use tapo::ApiClient;

use crate::errors::ErrorWrapper;
Expand All @@ -15,8 +16,17 @@ 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_s: Option<u64>,
) -> Result<Self, ErrorWrapper> {
let client = match timeout_s {
Some(timeout_s) => ApiClient::new(tapo_username, tapo_password)
.with_timeout(Duration::from_secs(timeout_s)),
None => ApiClient::new(tapo_username, tapo_password),
};

Ok(Self { client })
}

Expand Down
23 changes: 18 additions & 5 deletions tapo-py/tapo-py/tapo/api_client.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ Example:
See [more examples](https://github.com/mihai-dinculescu/tapo/tree/main/tapo-py/examples).
"""

from typing import Optional
from .color_light_handler import ColorLightHandler
from .generic_device_handler import GenericDeviceHandler
from .light_handler import LightHandler
from .color_light_handler import ColorLightHandler
from .plug_handler import PlugHandler
from .plug_energy_monitoring_handler import PlugEnergyMonitoringHandler
from .plug_handler import PlugHandler

class ApiClient:
"""Tapo API Client.
Expand All @@ -51,12 +52,13 @@ class ApiClient:
See [more examples](https://github.com/mihai-dinculescu/tapo/tree/main/tapo-py/examples).
"""

def __init__(self, tapo_username: str, tapo_password: str) -> None:
def __init__(self, tapo_username: str, tapo_password: str, timeout_s: int = 30) -> None:
"""Returns a new instance of `ApiClient`.
Args:
tapo_username (str): The Tapo username
tapo_password (str): The Tapo password
tapo_username (str): The Tapo username.
tapo_password (str): The Tapo password.
timeout_s (int): The connection timeout in seconds. The default value is 30 seconds.
Returns:
ApiClient: Tapo API Client.
Expand All @@ -79,6 +81,7 @@ class ApiClient:
See [more examples](https://github.com/mihai-dinculescu/tapo/tree/main/tapo-py/examples).
"""

async def generic_device(self, ip_address: str) -> GenericDeviceHandler:
"""Specializes the given `ApiClient` into an authenticated `GenericDeviceHandler`.
Expand All @@ -97,6 +100,7 @@ class ApiClient:
await device.on()
```
"""

async def l510(self, ip_address: str) -> LightHandler:
"""Specializes the given `ApiClient` into an authenticated `LightHandler`.
Expand All @@ -114,6 +118,7 @@ class ApiClient:
await device.on()
```
"""

async def l520(self, ip_address: str) -> LightHandler:
"""Specializes the given `ApiClient` into an authenticated `LightHandler`.
Expand All @@ -131,6 +136,7 @@ class ApiClient:
await device.on()
```
"""

async def l530(self, ip_address: str) -> ColorLightHandler:
"""Specializes the given `ApiClient` into an authenticated `ColorLightHandler`.
Expand All @@ -148,6 +154,7 @@ class ApiClient:
await device.on()
```
"""

async def l610(self, ip_address: str) -> LightHandler:
"""Specializes the given `ApiClient` into an authenticated `LightHandler`.
Expand All @@ -165,6 +172,7 @@ class ApiClient:
await device.on()
```
"""

async def l630(self, ip_address: str) -> ColorLightHandler:
"""Specializes the given `ApiClient` into an authenticated `ColorLightHandler`.
Expand All @@ -182,6 +190,7 @@ class ApiClient:
await device.on()
```
"""

async def l900(self, ip_address: str) -> ColorLightHandler:
"""Specializes the given `ApiClient` into an authenticated `ColorLightHandler`.
Expand All @@ -199,6 +208,7 @@ class ApiClient:
await device.on()
```
"""

async def p100(self, ip_address: str) -> PlugHandler:
"""Specializes the given `ApiClient` into an authenticated `PlugHandler`.
Expand All @@ -216,6 +226,7 @@ class ApiClient:
await device.on()
```
"""

async def p105(self, ip_address: str) -> PlugHandler:
"""Specializes the given `ApiClient` into an authenticated `PlugHandler`.
Expand All @@ -233,6 +244,7 @@ class ApiClient:
await device.on()
```
"""

async def p110(self, ip_address: str) -> PlugEnergyMonitoringHandler:
"""Specializes the given `ApiClient` into an authenticated `PlugEnergyMonitoringHandler`.
Expand All @@ -250,6 +262,7 @@ class ApiClient:
await device.on()
```
"""

async def p115(self, ip_address: str) -> PlugEnergyMonitoringHandler:
"""Specializes the given `ApiClient` into an authenticated `PlugEnergyMonitoringHandler`.
Expand Down
15 changes: 14 additions & 1 deletion tapo/src/api/api_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,16 @@ pub struct ApiClient {
/// Tapo API Client constructor.
impl ApiClient {
/// Returns a new instance of [`ApiClient`].
/// It it cheaper to [`ApiClient::clone`] an existing instance than to create a new one when multiple devices need to be controller.
/// It is cheaper to [`ApiClient::clone`] an existing instance than to create a new one when multiple devices need to be controller.
/// This is because [`ApiClient::clone`] reuses the underlying [`isahc::HttpClient`] and [`openssl::rsa::Rsa`] key.
///
/// # Arguments
///
/// * `tapo_username` - the Tapo username
/// * `tapo_password` - the Tapo password
///
/// Note: The default connection timeout is 30 seconds.
/// Use [`ApiClient::with_timeout`] to change it.
pub fn new(tapo_username: impl Into<String>, tapo_password: impl Into<String>) -> ApiClient {
Self {
tapo_username: tapo_username.into(),
Expand All @@ -75,6 +78,16 @@ impl ApiClient {
protocol: None,
}
}

/// Changes the connection timeout from the default value to the given value.
///
/// # Arguments
///
/// * `timeout` - The new connection timeout value.
pub fn with_timeout(mut self, timeout: Duration) -> ApiClient {
self.timeout = Some(timeout);
self
}
}

/// Device handler builders.
Expand Down

0 comments on commit 8baead0

Please sign in to comment.