summaryrefslogtreecommitdiffstats
path: root/src/keys.rs
diff options
context:
space:
mode:
authorCyborus <cyborus@noreply.codeberg.org>2024-10-25 18:17:41 +0200
committerCyborus <cyborus@noreply.codeberg.org>2024-10-25 18:17:41 +0200
commitf67260e548c45c01f89850e0e38dc2d2fb4a83cf (patch)
treedb664e48fc60b29add9cab6120893bbb18f50438 /src/keys.rs
parentMerge pull request 'add `repo readme`' (#134) from repo-readme into main (diff)
parentfeat: add user agent (diff)
downloadforgejo-cli-f67260e548c45c01f89850e0e38dc2d2fb4a83cf.tar.xz
forgejo-cli-f67260e548c45c01f89850e0e38dc2d2fb4a83cf.zip
Merge pull request 'add user agent' (#135) from user-agent into main
Reviewed-on: https://codeberg.org/Cyborus/forgejo-cli/pulls/135
Diffstat (limited to 'src/keys.rs')
-rw-r--r--src/keys.rs15
1 files changed, 12 insertions, 3 deletions
diff --git a/src/keys.rs b/src/keys.rs
index 5239df6..7296793 100644
--- a/src/keys.rs
+++ b/src/keys.rs
@@ -74,6 +74,15 @@ 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 {
@@ -100,7 +109,7 @@ impl LoginInfo {
pub async fn api_for(&mut self, url: &Url) -> eyre::Result<Forgejo> {
match self {
LoginInfo::Application { token, .. } => {
- let api = Forgejo::new(Auth::Token(token), url.clone())?;
+ let api = Forgejo::with_user_agent(Auth::Token(token), url.clone(), USER_AGENT)?;
Ok(api)
}
LoginInfo::OAuth {
@@ -110,7 +119,7 @@ impl LoginInfo {
..
} => {
if time::OffsetDateTime::now_utc() >= *expires_at {
- let api = Forgejo::new(Auth::None, url.clone())?;
+ let api = Forgejo::with_user_agent(Auth::None, url.clone(), 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?")
@@ -131,7 +140,7 @@ impl LoginInfo {
);
*expires_at = time::OffsetDateTime::now_utc() + expires_in;
}
- let api = Forgejo::new(Auth::Token(token), url.clone())?;
+ let api = Forgejo::with_user_agent(Auth::Token(token), url.clone(), USER_AGENT)?;
Ok(api)
}
}