use derive_builder::Builder;
use serde::{Deserialize, Serialize};
use crate::{DeepSeekClient, api_post, error::DeepSeekError};
#[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,
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 {
pub async fn send(self) -> Result<FetchResponse, DeepSeekError> {
let client = self.client.clone();
api_post("/v1/tools/fetch", &self, client).await
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct FetchResponse {
pub title: String,
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"));
}
}