snm_brightdata_client/
client.rs

1// src/client.rs
2use crate::{config::BrightDataConfig, error::BrightDataError};
3use reqwest::Client;
4use serde_json::Value;
5use anyhow::{Result, anyhow};
6use crate::tool::ToolResolver;
7
8pub struct BrightDataClient {
9    config: BrightDataConfig,
10    client: Client,
11}
12
13impl BrightDataClient {
14    pub fn new(config: BrightDataConfig) -> Self {
15        Self {
16            config,
17            client: Client::new(),
18        }
19    }
20
21    pub async fn get(&self, target_url: &str) -> Result<Value, BrightDataError> {
22        let payload = serde_json::json!({
23            "url": target_url,
24        });
25
26        let res = self
27            .client
28            .post(&self.config.endpoint)
29            .header("Authorization", format!("Bearer {}", self.config.token))
30            .json(&payload)
31            .send()
32            .await?
33            .json::<Value>()
34            .await?;
35
36        Ok(res)
37    }
38
39    pub async fn run(&self, tool_name: &str, input: Value) -> Result<Value> {
40        let resolver = ToolResolver::default();
41        let tool = resolver
42            .resolve(tool_name)
43            .ok_or_else(|| anyhow!("Tool `{tool_name}` not found"))?;
44        
45        // Use legacy method for backward compatibility
46        tool.execute_legacy(input).await.map_err(|e| anyhow!(e))
47    }
48}