Watch
0
0
Fork
You've already forked hyperhive
0

forge-client: take the token as a string, drop TokenSource and StaticToken

This commit is contained in:
atlas 2026-11-15 23:22:48 +01:00
commit 6d968c7fe6

View file

@ -1,7 +1,7 @@
//! Forgejo client construction, timeout policy and the drift-tolerant
//! notification thread, shared by every hyperhive forge consumer.
//!
//! Construction takes a base URL and a [`TokenSource`]; the bounds on how
//! Construction takes a base URL and the token as a string; the bounds on how
//! long a call may take come from a [`TimeoutPolicy`] the caller names.
mod notification;
@ -15,35 +15,6 @@ use forgejo_api::{Auth, Forgejo, ForgejoError};
pub use notification::{NotificationRepo, NotificationSubject, NotificationThread, parse_rfc3339};
/// Where a client's token comes from.
///
/// [`current`](TokenSource::current) is read each time a client is built. A
/// source whose token can change (re-read from a secret store after the
/// forge answers 401) rebuilds its client when `current()` returns a value
/// different from the one the client was built with; building is cheap and
/// pure, so a rebuild is a second call to [`async_client`].
pub trait TokenSource {
/// The token to send now.
fn current(&self) -> String;
}
/// A token that never changes.
#[derive(Clone)]
pub struct StaticToken(String);
impl StaticToken {
#[must_use]
pub fn new(token: impl Into<String>) -> Self {
Self(token.into())
}
}
impl TokenSource for StaticToken {
fn current(&self) -> String {
self.0.clone()
}
}
/// How long a caller lets forge calls take. Each caller picks one policy and
/// keeps it; the `forgejo_api` clients carry no timeout of their own, so a
/// bound exists only where the caller applies it from here.
@ -91,15 +62,15 @@ impl TimeoutPolicy {
}
}
/// The async typed client for the forge at `url`, sending `token`'s current
/// value. No timeout is set on it: bound each call with
/// The async typed client for the forge at `url`, sending `token`. No timeout
/// is set on it: bound each call with
/// [`TimeoutPolicy::bound_json`].
///
/// # Errors
///
/// `url` is not http(s), or the token is not ASCII.
pub fn async_client(url: url::Url, token: &impl TokenSource) -> Result<Forgejo, ForgejoError> {
Forgejo::new(Auth::Token(&token.current()), url)
pub fn async_client(url: url::Url, token: &str) -> Result<Forgejo, ForgejoError> {
Forgejo::new(Auth::Token(token), url)
}
/// The blocking typed client for the forge at `url`. No timeout is set on it.
@ -108,11 +79,8 @@ pub fn async_client(url: url::Url, token: &impl TokenSource) -> Result<Forgejo,
///
/// `url` is not http(s), or the token is not ASCII.
#[cfg(feature = "sync")]
pub fn sync_client(
url: url::Url,
token: &impl TokenSource,
) -> Result<forgejo_api::sync::Forgejo, ForgejoError> {
forgejo_api::sync::Forgejo::new(Auth::Token(&token.current()), url)
pub fn sync_client(url: url::Url, token: &str) -> Result<forgejo_api::sync::Forgejo, ForgejoError> {
forgejo_api::sync::Forgejo::new(Auth::Token(token), url)
}
/// Why [`raw_blocking_client`] failed.
@ -155,14 +123,14 @@ impl std::error::Error for RawClientError {
/// client cannot be built.
#[cfg(feature = "sync")]
pub fn raw_blocking_client(
token: &impl TokenSource,
token: &str,
policy: &TimeoutPolicy,
) -> Result<reqwest::blocking::Client, RawClientError> {
use reqwest::header::{ACCEPT, AUTHORIZATION, HeaderMap, HeaderValue};
let mut headers = HeaderMap::new();
let mut auth = HeaderValue::from_str(&format!("token {}", token.current()))
.map_err(RawClientError::AuthHeader)?;
let mut auth =
HeaderValue::from_str(&format!("token {token}")).map_err(RawClientError::AuthHeader)?;
auth.set_sensitive(true);
headers.insert(AUTHORIZATION, auth);
headers.insert(ACCEPT, HeaderValue::from_static("application/json"));
@ -197,18 +165,18 @@ mod tests {
}
#[test]
fn async_client_sends_the_token_source_value_and_rejects_non_http() {
fn async_client_rejects_a_non_http_url() {
let ok = url::Url::parse("http://forge.example/").unwrap();
assert!(async_client(ok, &StaticToken::new("t")).is_ok());
assert!(async_client(ok, "t").is_ok());
let bad = url::Url::parse("ftp://forge.example/").unwrap();
assert!(async_client(bad, &StaticToken::new("t")).is_err());
assert!(async_client(bad, "t").is_err());
}
#[cfg(feature = "sync")]
#[test]
fn raw_client_builds_and_rejects_an_unsendable_token() {
assert!(raw_blocking_client(&StaticToken::new("t"), &TimeoutPolicy::CLI).is_ok());
let err = raw_blocking_client(&StaticToken::new("a\nb"), &TimeoutPolicy::CLI).unwrap_err();
assert!(raw_blocking_client("t", &TimeoutPolicy::CLI).is_ok());
let err = raw_blocking_client("a\nb", &TimeoutPolicy::CLI).unwrap_err();
assert!(matches!(err, RawClientError::AuthHeader(_)));
}
}