Skip to content

Commit

Permalink
chore(api): make create/update tracker API params serializable
Browse files Browse the repository at this point in the history
  • Loading branch information
azasypkin committed Dec 23, 2024
1 parent 4100df3 commit 7e86070
Show file tree
Hide file tree
Showing 3 changed files with 374 additions and 21 deletions.
173 changes: 167 additions & 6 deletions components/retrack-types/src/trackers/tracker_create_params.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::trackers::{TrackerAction, TrackerConfig, TrackerTarget};
use serde::Deserialize;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;

/// Parameters for creating a tracker.
#[derive(Deserialize, Debug, Clone, PartialEq, Eq, ToSchema)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct TrackerCreateParams {
/// Arbitrary name of the tracker.
Expand Down Expand Up @@ -40,8 +40,172 @@ mod tests {
WebhookAction,
},
};
use serde_json::json;
use std::time::Duration;

#[test]
fn serialization() -> anyhow::Result<()> {
let params = TrackerCreateParams {
name: "tck".to_string(),
enabled: true,
target: TrackerTarget::Page(PageTarget {
extractor: "export async function execute(p) { await p.goto('https://retrack.dev/'); return await p.content(); }".to_string(),
params: None,
user_agent: None,
ignore_https_errors: false,
}),
config: Default::default(),
tags: vec![],
actions: vec![],
};
assert_eq!(
serde_json::to_value(&params)?,
json!({
"name": "tck",
"enabled": true,
"target": {
"type": "page",
"extractor": "export async function execute(p) { await p.goto('https://retrack.dev/'); return await p.content(); }"
},
"config": {
"revisions": 3
},
"actions": [],
"tags": []
})
);

let params = TrackerCreateParams {
name: "tck".to_string(),
enabled: false,
target: TrackerTarget::Page(PageTarget {
extractor: "export async function execute(p) { await p.goto('https://retrack.dev/'); return await p.content(); }".to_string(),
params: None,
user_agent: None,
ignore_https_errors: false,
}),
config: TrackerConfig {
revisions: 10,
..Default::default()
},
tags: vec![],
actions: vec![],
};
assert_eq!(
serde_json::to_value(&params)?,
json!({
"name": "tck",
"enabled": false,
"target": {
"type": "page",
"extractor": "export async function execute(p) { await p.goto('https://retrack.dev/'); return await p.content(); }"
},
"config": {
"revisions": 10
},
"actions": [],
"tags": []
})
);

let params = TrackerCreateParams {
name: "tck".to_string(),
enabled: true,
target: TrackerTarget::Page(PageTarget {
extractor: "export async function execute(p) { await p.goto('https://retrack.dev/'); return await p.content(); }".to_string(),
params: None,
user_agent: None,
ignore_https_errors: false,
}),
config: TrackerConfig {
revisions: 3,
timeout: Some(Duration::from_millis(2000)),
..Default::default()
},
tags: vec![],
actions: vec![],
};
assert_eq!(
serde_json::to_value(&params)?,
json!({
"name": "tck",
"enabled": true,
"target": {
"type": "page",
"extractor": "export async function execute(p) { await p.goto('https://retrack.dev/'); return await p.content(); }"
},
"config": {
"revisions": 3,
"timeout": 2000
},
"actions": [],
"tags": []
})
);

let params = TrackerCreateParams {
name: "tck".to_string(),
enabled: true,
target: TrackerTarget::Page(PageTarget {
extractor: "export async function execute(p) { await p.goto('https://retrack.dev/'); return await p.content(); }".to_string(),
params: Some(json!({ "param": "value" })),
user_agent: Some("Retrack/1.0.0".to_string()),
ignore_https_errors: true,
}),
config: TrackerConfig {
revisions: 3,
timeout: Some(Duration::from_millis(2000)),
job: Some(SchedulerJobConfig {
schedule: "0 0 * * *".to_string(),
retry_strategy: Some(SchedulerJobRetryStrategy::Exponential {
initial_interval: Duration::from_millis(1234),
multiplier: 2,
max_interval: Duration::from_secs(120),
max_attempts: 5,
})
}),
},
tags: vec!["tag".to_string()],
actions: vec![TrackerAction::ServerLog, TrackerAction::Webhook(WebhookAction {
url: "https://retrack.dev".parse()?,
method: None,
headers: None,
})],
};
assert_eq!(
serde_json::to_value(&params)?,
json!({
"name": "tck",
"enabled": true,
"target": {
"type": "page",
"extractor": "export async function execute(p) { await p.goto('https://retrack.dev/'); return await p.content(); }",
"params": { "param": "value" },
"userAgent": "Retrack/1.0.0",
"ignoreHTTPSErrors": true
},
"config": {
"revisions": 3,
"timeout": 2000,
"job": {
"schedule": "0 0 * * *",
"retryStrategy": {
"type": "exponential",
"initialInterval": 1234,
"multiplier": 2,
"maxInterval": 120000,
"maxAttempts": 5
}
}
},
"tags": ["tag"],
"actions": [{ "type": "log" }, { "type": "webhook", "url": "https://retrack.dev/" }]
})
);

Ok(())
}

#[test]
fn deserialization() -> anyhow::Result<()> {
assert_eq!(
Expand Down Expand Up @@ -146,9 +310,6 @@ mod tests {
"config": {
"revisions": 3,
"timeout": 2000,
"headers": {
"cookie": "my-cookie"
},
"job": {
"schedule": "0 0 * * *",
"retryStrategy": {
Expand All @@ -170,7 +331,7 @@ mod tests {
enabled: true,
target: TrackerTarget::Page(PageTarget {
extractor: "export async function execute(p) { await p.goto('https://retrack.dev/'); return await p.content(); }".to_string(),
params: Some(serde_json::json!({ "param": "value" })),
params: Some(json!({ "param": "value" })),
user_agent: Some("Retrack/1.0.0".to_string()),
ignore_https_errors: true,
}),
Expand Down
2 changes: 1 addition & 1 deletion components/retrack-types/src/trackers/tracker_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ mod tests {
use std::collections::HashMap;

#[test]
fn can_serialization_and_deserialize() -> anyhow::Result<()> {
fn can_serialize_and_deserialize() -> anyhow::Result<()> {
let target = TrackerTarget::Page(PageTarget {
extractor: "export async function execute(p) { await p.goto('https://retrack.dev/'); return await p.content(); }".to_string(),
params: None,
Expand Down
Loading

0 comments on commit 7e86070

Please sign in to comment.