#![allow(unused_imports)]
mod client;
mod common;
mod error;
mod feature;
mod meminfos;
mod response;
mod rusages;
mod self_proc_stats;
mod stream;
mod summary;
mod system_proc_stats;
mod vhost;
pub use error::SrsClientError;
pub use response::{SrsClientResp, SrsClientRespData};
use reqwest::{Client, Response as ReqwestResponse};
use url::Url;
#[derive(Clone, Debug)]
pub struct SrsClient {
http_client: Client,
base_url: Url,
}
impl SrsClient {
pub fn build<S: Into<String>>(base_url: S) -> Result<Self, SrsClientError> {
let base_url = Url::parse(&base_url.into())
.and_then(|url| url.join("/api/v1/"))
.map_err(SrsClientError::IncorrectBaseUrl)?;
tracing::debug!("base_url: {base_url}");
Ok(Self {
http_client: Client::new(),
base_url,
})
}
async fn get(&self, url: &str) -> Result<ReqwestResponse, SrsClientError> {
self.http_client
.get(
self.base_url
.join(url)
.map_err(SrsClientError::IncorrectApiUrl)?,
)
.send()
.await
.map_err(SrsClientError::RequestFailed)
}
async fn delete(&self, url: &str) -> Result<ReqwestResponse, SrsClientError> {
self.http_client
.delete(
self.base_url
.join(url)
.map_err(SrsClientError::IncorrectApiUrl)?,
)
.send()
.await
.map_err(SrsClientError::RequestFailed)
}
async fn process_resp(&self, resp: ReqwestResponse) -> Result<SrsClientResp, SrsClientError> {
if !resp.status().is_success() {
return Err(SrsClientError::BadStatus(resp.status()));
}
tracing::debug!("processing request to: {}", resp.url());
let resp = resp
.json::<SrsClientResp>()
.await
.map_err(SrsClientError::DeserializeError)?;
Ok(resp)
}
pub async fn kickoff_client<T: Into<String>>(
self,
id: T,
) -> Result<SrsClientResp, SrsClientError> {
let resp = self.delete(&format!("clients/{}/", id.into())).await?;
self.process_resp(resp).await
}
pub async fn get_version(self) -> Result<SrsClientResp, SrsClientError> {
let resp = self.get("versions").await?;
self.process_resp(resp).await
}
pub async fn get_vhosts(self) -> Result<SrsClientResp, SrsClientError> {
let resp = self.get("vhosts").await?;
self.process_resp(resp).await
}
pub async fn get_streams(self) -> Result<SrsClientResp, SrsClientError> {
let resp = self.get("streams").await?;
self.process_resp(resp).await
}
pub async fn get_clients(self) -> Result<SrsClientResp, SrsClientError> {
let resp = self.get("clients").await?;
self.process_resp(resp).await
}
pub async fn get_features(self) -> Result<SrsClientResp, SrsClientError> {
let resp = self.get("features").await?;
self.process_resp(resp).await
}
pub async fn get_rusages(self) -> Result<SrsClientResp, SrsClientError> {
let resp = self.get("rusages").await?;
self.process_resp(resp).await
}
pub async fn get_self_proc_stats(self) -> Result<SrsClientResp, SrsClientError> {
let resp = self.get("self_proc_stats").await?;
self.process_resp(resp).await
}
pub async fn get_system_proc_stats(self) -> Result<SrsClientResp, SrsClientError> {
let resp = self.get("system_proc_stats").await?;
self.process_resp(resp).await
}
pub async fn get_meminfos(self) -> Result<SrsClientResp, SrsClientError> {
let resp = self.get("meminfos").await?;
self.process_resp(resp).await
}
}