From c6827ad1d8951c24848fedc9c657cb813d3b5f20 Mon Sep 17 00:00:00 2001 From: Adam Chalmers Date: Tue, 8 Aug 2023 11:04:54 -0500 Subject: [PATCH] OpenAPItor has configurable request timeouts (#219) --- Cargo.lock | 17 ++++++++--------- Makefile | 1 + kittycad/src/lib.rs | 2 +- openapitor/src/client.rs | 19 +++++++++++++++---- openapitor/src/lib.rs | 5 +++++ 5 files changed, 30 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ab02514..e03da05 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -272,9 +272,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.2.4" +version = "4.3.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "956ac1f6381d8d82ab4684768f89c0ea3afe66925ceadb4eeb3fc452ffc55d62" +checksum = "c27cdf28c0f604ba3f512b0c9a409f8de8513e4816705deb0498b627e7c3a3fd" dependencies = [ "clap_builder", "clap_derive", @@ -283,13 +283,12 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.2.4" +version = "4.3.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84080e799e54cff944f4b4a4b0e71630b0e0443b25b985175c7dddc1a859b749" +checksum = "08a9f1ab5e9f01a9b81f202e8562eb9a10de70abf9eaeac1be465c28b75aa4aa" dependencies = [ "anstream", "anstyle", - "bitflags", "clap_lex", "once_cell", "strsim", @@ -300,9 +299,9 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.2.0" +version = "4.3.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f9644cd56d6b87dbe899ef8b053e331c0637664e9e21a33dfcdc36093f5c5c4" +checksum = "54a9bb5758fc5dfe728d1019941681eccaf0cf8a4189b692a0ee2f2ecf90a050" dependencies = [ "heck", "proc-macro2", @@ -312,9 +311,9 @@ dependencies = [ [[package]] name = "clap_lex" -version = "0.4.1" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a2dd5a6fe8c6e3502f568a6353e5273bbb15193ad9a89e457b9970798efbea1" +checksum = "2da6da31387c7e4ef160ffab6d5e7f00c42626fe39aea70a7b0f1773f7dd6c1b" [[package]] name = "codespan-reporting" diff --git a/Makefile b/Makefile index 251d437..a5fcdb7 100644 --- a/Makefile +++ b/Makefile @@ -28,6 +28,7 @@ kittycad: target/debug/openapitor -d "A fully generated & opinionated API client for the KittyCAD API." \ --spec-url "https://api.kittycad.io" \ --base-url "https://api.kittycad.io" \ + --request-timeout-seconds 600 \ --repo-name "KittyCAD/kittycad.rs" $(EXTRA_ARGS) mv -f $(CURDIR)/kittycad/kittycad.rs.patch.json $(CURDIR) diff --git a/kittycad/src/lib.rs b/kittycad/src/lib.rs index 434e6a6..37f2af7 100644 --- a/kittycad/src/lib.rs +++ b/kittycad/src/lib.rs @@ -154,7 +154,7 @@ impl Client { let client_http1 = reqwest::Client::builder() // For file conversions we need this to be long. .user_agent(APP_USER_AGENT) - .timeout(std::time::Duration::from_secs(60)) + .timeout(std::time::Duration::from_secs(600)) .connect_timeout(std::time::Duration::from_secs(60)) .http1_only() .build(); diff --git a/openapitor/src/client.rs b/openapitor/src/client.rs index b94229b..ebd4f21 100644 --- a/openapitor/src/client.rs +++ b/openapitor/src/client.rs @@ -18,11 +18,19 @@ pub fn generate_client(opts: &crate::Opts) -> String { "USER_CONSENT_ENDPOINT", opts.user_consent_endpoint.as_ref().unwrap().as_ref(), ) + .replace( + "TIMEOUT_NUM_SECONDS", + &opts.request_timeout_seconds.to_string(), + ) .replace("BASE_URL", opts.base_url.to_string().trim_end_matches('/')); } if opts.basic_auth { return CLIENT_FUNCTIONS_BASIC_AUTH + .replace( + "TIMEOUT_NUM_SECONDS", + &opts.request_timeout_seconds.to_string(), + ) .replace( "ENV_VARIABLE_PREFIX", &crate::template::get_env_variable_prefix(&opts.name), @@ -31,6 +39,10 @@ pub fn generate_client(opts: &crate::Opts) -> String { } CLIENT_FUNCTIONS_TOKEN + .replace( + "TIMEOUT_NUM_SECONDS", + &opts.request_timeout_seconds.to_string(), + ) .replace( "ENV_VARIABLE_PREFIX", &crate::template::get_env_variable_prefix(&opts.name), @@ -74,8 +86,7 @@ impl Client { reqwest_retry::policies::ExponentialBackoff::builder().build_with_max_retries(3); let client = reqwest::Client::builder() .user_agent(APP_USER_AGENT) - // For file conversions we need this to be long. - .timeout(std::time::Duration::from_secs(600)) + .timeout(std::time::Duration::from_secs(TIMEOUT_NUM_SECONDS)) .connect_timeout(std::time::Duration::from_secs(60)) .build(); match client { @@ -201,13 +212,13 @@ impl Client { let client = reqwest::Client::builder() .user_agent(APP_USER_AGENT) // For file conversions we need this to be long. - .timeout(std::time::Duration::from_secs(600)) + .timeout(std::time::Duration::from_secs(TIMEOUT_NUM_SECONDS)) .connect_timeout(std::time::Duration::from_secs(60)) .build(); let client_http1 = reqwest::Client::builder() // For file conversions we need this to be long. .user_agent(APP_USER_AGENT) - .timeout(std::time::Duration::from_secs(60)) + .timeout(std::time::Duration::from_secs(TIMEOUT_NUM_SECONDS)) .connect_timeout(std::time::Duration::from_secs(60)) .http1_only() .build(); diff --git a/openapitor/src/lib.rs b/openapitor/src/lib.rs index eccded3..7a3a11f 100644 --- a/openapitor/src/lib.rs +++ b/openapitor/src/lib.rs @@ -458,6 +458,10 @@ pub struct Opts { /// Use basic auth for authentication instead of bearer tokens #[arg(long)] pub basic_auth: bool, + + /// Default timeout on the client + #[arg(long)] + pub request_timeout_seconds: u64, } impl Opts { @@ -523,6 +527,7 @@ impl Default for Opts { user_consent_endpoint: Default::default(), date_time_format: Default::default(), basic_auth: Default::default(), + request_timeout_seconds: 60, } } }