From 4a369f2de9006978c29d4a81c020e01a764b5615 Mon Sep 17 00:00:00 2001 From: sigseg5 Date: Mon, 11 Sep 2023 13:58:23 +0400 Subject: [PATCH] Polish connection timeout reading logic --- src/lib.rs | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 19348fb..da65dd7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -118,11 +118,11 @@ fn handle_json_api_error(response: Response) -> Result { api_url: &'a str, session: Client, - request_timeout: Option, + request_timeout_in_sec: Duration, } // Endpoints @@ -131,8 +131,6 @@ const HOSTNAME_ENDPOINT: &str = "/server/hostname-for-access-keys"; const CHANGE_PORT_ENDPOINT: &str = "/server/port-for-new-access-keys"; const KEY_DATA_LIMIT_ENDPOINT: &str = "/server/access-key-data-limit"; -const REQUEST_TIMEOUT_IN_SEC: u64 = 5; - impl OutlineVPN<'_> { fn call_api( &self, @@ -147,10 +145,7 @@ impl OutlineVPN<'_> { let response = self .session .request(request_method, &url) - .timeout( - self.request_timeout - .unwrap_or_else(|| std::time::Duration::from_secs(REQUEST_TIMEOUT_IN_SEC)), - ) + .timeout(self.request_timeout_in_sec) .header(reqwest::header::CONTENT_TYPE, "application/json") .body(request_body) .send()?; @@ -486,8 +481,7 @@ impl OutlineVPN<'_> { /// /// - `cert_sha256`: A reference to a string representing the SHA-256 hash of the server's certificate. /// - `api_url`: A reference to a string representing the URL of the Outline VPN server API. -/// - `request_timeout`: An optional `Duration` specifying the timeout for API requests. If `None` is -/// provided, a default timeout of 5 seconds is used. +/// - `request_timeout_in_sec`: `Duration` specifying the timeout for API requests. /// /// # Returns /// @@ -504,7 +498,7 @@ impl OutlineVPN<'_> { /// // https://github.com/sigseg5/outline-api/blob/master/README.md /// let api_url = "https://example.com/secret"; /// let cert_sha256 = "cert_sha256_hash"; -/// let request_timeout = Some(Duration::from_secs(10)); +/// let request_timeout = Duration::from_secs(10); /// /// let outline_vpn = outline_api::new(api_url, cert_sha256, request_timeout); /// @@ -522,7 +516,7 @@ impl OutlineVPN<'_> { pub fn new<'a>( cert_sha256: &'a str, api_url: &'a str, - request_timeout: Option, + request_timeout: Duration, ) -> OutlineVPN<'a> { let mut headers = HeaderMap::new(); headers.insert( @@ -540,6 +534,6 @@ pub fn new<'a>( OutlineVPN { api_url: &api_url, session, - request_timeout, + request_timeout_in_sec: request_timeout, } }