Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

let created = capture.create_session(Some(&CreateSessionOptions {
max_ttl_seconds: Some(300),
proxy: None,
bypass_bot_detection: None,
..Default::default()
})).await?;
let session_id = created["session"]["id"]
.as_str()
Expand All @@ -89,6 +88,40 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}
```

#### CDP Sessions

Set `cdp` to create a session that exposes a Chrome DevTools Protocol connect URL.
The API returns this as `connectUrl`; Rust code commonly stores it as `connect_url`.
CDP sessions cannot be combined with `proxy` or `bypassBotDetection`.

```rust
use capture_rust::{Capture, CreateSessionOptions};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let capture = Capture::new("your_api_key".to_string(), "your_api_secret".to_string());

let created = capture.create_session(Some(&CreateSessionOptions {
cdp: Some(true),
max_ttl_seconds: Some(300),
..Default::default()
})).await?;

let session_id = created["session"]["id"]
.as_str()
.expect("session id");
let connect_url = created["session"]["connectUrl"]
.as_str()
.expect("CDP connectUrl");

println!("CDP connect URL: {connect_url}");

capture.close_session(session_id).await?;

Ok(())
}
```

### Image Capture

```rust
Expand Down
33 changes: 33 additions & 0 deletions examples/cdp_session.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use capture_rust::{Capture, CreateSessionOptions};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let api_key =
std::env::var("CAPTURE_KEY").expect("CAPTURE_KEY environment variable is required");
let api_secret =
std::env::var("CAPTURE_SECRET").expect("CAPTURE_SECRET environment variable is required");

let capture = Capture::new(api_key, api_secret);

let created = capture
.create_session(Some(&CreateSessionOptions {
cdp: Some(true),
max_ttl_seconds: Some(300),
..Default::default()
}))
.await?;

let session_id = created["session"]["id"]
.as_str()
.expect("created session id");
let connect_url = created["session"]["connectUrl"]
.as_str()
.expect("CDP connectUrl");

println!("Session ID: {session_id}");
println!("CDP connect URL: {connect_url}");

capture.close_session(session_id).await?;

Ok(())
}
15 changes: 13 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,8 @@ pub struct CreateSessionOptions {
pub proxy: Option<bool>,
#[serde(rename = "bypassBotDetection", skip_serializing_if = "Option::is_none")]
pub bypass_bot_detection: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub cdp: Option<bool>,
}

#[derive(Debug, Serialize)]
Expand Down Expand Up @@ -1015,17 +1017,26 @@ mod tests {
fn test_create_session_options_serialization() {
let options = CreateSessionOptions {
max_ttl_seconds: Some(300),
proxy: Some(true),
proxy: None,
bypass_bot_detection: None,
cdp: Some(true),
};

let value = serde_json::to_value(options).unwrap();
assert_eq!(
value,
serde_json::json!({
"maxTtlSeconds": 300,
"proxy": true
"cdp": true
})
);
}

#[test]
fn test_create_session_options_omit_empty_serialization() {
let options = CreateSessionOptions::default();

let value = serde_json::to_value(options).unwrap();
assert_eq!(value, serde_json::json!({}));
}
}
1 change: 1 addition & 0 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ async fn test_live_session_screenshot_example_dot_com() {
max_ttl_seconds: Some(120),
proxy: None,
bypass_bot_detection: None,
cdp: None,
}))
.await
.expect("create session");
Expand Down
Loading