pfp 0.5.0

Prefect CLI - a fast Rust CLI for managing Prefect deployments and flow runs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
use crate::config::Config;
use crate::error::{PfpError, Result};
use reqwest::Client;
use serde::de::DeserializeOwned;
use std::collections::HashMap;

pub struct PrefectClient {
    client: Client,
    config: Config,
}

impl PrefectClient {
    pub fn new(config: Config) -> Self {
        Self {
            client: Client::new(),
            config,
        }
    }

    pub async fn get<T: DeserializeOwned>(&self, path: &str) -> Result<T> {
        let url = format!("{}{}", self.config.api_url, path);
        let mut req = self.client.get(&url);
        if let Some(auth) = &self.config.auth_header {
            req = req.header("Authorization", auth);
        }
        let response = req.send().await?;

        if !response.status().is_success() {
            let status = response.status();
            let body = response.text().await.unwrap_or_default();
            return Err(PfpError::Api(format!("{}: {}", status, body)));
        }

        Ok(response.json().await?)
    }

    pub async fn post<T: DeserializeOwned>(
        &self,
        path: &str,
        body: &serde_json::Value,
    ) -> Result<T> {
        let url = format!("{}{}", self.config.api_url, path);
        let mut req = self.client.post(&url);
        if let Some(auth) = &self.config.auth_header {
            req = req.header("Authorization", auth);
        }
        let response = req.json(body).send().await?;

        if !response.status().is_success() {
            let status = response.status();
            let body = response.text().await.unwrap_or_default();
            return Err(PfpError::Api(format!("{}: {}", status, body)));
        }

        Ok(response.json().await?)
    }

    pub async fn patch_no_content(&self, path: &str, body: &serde_json::Value) -> Result<()> {
        let url = format!("{}{}", self.config.api_url, path);
        let mut req = self.client.patch(&url);
        if let Some(auth) = &self.config.auth_header {
            req = req.header("Authorization", auth);
        }
        let response = req.json(body).send().await?;

        if !response.status().is_success() {
            let status = response.status();
            let body = response.text().await.unwrap_or_default();
            return Err(PfpError::Api(format!("{}: {}", status, body)));
        }

        Ok(())
    }

    // -- Prefect API methods --

    pub async fn list_deployments(&self) -> Result<Vec<serde_json::Value>> {
        let body = serde_json::json!({
            "limit": 100,
            "offset": 0
        });
        let mut deployments: Vec<serde_json::Value> =
            self.post("/deployments/filter", &body).await?;

        // Collect unique flow_ids to resolve flow names
        let flow_ids: Vec<String> = deployments
            .iter()
            .filter_map(|d| d["flow_id"].as_str().map(|s| s.to_string()))
            .collect::<std::collections::HashSet<_>>()
            .into_iter()
            .collect();

        if !flow_ids.is_empty() {
            let flow_names = self.fetch_flow_names(&flow_ids).await?;
            for dep in &mut deployments {
                if let Some(fid) = dep["flow_id"].as_str() {
                    if let Some(name) = flow_names.get(fid) {
                        dep["flow_name"] = serde_json::Value::String(name.clone());
                    }
                }
            }
        }

        Ok(deployments)
    }

    async fn fetch_flow_names(&self, flow_ids: &[String]) -> Result<HashMap<String, String>> {
        let body = serde_json::json!({
            "flows": {
                "id": {
                    "any_": flow_ids
                }
            }
        });
        let flows: Vec<serde_json::Value> = self.post("/flows/filter", &body).await?;
        Ok(flows
            .into_iter()
            .filter_map(|f| {
                let id = f["id"].as_str()?.to_string();
                let name = f["name"].as_str()?.to_string();
                Some((id, name))
            })
            .collect())
    }

    pub async fn create_flow_run(
        &self,
        deployment_id: &str,
        parameters: serde_json::Value,
    ) -> Result<serde_json::Value> {
        let body = serde_json::json!({
            "parameters": parameters
        });
        self.post(
            &format!("/deployments/{}/create_flow_run", deployment_id),
            &body,
        )
        .await
    }

    pub async fn get_flow_run(&self, flow_run_id: &str) -> Result<serde_json::Value> {
        self.get(&format!("/flow_runs/{}", flow_run_id)).await
    }

    pub async fn filter_flow_runs(
        &self,
        deployment_id: &str,
        limit: usize,
    ) -> Result<Vec<serde_json::Value>> {
        let body = serde_json::json!({
            "flow_runs": {
                "deployment_id": {
                    "any_": [deployment_id]
                }
            },
            "sort": "START_TIME_DESC",
            "limit": limit
        });
        self.post("/flow_runs/filter", &body).await
    }

    pub async fn filter_flow_runs_global(&self, limit: usize) -> Result<Vec<serde_json::Value>> {
        let body = serde_json::json!({
            "sort": "START_TIME_DESC",
            "limit": limit
        });
        self.post("/flow_runs/filter", &body).await
    }

