-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraw_log.rs
More file actions
49 lines (43 loc) · 1.88 KB
/
Copy pathraw_log.rs
File metadata and controls
49 lines (43 loc) · 1.88 KB
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
//! Fetch an Actor run's log in its raw (unprocessed) form.
//!
//! By default the API adds a per-line timestamp prefix to log lines. Passing
//! `LogOptions { raw: Some(true) }` requests the unprocessed log instead — the same raw form
//! the JS reference's log redirection consumes. This example exercises both the buffered
//! (`get_with_options`) and the streaming (`stream_with_options`) raw-log paths end-to-end so
//! the `Test examples` CI step covers the new `raw` parameter against the live API.
//!
//! Run with: `APIFY_TOKEN=... cargo run --example raw_log`
use apify_client::{ApifyClient, LogOptions};
use futures_util::StreamExt;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let token = std::env::var("APIFY_TOKEN").expect("set APIFY_TOKEN");
let client = ApifyClient::new(token);
// Run a short public Actor and wait for it to finish so it has a complete log.
let run = client
.actor("apify/hello-world")
.call::<serde_json::Value>(None, Default::default(), Some(120))
.await?;
println!("Run {} finished with status {:?}", run.id, run.status);
// Buffered raw log: fetch the whole log without the server-added per-line timestamps.
let raw_log = client
.run(&run.id)
.log()
.get_with_options(LogOptions { raw: Some(true) })
.await?;
match raw_log {
Some(log) => println!("Fetched raw log ({} bytes)", log.len()),
None => println!("Run has no log"),
}
// Streaming raw log: stream the same unprocessed log chunk by chunk.
let mut stream = client
.run(&run.id)
.get_streamed_log_with_options(LogOptions { raw: Some(true) })
.await?;
let mut streamed_bytes = 0usize;
while let Some(chunk) = stream.next().await {
streamed_bytes += chunk?.len();
}
println!("Streamed raw log ({streamed_bytes} bytes)");
Ok(())
}