@@ -2,7 +2,7 @@ use crate::perms::oauth::SharedToken;
22use serde:: de:: DeserializeOwned ;
33use signet_tx_cache:: {
44 error:: Result ,
5- types:: { TxCacheBundle , TxCacheBundleResponse , TxCacheBundlesResponse } ,
5+ types:: { CacheObject , TxCacheBundle , TxCacheBundleResponse , TxCacheBundlesResponse } ,
66 TxCache ,
77} ;
88use tracing:: { instrument, warn} ;
@@ -35,13 +35,35 @@ impl std::ops::DerefMut for BuilderTxCache {
3535}
3636
3737impl 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 }
0 commit comments