From 3006688bfadd5af84691c82f39abeb54dfdb859a Mon Sep 17 00:00:00 2001 From: Alexander Lyon Date: Wed, 5 Apr 2023 14:24:05 +0100 Subject: [PATCH] fix: generate api version from the codegen --- openapi/src/main.rs | 1 + openapi/src/metadata.rs | 14 ++++++++++++++ openapi/src/spec.rs | 4 ++++ src/client/stripe.rs | 5 +++-- src/resources/generated.rs | 1 + src/resources/generated/version.rs | 3 +++ src/resources/types.rs | 6 ++++++ 7 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 src/resources/generated/version.rs diff --git a/openapi/src/main.rs b/openapi/src/main.rs index 1c506feca..93fe627e2 100644 --- a/openapi/src/main.rs +++ b/openapi/src/main.rs @@ -55,6 +55,7 @@ fn main() -> Result<()> { let url_finder = UrlFinder::new().context("couldn't initialize url finder")?; meta.write_placeholders(&out_path); + meta.write_version(&out_path); // write files and get those files referenced let shared_objects = meta diff --git a/openapi/src/metadata.rs b/openapi/src/metadata.rs index 0cd4a9502..d5fa44718 100644 --- a/openapi/src/metadata.rs +++ b/openapi/src/metadata.rs @@ -140,6 +140,20 @@ impl<'a> Metadata<'a> { write(&out_path.as_ref().join("placeholders.rs"), out.as_bytes()).unwrap(); } + pub fn write_version(&self, out_path: T) + where + T: AsRef, + { + let mut out = String::new(); + out.push_str("use crate::ApiVersion;\n\n"); + out.push_str(&format!( + "pub const VERSION: ApiVersion = ApiVersion::V{};", + self.spec.version().replace('-', "_") + )); + + write(&out_path.as_ref().join("version.rs"), out.as_bytes()).unwrap(); + } + #[tracing::instrument(skip_all)] pub fn get_files(&self) -> Vec { self.objects diff --git a/openapi/src/spec.rs b/openapi/src/spec.rs index 092bd9da2..c229f22ce 100644 --- a/openapi/src/spec.rs +++ b/openapi/src/spec.rs @@ -13,6 +13,10 @@ impl Spec { Self(spec) } + pub fn version(&self) -> &str { + self.0.info.version.as_str() + } + fn components(&self) -> &Components { self.0.components.as_ref().expect("Spec did not contain `components`!") } diff --git a/src/client/stripe.rs b/src/client/stripe.rs index c8a9055b2..d3b0aa23d 100644 --- a/src/client/stripe.rs +++ b/src/client/stripe.rs @@ -4,8 +4,9 @@ use serde::{de::DeserializeOwned, Serialize}; use crate::{ client::{request_strategy::RequestStrategy, BaseClient, Response}, config::err, + generated::core::version::VERSION, params::AppInfo, - AccountId, ApiVersion, ApplicationId, Headers, StripeError, + AccountId, ApplicationId, Headers, StripeError, }; static USER_AGENT: &str = concat!("Stripe/v1 RustBindings/", env!("CARGO_PKG_VERSION")); @@ -33,7 +34,7 @@ impl Client { client: BaseClient::new(), secret_key: secret_key.into(), headers: Headers { - stripe_version: ApiVersion::V2020_08_27, + stripe_version: VERSION, user_agent: USER_AGENT.to_string(), client_id: None, stripe_account: None, diff --git a/src/resources/generated.rs b/src/resources/generated.rs index 8ab7608a9..24cce3a9c 100644 --- a/src/resources/generated.rs +++ b/src/resources/generated.rs @@ -45,6 +45,7 @@ pub mod core { pub mod tax_deducted_at_source; pub mod test_helpers_test_clock; pub mod token; + pub mod version; } #[path = "generated"] diff --git a/src/resources/generated/version.rs b/src/resources/generated/version.rs new file mode 100644 index 000000000..8a98fdbc5 --- /dev/null +++ b/src/resources/generated/version.rs @@ -0,0 +1,3 @@ +use crate::ApiVersion; + +pub const VERSION: ApiVersion = ApiVersion::V2022_11_15; diff --git a/src/resources/types.rs b/src/resources/types.rs index c7604b664..b0ccf607d 100644 --- a/src/resources/types.rs +++ b/src/resources/types.rs @@ -188,6 +188,10 @@ pub enum ApiVersion { V2019_09_09, #[serde(rename = "2020-08-27")] V2020_08_27, + #[serde(rename = "2022-08-01")] + V2022_08_01, + #[serde(rename = "2022-11-15")] + V2022_11_15, } impl ApiVersion { @@ -284,6 +288,8 @@ impl ApiVersion { ApiVersion::V2019_08_14 => "2019-08-14", ApiVersion::V2019_09_09 => "2019-09-09", ApiVersion::V2020_08_27 => "2020-08-27", + ApiVersion::V2022_08_01 => "2022-08-01", + ApiVersion::V2022_11_15 => "2022-11-15", } } }