Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: generate api version from the codegen #361

Merged
merged 1 commit into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions openapi/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions openapi/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,20 @@ impl<'a> Metadata<'a> {
write(&out_path.as_ref().join("placeholders.rs"), out.as_bytes()).unwrap();
}

pub fn write_version<T>(&self, out_path: T)
where
T: AsRef<Path>,
{
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<FileGenerator> {
self.objects
Expand Down
4 changes: 4 additions & 0 deletions openapi/src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`!")
}
Expand Down
5 changes: 3 additions & 2 deletions src/client/stripe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/resources/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
3 changes: 3 additions & 0 deletions src/resources/generated/version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
use crate::ApiVersion;

pub const VERSION: ApiVersion = ApiVersion::V2022_11_15;
6 changes: 6 additions & 0 deletions src/resources/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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",
}
}
}
Expand Down