use crate::errors::HivehookError;
use crate::resources::_base::{put_opt, vars};
#[cfg(feature = "blocking")]
use crate::transport::BlockingGraphQLTransport;
use crate::types::{AlertRule, EmailAlertConfig, ListResult, SlackAlertConfig};
use serde::{Deserialize, Serialize};
use serde_json::Value;
const FRAGMENT: &str = "id name conditionType threshold webhookUrl channel emailConfig { to subjectTemplate } slackConfig { webhookUrl channel } cooldown enabled createdAt";
#[non_exhaustive]
#[derive(Debug, Default, Clone)]
pub struct ListAlertRulesOptions {
pub enabled: Option<bool>,
pub search: Option<String>,
pub limit: Option<i32>,
pub offset: Option<i32>,
pub after: Option<String>,
pub first: Option<i32>,
}
#[non_exhaustive]
#[derive(Debug, Default, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateAlertRuleInput {
pub name: String,
pub condition_type: String,
pub threshold: i32,
pub cooldown: String,
pub enabled: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub webhook_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub channel: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub email_config: Option<EmailAlertConfig>,
#[serde(skip_serializing_if = "Option::is_none")]
pub slack_config: Option<SlackAlertConfig>,
}
#[non_exhaustive]
#[derive(Debug, Default, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct UpdateAlertRuleInput {
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub condition_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub threshold: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub webhook_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub channel: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub email_config: Option<EmailAlertConfig>,
#[serde(skip_serializing_if = "Option::is_none")]
pub slack_config: Option<SlackAlertConfig>,
#[serde(skip_serializing_if = "Option::is_none")]
pub cooldown: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub enabled: Option<bool>,
}
#[derive(Deserialize)]
struct ListData {
#[serde(rename = "alertRules")]
alert_rules: ListResult<AlertRule>,
}
#[derive(Deserialize)]
struct GetData {
#[serde(rename = "alertRule")]
alert_rule: Option<AlertRule>,
}
#[derive(Deserialize)]
struct CreateData {
#[serde(rename = "createAlertRule")]
create_alert_rule: AlertRule,
}
#[derive(Deserialize)]
struct UpdateData {
#[serde(rename = "updateAlertRule")]
update_alert_rule: AlertRule,
}
#[derive(Deserialize)]
struct DeleteData {
#[serde(rename = "deleteAlertRule")]
delete_alert_rule: bool,
}
#[derive(Deserialize)]
struct TestData {
#[serde(rename = "testAlertRule")]
test_alert_rule: bool,
}
#[cfg(feature = "blocking")]
pub struct AlertRuleService<'a> {
pub(crate) transport: &'a BlockingGraphQLTransport,
}
#[cfg(feature = "blocking")]
impl<'a> AlertRuleService<'a> {
pub fn list(
&self,
options: ListAlertRulesOptions,
) -> Result<ListResult<AlertRule>, HivehookError> {
let query = format!(
r#"query($enabled: Boolean, $search: String, $limit: Int, $offset: Int, $after: String, $first: Int) {{
alertRules(enabled: $enabled, search: $search, limit: $limit, offset: $offset, after: $after, first: $first) {{
nodes {{ {FRAGMENT} }}
pageInfo {{ total limit offset endCursor hasNextPage }}
}}
}}"#
);
let mut v = vars();
put_opt(&mut v, "enabled", options.enabled);
put_opt(&mut v, "search", options.search);
put_opt(&mut v, "limit", options.limit);
put_opt(&mut v, "offset", options.offset);
put_opt(&mut v, "after", options.after);
put_opt(&mut v, "first", options.first);
let data: ListData = self.transport.execute(&query, Some(v))?;
Ok(data.alert_rules)
}
pub fn get(&self, id: &str) -> Result<Option<AlertRule>, HivehookError> {
let query = format!("query($id: UUID!) {{ alertRule(id: $id) {{ {FRAGMENT} }} }}");
let mut v = vars();
v.insert("id".into(), Value::String(id.into()));
let data: GetData = self.transport.execute(&query, Some(v))?;
Ok(data.alert_rule)
}
pub fn create(&self, input: CreateAlertRuleInput) -> Result<AlertRule, HivehookError> {
let query = format!(
"mutation($input: CreateAlertRuleInput!) {{ createAlertRule(input: $input) {{ {FRAGMENT} }} }}"
);
let mut v = vars();
v.insert("input".into(), serde_json::to_value(input)?);
let data: CreateData = self.transport.execute(&query, Some(v))?;
Ok(data.create_alert_rule)
}
pub fn update(
&self,
id: &str,
input: UpdateAlertRuleInput,
) -> Result<AlertRule, HivehookError> {
let query = format!(
"mutation($id: UUID!, $input: UpdateAlertRuleInput!) {{ updateAlertRule(id: $id, input: $input) {{ {FRAGMENT} }} }}"
);
let mut v = vars();
v.insert("id".into(), Value::String(id.into()));
v.insert("input".into(), serde_json::to_value(input)?);
let data: UpdateData = self.transport.execute(&query, Some(v))?;
Ok(data.update_alert_rule)
}
pub fn delete(&self, id: &str) -> Result<bool, HivehookError> {
let mut v = vars();
v.insert("id".into(), Value::String(id.into()));
let data: DeleteData = self
.transport
.execute("mutation($id: UUID!) { deleteAlertRule(id: $id) }", Some(v))?;
Ok(data.delete_alert_rule)
}
pub fn test(&self, id: &str) -> Result<bool, HivehookError> {
let mut v = vars();
v.insert("id".into(), Value::String(id.into()));
let data: TestData = self
.transport
.execute("mutation($id: UUID!) { testAlertRule(id: $id) }", Some(v))?;
Ok(data.test_alert_rule)
}
}
#[cfg(feature = "async")]
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
pub struct AsyncAlertRuleService<'a> {
pub(crate) transport: &'a crate::transport::AsyncGraphQLTransport,
}
#[cfg(feature = "async")]
impl<'a> AsyncAlertRuleService<'a> {
pub async fn list(
&self,
options: ListAlertRulesOptions,
) -> Result<ListResult<AlertRule>, HivehookError> {
let query = format!(
r#"query($enabled: Boolean, $search: String, $limit: Int, $offset: Int, $after: String, $first: Int) {{
alertRules(enabled: $enabled, search: $search, limit: $limit, offset: $offset, after: $after, first: $first) {{
nodes {{ {FRAGMENT} }}
pageInfo {{ total limit offset endCursor hasNextPage }}
}}
}}"#
);
let mut v = vars();
put_opt(&mut v, "enabled", options.enabled);
put_opt(&mut v, "search", options.search);
put_opt(&mut v, "limit", options.limit);
put_opt(&mut v, "offset", options.offset);
put_opt(&mut v, "after", options.after);
put_opt(&mut v, "first", options.first);
let data: ListData = self.transport.execute(&query, Some(v)).await?;
Ok(data.alert_rules)
}
pub async fn get(&self, id: &str) -> Result<Option<AlertRule>, HivehookError> {
let query = format!("query($id: UUID!) {{ alertRule(id: $id) {{ {FRAGMENT} }} }}");
let mut v = vars();
v.insert("id".into(), Value::String(id.into()));
let data: GetData = self.transport.execute(&query, Some(v)).await?;
Ok(data.alert_rule)
}
pub async fn create(&self, input: CreateAlertRuleInput) -> Result<AlertRule, HivehookError> {
let query = format!(
"mutation($input: CreateAlertRuleInput!) {{ createAlertRule(input: $input) {{ {FRAGMENT} }} }}"
);
let mut v = vars();
v.insert("input".into(), serde_json::to_value(input)?);
let data: CreateData = self.transport.execute(&query, Some(v)).await?;
Ok(data.create_alert_rule)
}
pub async fn update(
&self,
id: &str,
input: UpdateAlertRuleInput,
) -> Result<AlertRule, HivehookError> {
let query = format!(
"mutation($id: UUID!, $input: UpdateAlertRuleInput!) {{ updateAlertRule(id: $id, input: $input) {{ {FRAGMENT} }} }}"
);
let mut v = vars();
v.insert("id".into(), Value::String(id.into()));
v.insert("input".into(), serde_json::to_value(input)?);
let data: UpdateData = self.transport.execute(&query, Some(v)).await?;
Ok(data.update_alert_rule)
}
pub async fn delete(&self, id: &str) -> Result<bool, HivehookError> {
let mut v = vars();
v.insert("id".into(), Value::String(id.into()));
let data: DeleteData = self
.transport
.execute("mutation($id: UUID!) { deleteAlertRule(id: $id) }", Some(v))
.await?;
Ok(data.delete_alert_rule)
}
pub async fn test(&self, id: &str) -> Result<bool, HivehookError> {
let mut v = vars();
v.insert("id".into(), Value::String(id.into()));
let data: TestData = self
.transport
.execute("mutation($id: UUID!) { testAlertRule(id: $id) }", Some(v))
.await?;
Ok(data.test_alert_rule)
}
}