Skip to content

Commit 2f9ec68

Browse files
authored
fix: use error for status (#104)
1 parent def647a commit 2f9ec68

File tree

4 files changed

+61
-11
lines changed

4 files changed

+61
-11
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "init4-bin-base"
33

44
description = "Internal utilities for binaries produced by the init4 team"
55
keywords = ["init4", "bin", "base"]
6-
version = "0.18.0-rc.3"
6+
version = "0.18.0-rc.4"
77
edition = "2021"
88
rust-version = "1.85"
99
authors = ["init4", "James Prestwich", "evalir"]

src/perms/oauth.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,14 @@ impl SharedToken {
289289
}
290290
}
291291

292+
#[doc(hidden)]
293+
impl SharedToken {
294+
/// Create an empty `SharedToken` that will never be authenticated.
295+
pub fn empty() -> Self {
296+
Self(watch::channel(None).1)
297+
}
298+
}
299+
292300
/// A reference to token data, contained in a [`SharedToken`].
293301
///
294302
/// This is implemented using [`watch::Ref`], and as a result holds a lock on

src/perms/tx_cache.rs

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::perms::oauth::SharedToken;
22
use serde::de::DeserializeOwned;
33
use signet_tx_cache::{
44
error::Result,
5-
types::{TxCacheBundle, TxCacheBundleResponse, TxCacheBundlesResponse},
5+
types::{CacheObject, TxCacheBundle, TxCacheBundleResponse, TxCacheBundlesResponse},
66
TxCache,
77
};
88
use tracing::{instrument, warn};
@@ -35,13 +35,35 @@ impl std::ops::DerefMut for BuilderTxCache {
3535
}
3636

3737
impl BuilderTxCache {
38-
/// Create a new `TxCacheClient` with the given transaction cache and shared token.
39-
pub const fn new(tx_cache: TxCache, token: SharedToken) -> Self {
40-
Self { tx_cache, token }
38+
/// Instantiate with the given transaction cache and shared token.
39+
pub fn new(url: reqwest::Url, token: SharedToken) -> Self {
40+
Self {
41+
tx_cache: TxCache::new(url),
42+
token,
43+
}
44+
}
45+
46+
/// Instantiate from a string URL and shared token.
47+
pub fn new_from_string(url: &str, token: SharedToken) -> Result<Self> {
48+
let tx_cache = TxCache::new_from_string(url)?;
49+
Ok(Self { tx_cache, token })
50+
}
51+
52+
/// Instantiate with the given transaction cache and shared token, using
53+
/// a specific reqwest client.
54+
pub const fn new_with_client(
55+
url: reqwest::Url,
56+
client: reqwest::Client,
57+
token: SharedToken,
58+
) -> Self {
59+
Self {
60+
tx_cache: TxCache::new_with_client(url, client),
61+
token,
62+
}
4163
}
4264

4365
/// Get a reference to the transaction cache client.
44-
pub const fn tx_cache(&self) -> &TxCache {
66+
pub const fn inner(&self) -> &TxCache {
4567
&self.tx_cache
4668
}
4769

@@ -50,7 +72,10 @@ impl BuilderTxCache {
5072
&self.token
5173
}
5274

53-
async fn get_inner_with_token<T: DeserializeOwned>(&self, join: &str) -> Result<T> {
75+
async fn get_inner_with_token<T>(&self, join: &str, query: Option<T::Key>) -> Result<T>
76+
where
77+
T: DeserializeOwned + CacheObject,
78+
{
5479
let url = self.tx_cache.url().join(join)?;
5580
let secret = self.token.secret().await.unwrap_or_else(|_| {
5681
warn!("Failed to get token secret");
@@ -60,10 +85,11 @@ impl BuilderTxCache {
6085
self.tx_cache
6186
.client()
6287
.get(url)
88+
.query(&query)
6389
.bearer_auth(secret)
6490
.send()
65-
.await
66-
.inspect_err(|e| warn!(%e, "Failed to get object from transaction cache"))?
91+
.await?
92+
.error_for_status()?
6793
.json::<T>()
6894
.await
6995
.map_err(Into::into)
@@ -72,7 +98,7 @@ impl BuilderTxCache {
7298
/// Get bundles from the cache.
7399
#[instrument(skip_all)]
74100
pub async fn get_bundles(&self) -> Result<Vec<TxCacheBundle>> {
75-
self.get_inner_with_token::<TxCacheBundlesResponse>(BUNDLES)
101+
self.get_inner_with_token::<TxCacheBundlesResponse>(BUNDLES, None)
76102
.await
77103
.map(|response| response.bundles)
78104
}
@@ -86,7 +112,7 @@ impl BuilderTxCache {
86112
#[instrument(skip_all)]
87113
pub async fn get_bundle(&self, bundle_id: &str) -> Result<TxCacheBundle> {
88114
let url = self.get_bundle_url_path(bundle_id);
89-
self.get_inner_with_token::<TxCacheBundleResponse>(&url)
115+
self.get_inner_with_token::<TxCacheBundleResponse>(&url, None)
90116
.await
91117
.map(|response| response.bundle)
92118
}

tests/tx-cache.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![cfg(feature = "perms")]
2+
3+
use init4_bin_base::perms::{tx_cache::BuilderTxCache, SharedToken};
4+
use signet_tx_cache::TxCacheError;
5+
6+
const URL: &str = "https://transactions.parmigiana.signet.sh";
7+
8+
#[ignore = "integration"]
9+
#[tokio::test]
10+
async fn test_tx_cache_get_bundles() {
11+
let client = BuilderTxCache::new_from_string(URL, SharedToken::empty()).unwrap();
12+
13+
let bundles = client.get_bundles().await.unwrap_err();
14+
15+
assert!(matches!(bundles, TxCacheError::NotOurSlot));
16+
}

0 commit comments

Comments
 (0)