sponsor_block/client/user/
api_status.rs

1// Uses
2use core::time::Duration;
3
4use chrono::{serde::ts_milliseconds, DateTime, Utc};
5use serde::Deserialize;
6use serde_json::from_str as from_json_str;
7
8use crate::{
9	error::Result,
10	util::{
11		de::{duration_from_millis_str, duration_from_seconds_str},
12		get_response_text,
13	},
14	Client,
15};
16
17/// The results of an API status request.
18#[derive(Deserialize, Debug)]
19#[serde(default)]
20pub struct ApiStatus {
21	/// The server process uptime.
22	#[serde(deserialize_with = "duration_from_seconds_str")]
23	pub uptime: Duration,
24	/// The SHA hash of the most recent commit the server is running.
25	pub commit: String,
26	/// The version of the database.
27	#[serde(rename = "db")]
28	pub db_version: u32,
29	/// The date and time when the request was received.
30	#[serde(rename = "startTime", with = "ts_milliseconds")]
31	pub request_start_time: DateTime<Utc>,
32	/// The time that it took the API to send it's reply.
33	#[serde(rename = "processTime", deserialize_with = "duration_from_millis_str")]
34	pub request_time_taken: Duration,
35	/// The load average for the server. The first entry is the average for 5
36	/// minutes, and the second is for 15 minutes.
37	///
38	/// If you want more information about the source of this information, visit
39	/// <https://github.com/ajayyy/SponsorBlockServer/blob/06af78c770b82722be8b03d2b1b82eb7409f675b/src/routes/getStatus.ts#L18>
40	#[serde(rename = "loadavg")]
41	pub load_average: [f32; 2],
42}
43
44impl Default for ApiStatus {
45	fn default() -> Self {
46		Self {
47			uptime: Duration::default(),
48			commit: String::default(),
49			db_version: u32::default(),
50			request_start_time: Utc::now(),
51			request_time_taken: Duration::default(),
52			load_average: Default::default(),
53		}
54	}
55}
56
57// Function Implementation
58impl Client {
59	/// Fetches the API status.
60	///
61	/// # Errors
62	/// Can return pretty much any error type from [`SponsorBlockError`]. See
63	/// the error type definitions for explanations of when they might be
64	/// encountered.
65	///
66	/// [`SponsorBlockError`]: crate::SponsorBlockError
67	pub async fn fetch_api_status(&self) -> Result<ApiStatus> {
68		// Function Constants
69		const API_ENDPOINT: &str = "/status";
70
71		// Build the request
72		let request = self.http.get(format!("{}{}", &self.base_url, API_ENDPOINT));
73
74		// Send the request
75		let response = get_response_text(request.send().await?).await?;
76
77		// Parse the response
78		Ok(from_json_str::<ApiStatus>(response.as_str())?)
79	}
80}