use k256::ecdsa::SigningKey;
use super::*;
pub async fn create_offchain_job(
signer: &SigningKey,
now_secs: u64,
kind: &str,
target: &str,
task: &str,
interval_secs: u64,
runs: u32,
) -> Result<String, String> {
let token = proxy_auth_token(signer, now_secs, "schedule");
let url = format!("{CREDIT_PROXY_URL}api/schedule");
let mut body = serde_json::json!({
"action": "create",
"kind": kind,
"task": task,
"intervalSecs": interval_secs,
"runs": runs,
});
if !target.is_empty() {
body["target"] = serde_json::Value::String(target.to_string());
}
let resp = http_post_json_authed_returning(&url, &token, &body).await?;
resp.get("id")
.and_then(|v| v.as_str())
.map(|s| s.to_string())
.ok_or_else(|| format!("schedule: unexpected response {resp}"))
}
pub async fn cancel_offchain_job(signer: &SigningKey, now_secs: u64, id: &str) -> Result<(), String> {
let token = proxy_auth_token(signer, now_secs, "schedule");
let url = format!("{CREDIT_PROXY_URL}api/schedule");
let body = serde_json::json!({ "action": "cancel", "id": id });
http_post_json_authed_returning(&url, &token, &body).await.map(|_| ())
}
pub async fn list_offchain_jobs(
signer: &SigningKey,
now_secs: u64,
) -> Result<Vec<serde_json::Value>, String> {
let token = proxy_auth_token(signer, now_secs, "schedule");
let url = format!("{CREDIT_PROXY_URL}api/schedule");
let body = serde_json::json!({ "action": "list" });
let resp = http_post_json_authed_returning(&url, &token, &body).await?;
Ok(resp
.get("jobs")
.and_then(|v| v.as_array())
.cloned()
.unwrap_or_default())
}