    pub async fn get_flow_run_logs(
        &self,
        flow_run_id: &str,
        limit: usize,
        start_offset: usize,
    ) -> Result<Vec<serde_json::Value>> {
        const PAGE_SIZE: usize = 200;
        let mut all_logs = Vec::new();
        let mut offset: usize = start_offset;

        loop {
            let remaining = limit - all_logs.len();
            let page_limit = remaining.min(PAGE_SIZE);

            let body = serde_json::json!({
                "logs": {
                    "flow_run_id": {
                        "any_": [flow_run_id]
                    }
                },
                "sort": "TIMESTAMP_ASC",
                "limit": page_limit,
                "offset": offset
            });

            let page: Vec<serde_json::Value> = self.post("/logs/filter", &body).await?;
            let page_len = page.len();
            all_logs.extend(page);

            if page_len < page_limit || all_logs.len() >= limit {
                break;
            }

            offset += page_len;
        }

        Ok(all_logs)
    }

    pub async fn set_deployment_paused(&self, deployment_id: &str, paused: bool) -> Result<()> {
        let body = serde_json::json!({ "paused": paused });
        self.patch_no_content(&format!("/deployments/{}", deployment_id), &body)
            .await
    }

    pub async fn cancel_flow_run(&self, flow_run_id: &str) -> Result<serde_json::Value> {
        let body = serde_json::json!({
            "state": {
                "type": "CANCELLED",
                "message": "Cancelled via pfp CLI"
            },
            "force": true
        });
        self.post(&format!("/flow_runs/{}/set_state", flow_run_id), &body)
            .await
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn test_client(server: &mockito::Server) -> PrefectClient {
        let config = Config {
            api_url: server.url(),
            auth_header: Some("Basic dGVzdDp0ZXN0".to_string()),
        };
        PrefectClient::new(config)
    }

    #[tokio::test]
    async fn list_deployments_success() {
        let mut server = mockito::Server::new_async().await;
        let deploy_mock = server
            .mock("POST", "/deployments/filter")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(r#"[{"name":"test-deploy","flow_id":"flow-1"}]"#)
            .create_async()
            .await;
        let flow_mock = server
            .mock("POST", "/flows/filter")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(r#"[{"id":"flow-1","name":"test_flow"}]"#)
            .create_async()
            .await;

        let client = test_client(&server);
        let result = client.list_deployments().await.unwrap();

        assert_eq!(result.len(), 1);
        assert_eq!(result[0]["name"], "test-deploy");
        assert_eq!(result[0]["flow_name"], "test_flow");
        deploy_mock.assert_async().await;
        flow_mock.assert_async().await;
    }

    #[tokio::test]
    async fn list_deployments_api_error() {
        let mut server = mockito::Server::new_async().await;
        let mock = server
            .mock("POST", "/deployments/filter")
            .with_status(401)
            .with_body("Unauthorized")
            .create_async()
            .await;

        let client = test_client(&server);
        let result = client.list_deployments().await;

        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), PfpError::Api(ref msg) if msg.contains("401")));
        mock.assert_async().await;
    }

