mirror of
https://codeberg.org/Cyborus/forgejo-cli.git
synced 2025-03-14 21:25:54 +01:00
fix: always use Forgejo::with_user_agent
This commit is contained in:
parent
f51a7093b9
commit
1b3b032d21
4 changed files with 29 additions and 17 deletions
|
@ -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
|
||||
|
|
20
src/keys.rs
20
src/keys.rs
|
@ -54,7 +54,8 @@ impl KeyInfo {
|
|||
pub async fn get_api(&mut self, url: &Url) -> eyre::Result<Forgejo> {
|
||||
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<Forgejo> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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')]
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Add table
Reference in a new issue