1use crate::client::CdpClient;
8use crate::error::{CdpError, Result};
9use crate::types::{BrowserVersion, Target};
10
11impl CdpClient {
12 pub async fn get_version(host: &str, port: u16) -> Result<BrowserVersion> {
14 let url = format!("http://{host}:{port}/json/version");
15 Ok(reqwest::get(&url).await?.json().await?)
16 }
17
18 pub async fn list_targets(host: &str, port: u16) -> Result<Vec<Target>> {
20 let url = format!("http://{host}:{port}/json/list");
21 Ok(reqwest::get(&url).await?.json().await?)
22 }
23
24 pub async fn connect_to_page(host: &str, port: u16) -> Result<Self> {
27 let targets = Self::list_targets(host, port).await?;
28 let page = targets
29 .into_iter()
30 .find(|t| t.target_type == "page")
31 .ok_or(CdpError::NoTarget)?;
32 let ws_url = page
33 .web_socket_debugger_url
34 .ok_or_else(|| CdpError::InvalidUrl("target has no debugger URL".into()))?;
35 Self::connect(&ws_url).await
36 }
37
38 pub async fn create_tab(host: &str, port: u16, url: Option<&str>) -> Result<Target> {
41 let endpoint = match url {
43 Some(u) => format!("http://{host}:{port}/json/new?{u}"),
44 None => format!("http://{host}:{port}/json/new"),
45 };
46 Ok(reqwest::Client::new()
47 .put(&endpoint)
48 .send()
49 .await?
50 .json()
51 .await?)
52 }
53}