deepseek-sdk 0.6.0

DeepSeek API client for Rust.
Documentation
use derive_builder::Builder;
use serde::{Deserialize, Serialize};

use crate::{DeepSeekClient, api_post, error::DeepSeekError};

/// Request body for `POST /v1/tools/fetch`.
#[derive(Clone, PartialEq, Serialize, Builder)]
#[builder(
    pattern = "owned",
    setter(into, strip_option),
    build_fn(validate = "Self::validate"),
    name = "FetchRequestBuilder"
)]
pub struct FetchRequest {
    #[serde(skip_serializing)]
    pub client: DeepSeekClient,

    /// **Possible values:** `non-empty` and `<= 8192 characters`
    ///
    /// The URL to fetch. Must be at most 8192 characters and an HTTP or HTTPS URL with a host.
    /// The URL must not contain a username or password.
    pub url: String,
}

impl FetchRequestBuilder {
    fn validate(&self) -> Result<(), String> {
        if let Some(url) = self.url.as_ref() {
            if url.trim().is_empty() {
                return Err("url must not be empty".to_string());
            }
            if url.chars().count() > 8192 {
                return Err("url must be at most 8192 characters".to_string());
            }
        }

        Ok(())
    }
}

impl FetchRequest {
    /// Send the fetch request.
    pub async fn send(self) -> Result<FetchResponse, DeepSeekError> {
        let client = self.client.clone();
        api_post("/v1/tools/fetch", &self, client).await
    }
}

/// Response from `POST /v1/tools/fetch`.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct FetchResponse {
    /// The title of the page. Empty when the title is unavailable.
    pub title: String,

    /// The fetched page content as Markdown text.
    pub content: String,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::DEFAULT_BASE_URL;
    use serde_json::json;

    fn client() -> DeepSeekClient {
        DeepSeekClient::new("sk-...", DEFAULT_BASE_URL.clone())
    }

    #[test]
    fn request_serializes_url_and_skips_client() {
        let req = FetchRequestBuilder::default()
            .client(client())
            .url("https://example.com/article")
            .build()
            .unwrap();
        let value = serde_json::to_value(&req).unwrap();
        assert_eq!(value, json!({"url": "https://example.com/article"}));
    }

    #[test]
    fn validation_rejects_invalid_urls() {
        assert!(
            FetchRequestBuilder::default()
                .client(client())
                .url("   ")
                .build()
                .is_err()
        );
        let long_url = format!("https://example.com/{}", "a".repeat(8192));
        assert!(
            FetchRequestBuilder::default()
                .client(client())
                .url(long_url)
                .build()
                .is_err()
        );
    }

    #[test]
    fn response_deserializes_api_example() {
        let value = json!({
            "title": "Example article",
            "content": "# Example article\n\nArticle content."
        });
        let resp: FetchResponse = serde_json::from_value(value).unwrap();
        assert_eq!(resp.title, "Example article");
        assert!(resp.content.starts_with("# Example article"));
    }
}