use std::collections::HashMap;
use reqwest::Method;
use serde::{Deserialize, Serialize};
use super::Client;
use crate::error::Result;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum LogState {
Created,
Active,
Retry,
Error,
Delivered,
Failed,
Cancelled,
CancelRequested,
InProgress,
#[serde(other)]
Unknown,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LogEntry {
pub time: u64,
pub state: LogState,
pub message_id: String,
pub next_delivery_time: Option<u64>,
pub error: Option<String>,
pub url: Option<String>,
pub topic_name: Option<String>,
pub endpoint_name: Option<String>,
pub header: Option<HashMap<String, Vec<String>>>,
pub body: Option<String>,
pub body_base64: Option<String>,
pub response_status: Option<u16>,
pub label: Option<String>,
pub queue_name: Option<String>,
pub schedule_id: Option<String>,
pub caller_ip: Option<String>,
pub flow_control_key: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LogsPage {
pub cursor: Option<String>,
pub logs: Vec<LogEntry>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct LogFilter {
pub cursor: Option<String>,
pub message_id: Option<String>,
pub state: Option<LogState>,
pub url: Option<String>,
pub url_group_name: Option<String>,
pub endpoint_name: Option<String>,
pub schedule_id: Option<String>,
pub queue_name: Option<String>,
pub caller_ip: Option<String>,
pub flow_control_key: Option<String>,
pub response_status: Option<u16>,
pub label: Option<String>,
pub from_date: Option<u64>,
pub to_date: Option<u64>,
pub count: Option<u32>,
}
pub struct LogsApi<'a> {
pub(crate) client: &'a Client,
}
impl LogsApi<'_> {
pub async fn list(&self, filter: &LogFilter) -> Result<LogsPage> {
self.client
.http
.send_json(
Method::GET,
"v2/logs",
&log_filter_query(filter),
None,
None,
)
.await
}
}
fn log_filter_query(filter: &LogFilter) -> Vec<(String, String)> {
let mut query = Vec::new();
if let Some(cursor) = &filter.cursor {
query.push((String::from("cursor"), cursor.clone()));
}
if let Some(message_id) = &filter.message_id {
query.push((String::from("messageId"), message_id.clone()));
}
if let Some(state) = &filter.state {
query.push((String::from("state"), serde_state(state)));
}
if let Some(url) = &filter.url {
query.push((String::from("url"), url.clone()));
}
if let Some(url_group_name) = &filter.url_group_name {
query.push((String::from("topicName"), url_group_name.clone()));
}
if let Some(endpoint_name) = &filter.endpoint_name {
query.push((String::from("endpointName"), endpoint_name.clone()));
}
if let Some(schedule_id) = &filter.schedule_id {
query.push((String::from("scheduleId"), schedule_id.clone()));
}
if let Some(queue_name) = &filter.queue_name {
query.push((String::from("queueName"), queue_name.clone()));
}
if let Some(caller_ip) = &filter.caller_ip {
query.push((String::from("callerIp"), caller_ip.clone()));
}
if let Some(flow_control_key) = &filter.flow_control_key {
query.push((String::from("flowControlKey"), flow_control_key.clone()));
}
if let Some(response_status) = filter.response_status {
query.push((String::from("responseStatus"), response_status.to_string()));
}
if let Some(label) = &filter.label {
query.push((String::from("label"), label.clone()));
}
if let Some(from_date) = filter.from_date {
query.push((String::from("fromDate"), from_date.to_string()));
}
if let Some(to_date) = filter.to_date {
query.push((String::from("toDate"), to_date.to_string()));
}
if let Some(count) = filter.count {
query.push((String::from("count"), count.to_string()));
}
query
}
fn serde_state(state: &LogState) -> String {
match state {
LogState::Created => String::from("CREATED"),
LogState::Active => String::from("ACTIVE"),
LogState::Retry => String::from("RETRY"),
LogState::Error => String::from("ERROR"),
LogState::Delivered => String::from("DELIVERED"),
LogState::Failed => String::from("FAILED"),
LogState::Cancelled => String::from("CANCELLED"),
LogState::CancelRequested => String::from("CANCEL_REQUESTED"),
LogState::InProgress => String::from("IN_PROGRESS"),
LogState::Unknown => String::from("UNKNOWN"),
}
}