Skip to content

Commit

Permalink
Merge pull request #1 from sigseg5/connection_timeout_ref
Browse files Browse the repository at this point in the history
Connection timeout refactoring
  • Loading branch information
sigseg5 authored Sep 11, 2023
2 parents dab23ce + 4a369f2 commit bfc250d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 19 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "outline_api"
version = "1.0.3"
version = "2.0.0"
edition = "2021"
authors = ["sigseg5"]
license = "MIT"
Expand All @@ -18,7 +18,7 @@ exclude = [

[dependencies]
reqwest = { version = "0.11.20", features = ["blocking"] }
serde_json = "1.0.105"
serde_json = "1.0.106"
log = "0.4.20"
env_logger = "0.10.0"

Expand Down
24 changes: 7 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ fn handle_json_api_error(response: Response) -> Result<serde_json::Value, String
///
/// - `api_url`: A reference to a string representing the URL (including `secret`) of the Outline VPN server API.
/// - `session`: A reqwest HTTP client used to make API requests.
/// - `request_timeout`: The time to set the timeout for API requests.
/// - `request_timeout_in_sec`: The time to set the timeout for API requests.
pub struct OutlineVPN<'a> {
api_url: &'a str,
session: Client,
request_timeout: Duration,
request_timeout_in_sec: Duration,
}

// Endpoints
Expand All @@ -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,
Expand All @@ -147,7 +145,7 @@ impl OutlineVPN<'_> {
let response = self
.session
.request(request_method, &url)
.timeout(self.request_timeout)
.timeout(self.request_timeout_in_sec)
.header(reqwest::header::CONTENT_TYPE, "application/json")
.body(request_body)
.send()?;
Expand Down Expand Up @@ -483,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
///
Expand All @@ -501,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);
///
Expand All @@ -519,7 +516,7 @@ impl OutlineVPN<'_> {
pub fn new<'a>(
cert_sha256: &'a str,
api_url: &'a str,
request_timeout: Option<Duration>,
request_timeout: Duration,
) -> OutlineVPN<'a> {
let mut headers = HeaderMap::new();
headers.insert(
Expand All @@ -534,16 +531,9 @@ pub fn new<'a>(
.build()
.unwrap();

let default_request_timeout = Duration::from_secs(REQUEST_TIMEOUT_IN_SEC);
let safe_request_timeout = if let Some(timeout) = request_timeout {
timeout
} else {
default_request_timeout
};

OutlineVPN {
api_url: &api_url,
session,
request_timeout: safe_request_timeout,
request_timeout_in_sec: request_timeout,
}
}

0 comments on commit bfc250d

Please sign in to comment.