|
| 1 | +use crate::perms::oauth::SharedToken; |
| 2 | +use eyre::{bail, Result}; |
| 3 | +use oauth2::TokenResponse; |
| 4 | +use serde::de::DeserializeOwned; |
| 5 | +use signet_tx_cache::{ |
| 6 | + client::TxCache, |
| 7 | + types::{TxCacheBundle, TxCacheBundleResponse, TxCacheBundlesResponse}, |
| 8 | +}; |
| 9 | +use tracing::{instrument, warn}; |
| 10 | + |
| 11 | +const BUNDLES: &str = "bundles"; |
| 12 | + |
| 13 | +/// A client for interacting with the transaction cache, a thin wrapper around |
| 14 | +/// the [`TxCache`] and [`SharedToken`] that implements the necessary methods |
| 15 | +/// to fetch bundles and bundle details. |
| 16 | +#[derive(Debug, Clone)] |
| 17 | +pub struct BuilderTxCache { |
| 18 | + /// The transaction cache client. |
| 19 | + tx_cache: TxCache, |
| 20 | + /// The shared token for authentication. |
| 21 | + token: SharedToken, |
| 22 | +} |
| 23 | + |
| 24 | +impl std::ops::Deref for BuilderTxCache { |
| 25 | + type Target = TxCache; |
| 26 | + |
| 27 | + fn deref(&self) -> &Self::Target { |
| 28 | + &self.tx_cache |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +impl std::ops::DerefMut for BuilderTxCache { |
| 33 | + fn deref_mut(&mut self) -> &mut Self::Target { |
| 34 | + &mut self.tx_cache |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +impl BuilderTxCache { |
| 39 | + /// Create a new `TxCacheClient` with the given transaction cache and shared token. |
| 40 | + pub const fn new(tx_cache: TxCache, token: SharedToken) -> Self { |
| 41 | + Self { tx_cache, token } |
| 42 | + } |
| 43 | + |
| 44 | + /// Get a reference to the transaction cache client. |
| 45 | + pub const fn tx_cache(&self) -> &TxCache { |
| 46 | + &self.tx_cache |
| 47 | + } |
| 48 | + |
| 49 | + /// Get a reference to the shared token. |
| 50 | + pub const fn token(&self) -> &SharedToken { |
| 51 | + &self.token |
| 52 | + } |
| 53 | + |
| 54 | + async fn get_inner_with_token<T: DeserializeOwned>(&self, join: &str) -> Result<T> { |
| 55 | + let url = self.tx_cache.url().join(join)?; |
| 56 | + let Some(token) = self.token.read() else { |
| 57 | + bail!("No token available for authentication"); |
| 58 | + }; |
| 59 | + |
| 60 | + self.tx_cache |
| 61 | + .client() |
| 62 | + .get(url) |
| 63 | + .bearer_auth(token.access_token().secret()) |
| 64 | + .send() |
| 65 | + .await |
| 66 | + .inspect_err(|e| warn!(%e, "Failed to get object from transaction cache"))? |
| 67 | + .json::<T>() |
| 68 | + .await |
| 69 | + .map_err(Into::into) |
| 70 | + } |
| 71 | + |
| 72 | + /// Get bundles from the cache. |
| 73 | + #[instrument(skip_all)] |
| 74 | + pub async fn get_bundles(&self) -> Result<Vec<TxCacheBundle>> { |
| 75 | + self.get_inner_with_token::<TxCacheBundlesResponse>(BUNDLES) |
| 76 | + .await |
| 77 | + .map(|response| response.bundles) |
| 78 | + } |
| 79 | + |
| 80 | + fn get_bundle_url_path(&self, bundle_id: &str) -> String { |
| 81 | + format!("{BUNDLES}/{bundle_id}") |
| 82 | + } |
| 83 | + |
| 84 | + /// Get a bundle from the cache by its UUID. For convenience, this method |
| 85 | + /// takes a string reference, which is expected to be a valid UUID. |
| 86 | + #[instrument(skip_all)] |
| 87 | + pub async fn get_bundle(&self, bundle_id: &str) -> Result<TxCacheBundle> { |
| 88 | + let url = self.get_bundle_url_path(bundle_id); |
| 89 | + self.get_inner_with_token::<TxCacheBundleResponse>(&url) |
| 90 | + .await |
| 91 | + .map(|response| response.bundle) |
| 92 | + } |
| 93 | +} |
0 commit comments