    #[tokio::test]
    async fn get_flow_run_success() {
        let mut server = mockito::Server::new_async().await;
        let mock = server
            .mock("GET", "/flow_runs/abc-123")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(r#"{"id":"abc-123","state_type":"COMPLETED","state_name":"Completed"}"#)
            .create_async()
            .await;

        let client = test_client(&server);
        let result = client.get_flow_run("abc-123").await.unwrap();

        assert_eq!(result["state_type"], "COMPLETED");
        mock.assert_async().await;
    }

    #[tokio::test]
    async fn create_flow_run_success() {
        let mut server = mockito::Server::new_async().await;
        let mock = server
            .mock("POST", "/deployments/dep-id/create_flow_run")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(r#"{"id":"run-123","name":"cool-name","state_type":"SCHEDULED"}"#)
            .create_async()
            .await;

        let client = test_client(&server);
        let params = serde_json::json!({"config": {"action": "plan"}});
        let result = client.create_flow_run("dep-id", params).await.unwrap();

        assert_eq!(result["id"], "run-123");
        assert_eq!(result["name"], "cool-name");
        mock.assert_async().await;
    }

    #[tokio::test]
    async fn patch_no_content_success() {
        let mut server = mockito::Server::new_async().await;
        let mock = server
            .mock("PATCH", "/deployments/dep-id")
            .with_status(204)
            .create_async()
            .await;

        let client = test_client(&server);
        let result = client.set_deployment_paused("dep-id", true).await;

        assert!(result.is_ok());
        mock.assert_async().await;
    }

    #[tokio::test]
    async fn get_flow_run_logs_single_page() {
        let mut server = mockito::Server::new_async().await;
        let mock = server
            .mock("POST", "/logs/filter")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(r#"[{"level":20,"message":"hello","timestamp":"2026-01-01T00:00:00Z"},{"level":20,"message":"world","timestamp":"2026-01-01T00:00:01Z"}]"#)
            .expect(1)
            .create_async()
            .await;

        let client = test_client(&server);
        let result = client.get_flow_run_logs("run-1", 10_000, 0).await.unwrap();

        assert_eq!(result.len(), 2);
        assert_eq!(result[0]["message"], "hello");
        assert_eq!(result[1]["message"], "world");
        mock.assert_async().await;
    }

    #[tokio::test]
    async fn get_flow_run_logs_multi_page() {
        let mut server = mockito::Server::new_async().await;

        // Build a response with exactly 200 entries (one full page)
        let page1: Vec<serde_json::Value> = (0..200)
            .map(|i| serde_json::json!({"level":20,"message":format!("msg-{}", i),"timestamp":"2026-01-01T00:00:00Z"}))
            .collect();
        let page2 = vec![
            serde_json::json!({"level":20,"message":"msg-200","timestamp":"2026-01-01T00:00:01Z"}),
            serde_json::json!({"level":20,"message":"msg-201","timestamp":"2026-01-01T00:00:02Z"}),
        ];

        let mock1 = server
            .mock("POST", "/logs/filter")
            .match_body(mockito::Matcher::PartialJsonString(
                r#"{"offset":0}"#.to_string(),
            ))
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(serde_json::to_string(&page1).unwrap())
            .expect(1)
            .create_async()
            .await;

        let mock2 = server
            .mock("POST", "/logs/filter")
            .match_body(mockito::Matcher::PartialJsonString(
                r#"{"offset":200}"#.to_string(),
            ))
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(serde_json::to_string(&page2).unwrap())
            .expect(1)
            .create_async()
            .await;

        let client = test_client(&server);
        let result = client.get_flow_run_logs("run-1", 10_000, 0).await.unwrap();

        assert_eq!(result.len(), 202);
        assert_eq!(result[0]["message"], "msg-0");
        assert_eq!(result[201]["message"], "msg-201");
        mock1.assert_async().await;
        mock2.assert_async().await;
    }

    #[tokio::test]
    async fn get_flow_run_logs_respects_limit() {
        let mut server = mockito::Server::new_async().await;

        // Return 150 entries — but we set limit to 150, which is less than page size 200
        // so the request should ask for limit=150 and get 150 back, then stop
        let entries: Vec<serde_json::Value> = (0..150)
            .map(|i| serde_json::json!({"level":20,"message":format!("msg-{}", i),"timestamp":"2026-01-01T00:00:00Z"}))
            .collect();

        let mock = server
            .mock("POST", "/logs/filter")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(serde_json::to_string(&entries).unwrap())
            .expect(1)
            .create_async()
            .await;

        let client = test_client(&server);
        let result = client.get_flow_run_logs("run-1", 150, 0).await.unwrap();

        assert_eq!(result.len(), 150);
        mock.assert_async().await;
    }

    #[tokio::test]
    async fn get_flow_run_logs_from_offset() {
        let mut server = mockito::Server::new_async().await;

        let mock = server
            .mock("POST", "/logs/filter")
            .match_body(mockito::Matcher::PartialJsonString(
                r#"{"offset":5}"#.to_string(),
            ))
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(r#"[{"level":20,"message":"new-msg","timestamp":"2026-01-01T00:01:00Z"}]"#)
            .expect(1)
            .create_async()
            .await;

        let client = test_client(&server);
        let result = client.get_flow_run_logs("run-1", 100, 5).await.unwrap();

        assert_eq!(result.len(), 1);
        assert_eq!(result[0]["message"], "new-msg");
        mock.assert_async().await;
    }

    #[tokio::test]
    async fn filter_flow_runs_global_success() {
        let mut server = mockito::Server::new_async().await;
        let mock = server
            .mock("POST", "/flow_runs/filter")
            .match_body(mockito::Matcher::PartialJsonString(
                r#"{"sort":"START_TIME_DESC","limit":50}"#.to_string(),
            ))
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(r#"[{"id":"aaa-111","name":"run-1","state_type":"COMPLETED"},{"id":"bbb-222","name":"run-2","state_type":"RUNNING"}]"#)
            .create_async()
            .await;

        let client = test_client(&server);
        let result = client.filter_flow_runs_global(50).await.unwrap();

        assert_eq!(result.len(), 2);
        assert_eq!(result[0]["id"], "aaa-111");
        assert_eq!(result[1]["id"], "bbb-222");
        mock.assert_async().await;
    }

    #[tokio::test]
    async fn filter_flow_runs_global_api_error() {
        let mut server = mockito::Server::new_async().await;
        let mock = server
            .mock("POST", "/flow_runs/filter")
            .with_status(500)
            .with_body("Internal Server Error")
            .create_async()
            .await;

        let client = test_client(&server);
        let result = client.filter_flow_runs_global(50).await;

        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), PfpError::Api(ref msg) if msg.contains("500")));
        mock.assert_async().await;
    }
}