use std::sync::Arc;
use serde_json::Value;
use crate::envelope::Envelope;
use crate::error::Error;
use crate::params;
use crate::routes;
use crate::transport::{RequestBody, Transport};
const INSIGHTS_CADENCES: &[&str] = &["hourly", "daily"];
#[derive(Debug)]
pub struct InsightsApi {
transport: Arc<Transport>,
}
impl InsightsApi {
#[must_use]
pub fn new(transport: Arc<Transport>) -> Self {
Self { transport }
}
pub async fn get_insights(
&self,
network_id: &str,
start: &str,
end: &str,
insight_type: &str,
cadence: &str,
) -> Result<Envelope, Error> {
let cadence = params::validate_cadence(cadence, INSIGHTS_CADENCES)?;
self.transport
.resource(
&routes::insights::V8_GET_INSIGHTS,
network_id,
None,
&[
("start", start.to_owned()),
("end", end.to_owned()),
("cadence", cadence.to_owned()),
("insight_type", insight_type.to_owned()),
],
RequestBody::None,
)
.await
}
pub async fn get_devices_insights(
&self,
network_id: &str,
start: &str,
end: &str,
cadence: &str,
insight_type: &str,
parent: Option<&Value>,
) -> Result<Envelope, Error> {
let cadence = params::validate_cadence(cadence, params::CADENCE_VALUES)?;
self.transport
.resource(
&routes::insights::GET_DEVICES_INSIGHTS,
network_id,
parent,
&[
("start", start.to_owned()),
("end", end.to_owned()),
("cadence", cadence.to_owned()),
("insight_type", insight_type.to_owned()),
],
RequestBody::None,
)
.await
}
pub async fn get_device_insights(
&self,
network_id: &str,
mac: &str,
start: &str,
end: &str,
cadence: &str,
insight_type: &str,
) -> Result<Envelope, Error> {
let cadence = params::validate_cadence(cadence, params::CADENCE_VALUES)?;
self.transport
.nested(
&routes::insights::GET_DEVICE_INSIGHTS,
network_id,
mac,
None,
&[
("start", start.to_owned()),
("end", end.to_owned()),
("cadence", cadence.to_owned()),
("insight_type", insight_type.to_owned()),
],
RequestBody::None,
)
.await
}
pub async fn get_profiles_insights(
&self,
network_id: &str,
start: &str,
end: &str,
cadence: &str,
insight_type: &str,
parent: Option<&Value>,
) -> Result<Envelope, Error> {
let cadence = params::validate_cadence(cadence, params::CADENCE_VALUES)?;
self.transport
.resource(
&routes::insights::GET_PROFILES_INSIGHTS,
network_id,
parent,
&[
("start", start.to_owned()),
("end", end.to_owned()),
("cadence", cadence.to_owned()),
("insight_type", insight_type.to_owned()),
],
RequestBody::None,
)
.await
}
pub async fn get_profile_insights(
&self,
network_id: &str,
profile_id: &str,
start: &str,
end: &str,
cadence: &str,
insight_type: &str,
) -> Result<Envelope, Error> {
let cadence = params::validate_cadence(cadence, params::CADENCE_VALUES)?;
self.transport
.nested(
&routes::insights::GET_PROFILE_INSIGHTS,
network_id,
profile_id,
None,
&[
("start", start.to_owned()),
("end", end.to_owned()),
("cadence", cadence.to_owned()),
("insight_type", insight_type.to_owned()),
],
RequestBody::None,
)
.await
}
pub async fn get_profile_devices_insights(
&self,
network_id: &str,
profile_id: &str,
start: &str,
end: &str,
cadence: &str,
insight_type: &str,
) -> Result<Envelope, Error> {
let cadence = params::validate_cadence(cadence, params::CADENCE_VALUES)?;
self.transport
.nested(
&routes::insights::GET_PROFILE_DEVICES_INSIGHTS,
network_id,
profile_id,
None,
&[
("start", start.to_owned()),
("end", end.to_owned()),
("cadence", cadence.to_owned()),
("insight_type", insight_type.to_owned()),
],
RequestBody::None,
)
.await
}
}