From eb5c85d241651573a26614d0923a8d3050fd4c1e Mon Sep 17 00:00:00 2001 From: James Date: Wed, 31 Dec 2025 11:43:55 -0500 Subject: [PATCH] fix: use error for status --- src/perms/oauth.rs | 8 ++++++++ src/perms/tx_cache.rs | 46 +++++++++++++++++++++++++++++++++---------- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/perms/oauth.rs b/src/perms/oauth.rs index 2a2b60c..70017cf 100644 --- a/src/perms/oauth.rs +++ b/src/perms/oauth.rs @@ -289,6 +289,14 @@ impl SharedToken { } } +#[doc(hidden)] +impl SharedToken { + /// Create an empty `SharedToken` that will never be authenticated. + pub fn empty() -> Self { + Self(watch::channel(None).1) + } +} + /// A reference to token data, contained in a [`SharedToken`]. /// /// This is implemented using [`watch::Ref`], and as a result holds a lock on diff --git a/src/perms/tx_cache.rs b/src/perms/tx_cache.rs index 51899b8..351428c 100644 --- a/src/perms/tx_cache.rs +++ b/src/perms/tx_cache.rs @@ -2,7 +2,7 @@ use crate::perms::oauth::SharedToken; use serde::de::DeserializeOwned; use signet_tx_cache::{ error::Result, - types::{TxCacheBundle, TxCacheBundleResponse, TxCacheBundlesResponse}, + types::{CacheObject, TxCacheBundle, TxCacheBundleResponse, TxCacheBundlesResponse}, TxCache, }; use tracing::{instrument, warn}; @@ -35,13 +35,35 @@ impl std::ops::DerefMut for BuilderTxCache { } impl BuilderTxCache { - /// Create a new `TxCacheClient` with the given transaction cache and shared token. - pub const fn new(tx_cache: TxCache, token: SharedToken) -> Self { - Self { tx_cache, token } + /// Instantiate with the given transaction cache and shared token. + pub fn new(url: reqwest::Url, token: SharedToken) -> Self { + Self { + tx_cache: TxCache::new(url), + token, + } + } + + /// Instantiate from a string URL and shared token. + pub fn new_from_string(url: &str, token: SharedToken) -> Result { + let tx_cache = TxCache::new_from_string(url)?; + Ok(Self { tx_cache, token }) + } + + /// Instantiate with the given transaction cache and shared token, using + /// a specific reqwest client. + pub const fn new_with_client( + url: reqwest::Url, + client: reqwest::Client, + token: SharedToken, + ) -> Self { + Self { + tx_cache: TxCache::new_with_client(url, client), + token, + } } /// Get a reference to the transaction cache client. - pub const fn tx_cache(&self) -> &TxCache { + pub const fn inner(&self) -> &TxCache { &self.tx_cache } @@ -50,7 +72,10 @@ impl BuilderTxCache { &self.token } - async fn get_inner_with_token(&self, join: &str) -> Result { + async fn get_inner_with_token(&self, join: &str, query: Option) -> Result + where + T: DeserializeOwned + CacheObject, + { let url = self.tx_cache.url().join(join)?; let secret = self.token.secret().await.unwrap_or_else(|_| { warn!("Failed to get token secret"); @@ -60,10 +85,11 @@ impl BuilderTxCache { self.tx_cache .client() .get(url) + .query(&query) .bearer_auth(secret) .send() - .await - .inspect_err(|e| warn!(%e, "Failed to get object from transaction cache"))? + .await? + .error_for_status()? .json::() .await .map_err(Into::into) @@ -72,7 +98,7 @@ impl BuilderTxCache { /// Get bundles from the cache. #[instrument(skip_all)] pub async fn get_bundles(&self) -> Result> { - self.get_inner_with_token::(BUNDLES) + self.get_inner_with_token::(BUNDLES, None) .await .map(|response| response.bundles) } @@ -86,7 +112,7 @@ impl BuilderTxCache { #[instrument(skip_all)] pub async fn get_bundle(&self, bundle_id: &str) -> Result { let url = self.get_bundle_url_path(bundle_id); - self.get_inner_with_token::(&url) + self.get_inner_with_token::(&url, None) .await .map(|response| response.bundle) }