From 1b3b032d2190607be48eb741008c3732c92cd7d0 Mon Sep 17 00:00:00 2001 From: Cyborus Date: Wed, 18 Dec 2024 09:57:15 -0500 Subject: fix: always use `Forgejo::with_user_agent` --- src/auth.rs | 9 +++++++-- src/keys.rs | 20 +++++++------------- src/main.rs | 9 +++++++++ src/version.rs | 8 ++++++-- 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/auth.rs b/src/auth.rs index 388e5ef..820917e 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -145,7 +145,11 @@ async fn oauth_login( } }; - let api = forgejo_api::Forgejo::new(forgejo_api::Auth::None, host.clone())?; + let api = forgejo_api::Forgejo::with_user_agent( + forgejo_api::Auth::None, + host.clone(), + crate::USER_AGENT, + )?; let request = forgejo_api::structs::OAuthTokenRequest::Public { client_id, code_verifier: &code_verifier, @@ -154,9 +158,10 @@ async fn oauth_login( }; let response = api.oauth_get_access_token(request).await?; - let api = forgejo_api::Forgejo::new( + let api = forgejo_api::Forgejo::with_user_agent( forgejo_api::Auth::OAuth2(&response.access_token), host.clone(), + crate::USER_AGENT, )?; let current_user = api.user_get_current().await?; let name = current_user diff --git a/src/keys.rs b/src/keys.rs index 7296793..8e6dc9f 100644 --- a/src/keys.rs +++ b/src/keys.rs @@ -54,7 +54,8 @@ impl KeyInfo { pub async fn get_api(&mut self, url: &Url) -> eyre::Result { match self.get_login(url) { Some(login) => login.api_for(url).await, - None => Forgejo::new(Auth::None, url.clone()).map_err(Into::into), + None => Forgejo::with_user_agent(Auth::None, url.clone(), crate::USER_AGENT) + .map_err(Into::into), } } @@ -74,15 +75,6 @@ impl KeyInfo { } } -pub const USER_AGENT: &str = concat!( - env!("CARGO_PKG_NAME"), - "/", - env!("CARGO_PKG_VERSION"), - " (", - env!("CARGO_PKG_REPOSITORY"), - ")" -); - #[derive(serde::Serialize, serde::Deserialize, Clone)] #[serde(tag = "type")] pub enum LoginInfo { @@ -109,7 +101,8 @@ impl LoginInfo { pub async fn api_for(&mut self, url: &Url) -> eyre::Result { match self { LoginInfo::Application { token, .. } => { - let api = Forgejo::with_user_agent(Auth::Token(token), url.clone(), USER_AGENT)?; + let api = + Forgejo::with_user_agent(Auth::Token(token), url.clone(), crate::USER_AGENT)?; Ok(api) } LoginInfo::OAuth { @@ -119,7 +112,7 @@ impl LoginInfo { .. } => { if time::OffsetDateTime::now_utc() >= *expires_at { - let api = Forgejo::with_user_agent(Auth::None, url.clone(), USER_AGENT)?; + let api = Forgejo::with_user_agent(Auth::None, url.clone(), crate::USER_AGENT)?; let (client_id, client_secret) = crate::auth::get_client_info_for(url) .ok_or_else(|| { eyre::eyre!("Can't refresh token; no client info for {url}. How did this happen?") @@ -140,7 +133,8 @@ impl LoginInfo { ); *expires_at = time::OffsetDateTime::now_utc() + expires_in; } - let api = Forgejo::with_user_agent(Auth::Token(token), url.clone(), USER_AGENT)?; + let api = + Forgejo::with_user_agent(Auth::Token(token), url.clone(), crate::USER_AGENT)?; Ok(api) } } diff --git a/src/main.rs b/src/main.rs index a9c6b36..9e52800 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,6 +17,15 @@ mod version; mod whoami; mod wiki; +pub const USER_AGENT: &str = concat!( + env!("CARGO_PKG_NAME"), + "/", + env!("CARGO_PKG_VERSION"), + " (", + env!("CARGO_PKG_REPOSITORY"), + ")" +); + #[derive(Parser, Debug)] pub struct App { #[clap(long, short = 'H')] diff --git a/src/version.rs b/src/version.rs index f3ab776..bd3b870 100644 --- a/src/version.rs +++ b/src/version.rs @@ -21,7 +21,7 @@ impl VersionCommand { pub async fn run(self) -> eyre::Result<()> { println!("{} v{}", env!("CARGO_BIN_NAME"), env!("CARGO_PKG_VERSION")); if self.verbose { - println!("user agent: {}", crate::keys::USER_AGENT); + println!("user agent: {}", crate::USER_AGENT); println!("build type: {BUILD_TYPE}"); println!(" target: {}", env!("BUILD_TARGET")); } @@ -36,7 +36,11 @@ impl VersionCommand { if self.check { let url = url::Url::parse("https://codeberg.org/")?; - let api = forgejo_api::Forgejo::new(forgejo_api::Auth::None, url)?; + let api = forgejo_api::Forgejo::with_user_agent( + forgejo_api::Auth::None, + url, + crate::USER_AGENT, + )?; let latest = api .repo_get_latest_release("Cyborus", "forgejo-cli") -- cgit v1.2.3