|
| 1 | +use reqwest::StatusCode; |
| 2 | +use serde::de::DeserializeOwned; |
| 3 | +use stats_proto::blockscout::stats::v1 as proto_v1; |
| 4 | +use thiserror::Error; |
| 5 | +use url::{ParseError, Url}; |
| 6 | + |
| 7 | +use crate::settings::LinkedStatsSettings; |
| 8 | + |
| 9 | +pub const LINK_HOP_HEADER: &str = "x-stats-link-hop"; |
| 10 | + |
| 11 | +#[derive(Debug, Clone)] |
| 12 | +pub struct LinkedStatsClient { |
| 13 | + client: reqwest::Client, |
| 14 | + base_url: Url, |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(Debug, Error)] |
| 18 | +pub enum LinkedStatsError { |
| 19 | + #[error("linked stats service returned not found")] |
| 20 | + NotFound, |
| 21 | + #[error("linked stats request failed: {0}")] |
| 22 | + Request(#[from] reqwest::Error), |
| 23 | + #[error("linked stats returned unexpected status {0}")] |
| 24 | + UnexpectedStatus(StatusCode), |
| 25 | + #[error("failed to construct linked stats URL: {0}")] |
| 26 | + InvalidUrl(#[from] ParseError), |
| 27 | +} |
| 28 | + |
| 29 | +impl LinkedStatsClient { |
| 30 | + /// Returns `None` when [`LinkedStatsSettings::base_url`] is not set. |
| 31 | + pub fn try_new(settings: &LinkedStatsSettings) -> Result<Option<Self>, reqwest::Error> { |
| 32 | + let Some(base_url) = settings.base_url.clone() else { |
| 33 | + return Ok(None); |
| 34 | + }; |
| 35 | + let base_url = normalize_base_url(base_url); |
| 36 | + let client = reqwest::Client::builder() |
| 37 | + .timeout(settings.timeout()) |
| 38 | + .build()?; |
| 39 | + Ok(Some(Self { client, base_url })) |
| 40 | + } |
| 41 | + |
| 42 | + pub async fn get_counters(&self, hop: u32) -> Result<proto_v1::Counters, LinkedStatsError> { |
| 43 | + self.get_json("api/v1/counters", hop).await |
| 44 | + } |
| 45 | + |
| 46 | + pub async fn get_line_charts( |
| 47 | + &self, |
| 48 | + hop: u32, |
| 49 | + ) -> Result<proto_v1::LineCharts, LinkedStatsError> { |
| 50 | + self.get_json("api/v1/lines", hop).await |
| 51 | + } |
| 52 | + |
| 53 | + pub async fn get_line_chart( |
| 54 | + &self, |
| 55 | + request: &proto_v1::GetLineChartRequest, |
| 56 | + hop: u32, |
| 57 | + ) -> Result<proto_v1::LineChart, LinkedStatsError> { |
| 58 | + let mut url = self.endpoint_with_path_segments(["api", "v1", "lines", &request.name])?; |
| 59 | + { |
| 60 | + let mut query = url.query_pairs_mut(); |
| 61 | + if let Some(from) = request.from.as_deref() { |
| 62 | + query.append_pair("from", from); |
| 63 | + } |
| 64 | + if let Some(to) = request.to.as_deref() { |
| 65 | + query.append_pair("to", to); |
| 66 | + } |
| 67 | + let resolution = request.resolution(); |
| 68 | + if resolution != proto_v1::Resolution::Unspecified { |
| 69 | + query.append_pair("resolution", resolution.as_str_name()); |
| 70 | + } |
| 71 | + } |
| 72 | + self.get_json_by_url(url, hop, true).await |
| 73 | + } |
| 74 | + |
| 75 | + pub async fn get_main_page_stats( |
| 76 | + &self, |
| 77 | + hop: u32, |
| 78 | + ) -> Result<proto_v1::MainPageStats, LinkedStatsError> { |
| 79 | + self.get_json("api/v1/pages/main", hop).await |
| 80 | + } |
| 81 | + |
| 82 | + pub async fn get_transactions_page_stats( |
| 83 | + &self, |
| 84 | + hop: u32, |
| 85 | + ) -> Result<proto_v1::TransactionsPageStats, LinkedStatsError> { |
| 86 | + self.get_json("api/v1/pages/transactions", hop).await |
| 87 | + } |
| 88 | + |
| 89 | + pub async fn get_contracts_page_stats( |
| 90 | + &self, |
| 91 | + hop: u32, |
| 92 | + ) -> Result<proto_v1::ContractsPageStats, LinkedStatsError> { |
| 93 | + self.get_json("api/v1/pages/contracts", hop).await |
| 94 | + } |
| 95 | + |
| 96 | + pub async fn get_main_page_multichain_stats( |
| 97 | + &self, |
| 98 | + hop: u32, |
| 99 | + ) -> Result<proto_v1::MainPageMultichainStats, LinkedStatsError> { |
| 100 | + self.get_json("api/v1/pages/multichain/main", hop).await |
| 101 | + } |
| 102 | + |
| 103 | + pub async fn get_main_page_interchain_stats( |
| 104 | + &self, |
| 105 | + hop: u32, |
| 106 | + ) -> Result<proto_v1::MainPageInterchainStats, LinkedStatsError> { |
| 107 | + self.get_json("api/v1/pages/interchain/main", hop).await |
| 108 | + } |
| 109 | + |
| 110 | + pub async fn get_update_status( |
| 111 | + &self, |
| 112 | + hop: u32, |
| 113 | + ) -> Result<proto_v1::UpdateStatus, LinkedStatsError> { |
| 114 | + self.get_json("api/v1/update-status", hop).await |
| 115 | + } |
| 116 | + |
| 117 | + fn endpoint(&self, path: &str) -> Result<Url, LinkedStatsError> { |
| 118 | + Ok(self.base_url.join(path)?) |
| 119 | + } |
| 120 | + |
| 121 | + fn endpoint_with_path_segments<'a>( |
| 122 | + &self, |
| 123 | + segments: impl IntoIterator<Item = &'a str>, |
| 124 | + ) -> Result<Url, LinkedStatsError> { |
| 125 | + build_url_with_path_segments(self.base_url.clone(), segments) |
| 126 | + } |
| 127 | + |
| 128 | + async fn get_json<T: DeserializeOwned>( |
| 129 | + &self, |
| 130 | + path: &str, |
| 131 | + hop: u32, |
| 132 | + ) -> Result<T, LinkedStatsError> { |
| 133 | + let url = self.endpoint(path)?; |
| 134 | + self.get_json_by_url(url, hop, false).await |
| 135 | + } |
| 136 | + |
| 137 | + async fn get_json_by_url<T: DeserializeOwned>( |
| 138 | + &self, |
| 139 | + url: Url, |
| 140 | + hop: u32, |
| 141 | + allow_not_found: bool, |
| 142 | + ) -> Result<T, LinkedStatsError> { |
| 143 | + let response = self |
| 144 | + .client |
| 145 | + .get(url) |
| 146 | + .header(LINK_HOP_HEADER, hop.to_string()) |
| 147 | + .send() |
| 148 | + .await?; |
| 149 | + let status = response.status(); |
| 150 | + if status.is_success() { |
| 151 | + return Ok(response.json().await?); |
| 152 | + } |
| 153 | + if allow_not_found && status == StatusCode::NOT_FOUND { |
| 154 | + return Err(LinkedStatsError::NotFound); |
| 155 | + } |
| 156 | + Err(LinkedStatsError::UnexpectedStatus(status)) |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +fn normalize_base_url(mut base_url: Url) -> Url { |
| 161 | + let trimmed_path = base_url.path().trim_end_matches('/'); |
| 162 | + let normalized_path = if trimmed_path.is_empty() { |
| 163 | + "/".to_string() |
| 164 | + } else { |
| 165 | + format!("{trimmed_path}/") |
| 166 | + }; |
| 167 | + base_url.set_path(&normalized_path); |
| 168 | + base_url |
| 169 | +} |
| 170 | + |
| 171 | +fn build_url_with_path_segments<'a>( |
| 172 | + mut base_url: Url, |
| 173 | + segments: impl IntoIterator<Item = &'a str>, |
| 174 | +) -> Result<Url, LinkedStatsError> { |
| 175 | + let mut path_segments = base_url |
| 176 | + .path_segments_mut() |
| 177 | + .map_err(|_| ParseError::RelativeUrlWithoutBase)?; |
| 178 | + path_segments.pop_if_empty(); |
| 179 | + path_segments.extend(segments); |
| 180 | + drop(path_segments); |
| 181 | + Ok(base_url) |
| 182 | +} |
0 commit comments