use crate::errors::HivehookError;
use crate::resources::_base::{put_opt, vars};
#[cfg(feature = "blocking")]
use crate::transport::BlockingGraphQLTransport;
use crate::types::{AuditLog, ListResult};
use serde::Deserialize;
use serde_json::Value;
const FRAGMENT: &str = "id actorType actorId actorName action resourceType resourceId orgId ipAddress userAgent details createdAt";
#[non_exhaustive]
#[derive(Debug, Default, Clone)]
pub struct ListAuditLogsOptions {
pub actor_type: Option<String>,
pub resource_type: Option<String>,
pub resource_id: Option<String>,
pub action: Option<String>,
pub since: Option<String>,
pub until: Option<String>,
pub search: Option<String>,
pub limit: Option<i32>,
pub offset: Option<i32>,
pub after: Option<String>,
pub first: Option<i32>,
}
#[derive(Deserialize)]
struct ListData {
#[serde(rename = "auditLogs")]
audit_logs: ListResult<AuditLog>,
}
#[derive(Deserialize)]
struct GetData {
#[serde(rename = "auditLog")]
audit_log: Option<AuditLog>,
}
#[cfg(feature = "blocking")]
pub struct AuditLogService<'a> {
pub(crate) transport: &'a BlockingGraphQLTransport,
}
#[cfg(feature = "blocking")]
impl<'a> AuditLogService<'a> {
pub fn list(
&self,
options: ListAuditLogsOptions,
) -> Result<ListResult<AuditLog>, HivehookError> {
let query = format!(
r#"query($actorType: String, $resourceType: String, $resourceId: UUID, $action: String, $since: Time, $until: Time, $search: String, $limit: Int, $offset: Int, $after: String, $first: Int) {{
auditLogs(actorType: $actorType, resourceType: $resourceType, resourceId: $resourceId, action: $action, since: $since, until: $until, 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, "actorType", options.actor_type);
put_opt(&mut v, "resourceType", options.resource_type);
put_opt(&mut v, "resourceId", options.resource_id);
put_opt(&mut v, "action", options.action);
put_opt(&mut v, "since", options.since);
put_opt(&mut v, "until", options.until);
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.audit_logs)
}
pub fn get(&self, id: &str) -> Result<Option<AuditLog>, HivehookError> {
let query = format!("query($id: UUID!) {{ auditLog(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.audit_log)
}
}
#[cfg(feature = "async")]
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
pub struct AsyncAuditLogService<'a> {
pub(crate) transport: &'a crate::transport::AsyncGraphQLTransport,
}
#[cfg(feature = "async")]
impl<'a> AsyncAuditLogService<'a> {
pub async fn list(
&self,
options: ListAuditLogsOptions,
) -> Result<ListResult<AuditLog>, HivehookError> {
let query = format!(
r#"query($actorType: String, $resourceType: String, $resourceId: UUID, $action: String, $since: Time, $until: Time, $search: String, $limit: Int, $offset: Int, $after: String, $first: Int) {{
auditLogs(actorType: $actorType, resourceType: $resourceType, resourceId: $resourceId, action: $action, since: $since, until: $until, 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, "actorType", options.actor_type);
put_opt(&mut v, "resourceType", options.resource_type);
put_opt(&mut v, "resourceId", options.resource_id);
put_opt(&mut v, "action", options.action);
put_opt(&mut v, "since", options.since);
put_opt(&mut v, "until", options.until);
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.audit_logs)
}
pub async fn get(&self, id: &str) -> Result<Option<AuditLog>, HivehookError> {
let query = format!("query($id: UUID!) {{ auditLog(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.audit_log)
}
}