browserflare 0.5.0

Rust client for Cloudflare Browser Rendering APIs
Documentation
use reqwest::Client;

use crate::config::ApiConfig;
use crate::error::{BrowserflareError, Result};
use crate::payloads::{MarkdownPayload, MarkdownResult};

pub async fn fetch_markdown(
    client: &Client,
    config: &ApiConfig,
    payload: &MarkdownPayload,
) -> Result<MarkdownResult> {
    let response = client
        .post(&config.base_url)
        .headers(config.headers.clone())
        .json(payload)
        .send()
        .await?;

    let status = response.status();
    if !status.is_success() {
        let body = response.text().await.unwrap_or_default();
        let truncated = match body.char_indices().nth(200) {
            Some((idx, _)) => &body[..idx],
            None => &body,
        };
        return Err(BrowserflareError::HttpError {
            status: status.as_u16(),
            body: truncated.to_string(),
        });
    }

    let result: MarkdownResult = response.json().await?;

    if !result.success {
        let error_msg = result
            .errors
            .as_ref()
            .map(|e| serde_json::to_value(e).unwrap_or_default())
            .unwrap_or_else(|| serde_json::json!({"error": "markdown fetch failed"}));
        return Err(BrowserflareError::ApiError(error_msg));
    }

    Ok(result)
}