Skip to main content

deepseek_sdk/tools/
fetch.rs

1use derive_builder::Builder;
2use serde::{Deserialize, Serialize};
3
4use crate::{DeepSeekClient, api_post, error::DeepSeekError};
5
6/// Request body for `POST /v1/tools/fetch`.
7#[derive(Clone, PartialEq, Serialize, Builder)]
8#[builder(
9    pattern = "owned",
10    setter(into, strip_option),
11    build_fn(validate = "Self::validate"),
12    name = "FetchRequestBuilder"
13)]
14pub struct FetchRequest {
15    #[serde(skip_serializing)]
16    pub client: DeepSeekClient,
17
18    /// **Possible values:** `non-empty` and `<= 8192 characters`
19    ///
20    /// The URL to fetch. Must be at most 8192 characters and an HTTP or HTTPS URL with a host.
21    /// The URL must not contain a username or password.
22    pub url: String,
23}
24
25impl FetchRequestBuilder {
26    fn validate(&self) -> Result<(), String> {
27        if let Some(url) = self.url.as_ref() {
28            if url.trim().is_empty() {
29                return Err("url must not be empty".to_string());
30            }
31            if url.chars().count() > 8192 {
32                return Err("url must be at most 8192 characters".to_string());
33            }
34        }
35
36        Ok(())
37    }
38}
39
40impl FetchRequest {
41    /// Send the fetch request.
42    pub async fn send(self) -> Result<FetchResponse, DeepSeekError> {
43        let client = self.client.clone();
44        api_post("/v1/tools/fetch", &self, client).await
45    }
46}
47
48/// Response from `POST /v1/tools/fetch`.
49#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
50pub struct FetchResponse {
51    /// The title of the page. Empty when the title is unavailable.
52    pub title: String,
53
54    /// The fetched page content as Markdown text.
55    pub content: String,
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61    use crate::DEFAULT_BASE_URL;
62    use serde_json::json;
63
64    fn client() -> DeepSeekClient {
65        DeepSeekClient::new("sk-...", DEFAULT_BASE_URL.clone())
66    }
67
68    #[test]
69    fn request_serializes_url_and_skips_client() {
70        let req = FetchRequestBuilder::default()
71            .client(client())
72            .url("https://example.com/article")
73            .build()
74            .unwrap();
75        let value = serde_json::to_value(&req).unwrap();
76        assert_eq!(value, json!({"url": "https://example.com/article"}));
77    }
78
79    #[test]
80    fn validation_rejects_invalid_urls() {
81        assert!(
82            FetchRequestBuilder::default()
83                .client(client())
84                .url("   ")
85                .build()
86                .is_err()
87        );
88        let long_url = format!("https://example.com/{}", "a".repeat(8192));
89        assert!(
90            FetchRequestBuilder::default()
91                .client(client())
92                .url(long_url)
93                .build()
94                .is_err()
95        );
96    }
97
98    #[test]
99    fn response_deserializes_api_example() {
100        let value = json!({
101            "title": "Example article",
102            "content": "# Example article\n\nArticle content."
103        });
104        let resp: FetchResponse = serde_json::from_value(value).unwrap();
105        assert_eq!(resp.title, "Example article");
106        assert!(resp.content.starts_with("# Example article"));
107    }
108}