browserbase 0.1.0

Rust SDK for Browserbase - cloud browser automation platform
Documentation
use std::path::Path;

use bytes::Bytes;
use reqwest::Method;
use serde_json::Value;

use crate::{
    client::Browserbase,
    error::Error,
    models::{CreateSessionRequest, ListSessionsParams, Session, UpdateSessionRequest},
};

pub struct Sessions<'a> {
    client: &'a Browserbase,
}

impl<'a> Sessions<'a> {
    pub(crate) fn new(client: &'a Browserbase) -> Self {
        Self { client }
    }

    pub async fn create(&self, request: CreateSessionRequest) -> Result<Session, Error> {
        let rb = self
            .client
            .request(Method::POST, "sessions")?
            .json(&request);
        self.client.send_json(rb).await
    }

    pub async fn list(&self, params: ListSessionsParams) -> Result<Vec<Session>, Error> {
        let rb = self.client.request(Method::GET, "sessions")?.query(&params);
        self.client.send_json(rb).await
    }

    pub async fn get(&self, id: &str) -> Result<Session, Error> {
        let rb = self
            .client
            .request(Method::GET, &format!("sessions/{id}"))?;
        self.client.send_json(rb).await
    }

    pub async fn update(&self, id: &str, request: UpdateSessionRequest) -> Result<Session, Error> {
        let rb = self
            .client
            .request(Method::POST, &format!("sessions/{id}"))?
            .json(&request);
        self.client.send_json(rb).await
    }

    pub async fn delete(&self, id: &str) -> Result<(), Error> {
        let rb = self
            .client
            .request(Method::DELETE, &format!("sessions/{id}"))?;
        self.client.send_unit(rb).await
    }

    pub async fn logs(&self, id: &str) -> Result<Value, Error> {
        let rb = self
            .client
            .request(Method::GET, &format!("sessions/{id}/logs"))?;
        self.client.send_json(rb).await
    }

    pub async fn create_upload_from_bytes(
        &self,
        id: &str,
        file_name: impl Into<String>,
        bytes: Bytes,
    ) -> Result<Value, Error> {
        let part = reqwest::multipart::Part::bytes(bytes.to_vec()).file_name(file_name.into());
        let form = reqwest::multipart::Form::new().part("file", part);

        let rb = self
            .client
            .request(Method::POST, &format!("sessions/{id}/uploads"))?
            .multipart(form);

        self.client.send_json(rb).await
    }

    pub async fn create_upload_from_path(&self, id: &str, path: &Path) -> Result<Value, Error> {
        let file_name = path
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or("upload.bin")
            .to_string();

        let bytes = std::fs::read(path)?;
        self.create_upload_from_bytes(id, file_name, Bytes::from(bytes))
            .await
    }

    pub async fn downloads_zip(&self, id: &str) -> Result<Bytes, Error> {
        let rb = self
            .client
            .request(Method::GET, &format!("sessions/{id}/downloads"))?;
        self.client.send_bytes(rb).await
    }
}