Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ on:
pull_request:
branches: [ "main" ]

env:
CARGO_TERM_COLOR: always

jobs:
build:

Expand All @@ -29,6 +26,10 @@ jobs:
github.event_name == 'pull_request_target'
uses: 1Password/check-signed-commits-action@v1

- name: Install rust toolchain
uses: actions-rust-lang/setup-rust-toolchain@v1
with:
components: clippy,rustfmt
- name: Foramt check
run: |
cargo fmt --all -- --check
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ fn main() -> Result<()> {
let args = Args::parse();

let client = DeepSeekClientBuilder::new(args.api_key.clone())
.timeout(300)
.with_timeout(300)
.build()?;

let mut history = vec![];
Expand Down
110 changes: 106 additions & 4 deletions deepseek-api/src/client_builder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::{Ok, Result};
use std::env;
cfg_if::cfg_if! {
if #[cfg(feature = "is_sync")] {
use reqwest::blocking::ClientBuilder as ReqwestClientBuilder;
Expand All @@ -19,7 +20,7 @@ use std::time::Duration;
///
/// ```ignore
/// let client = DeepSeekClientBuilder::new("your_api_key".to_string())
/// .timeout(30)
/// .with_timeout(30)
/// .build()
/// .expect("Failed to build client");
/// ```
Expand All @@ -29,6 +30,27 @@ pub struct DeepSeekClientBuilder {
host: String,
}

impl Default for DeepSeekClientBuilder {
/// Creates a new `DeepSeekClientBuilder` instance with default settings.
///
/// This method attempts to retrieve the API key from the environment variable
/// `DEEPSEEK_API_KEY`. If the environment variable is not set, it uses a empty string as
/// default API key. The `timeout` is set to `None`, indicating
/// no default timeout, and the `host` is set to the default API URL `"https://api.deepseek.com"`.
///
/// # Returns
/// A new instance of `DeepSeekClientBuilder` with default values.
fn default() -> Self {
let api_key = env::var("DEEPSEEK_API_KEY").unwrap_or_else(|_| String::from(""));

DeepSeekClientBuilder {
api_key,
timeout: None,
host: String::from("https://api.deepseek.com"),
}
}
}

impl DeepSeekClientBuilder {
/// Creates a new `DeepSeekClientBuilder` with the specified API key.
///
Expand Down Expand Up @@ -60,13 +82,49 @@ impl DeepSeekClientBuilder {
/// The `DeepSeekClientBuilder` instance with the timeout configured.
/// ```ignore
/// let builder = DeepSeekClientBuilder::new("your_api_key".to_string())
/// .timeout(30);
/// .with_timeout(30);
/// ```
pub fn timeout(mut self, duration: u64) -> Self {
pub fn with_timeout(mut self, duration: u64) -> Self {
self.timeout = Some(duration);
self
}

/// Sets the host url for the client.
///
/// # Arguments
///
/// * `host` - A `string` value representing the host url.
///
/// # Returns
///
/// The `DeepSeekClientBuilder` instance with the host configured.
/// ```ignore
/// let builder = DeepSeekClientBuilder::new("your_api_key".to_string())
/// .with_host("https://api.deepseek.com");
/// ```
pub fn with_host(mut self, host: &str) -> Self {
self.host = host.to_string();
self
}

/// Sets the api key for the client.
///
/// # Arguments
///
/// * `api_key` - A `string` value representing the api key.
///
/// # Returns
///
/// The `DeepSeekClientBuilder` instance with the api key configured.
/// ```ignore
/// let builder = DeepSeekClientBuilder::new("your_api_key".to_string())
/// .with_api_key("your_api_key");
/// ```
pub fn with_api_key(mut self, api_key: &str) -> Self {
self.api_key = api_key.to_string();
self
}

/// Builds the `Client` instance using the configured options.
///
/// # Returns
Expand All @@ -83,7 +141,7 @@ impl DeepSeekClientBuilder {
///
/// ```ignore
/// let client = DeepSeekClientBuilder::new("your_api_key".to_string())
/// .timeout(30)
/// .with_timeout(30)
/// .build()
/// .expect("Failed to build client");
/// ```
Expand All @@ -105,3 +163,47 @@ impl DeepSeekClientBuilder {
})
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::env;

#[test]
fn test_deep_seek_client_builder_from_env_var() {
env::set_var("DEEPSEEK_API_KEY", "test_api_key");

let builder = DeepSeekClientBuilder::default().with_timeout(15);

assert_eq!(builder.host, "https://api.deepseek.com");
assert_eq!(builder.timeout, Some(15));
assert_eq!(builder.api_key, "test_api_key");

assert!(builder.build().is_ok());
}

#[test]
fn test_deep_seek_client_override_options() {
// Build the client using the builder with a provided API key
let builder = DeepSeekClientBuilder::new("test_api_key".to_string())
.with_host("http://override.com")
.with_api_key("another_test_api_keyu");

assert_eq!(builder.host, "http://override.com");
assert_eq!(builder.api_key, "another_test_api_keyu");

assert!(builder.build().is_ok());
}

#[test]
fn test_deep_seek_client_builder_from_new_function() {
// Build the client using the builder with a provided API key
let builder = DeepSeekClientBuilder::new("test_api_key".to_string()).with_timeout(20);

assert_eq!(builder.host, "https://api.deepseek.com");
assert_eq!(builder.timeout, Some(20));
assert_eq!(builder.api_key, "test_api_key");

assert!(builder.build().is_ok());
}
}
2 changes: 1 addition & 1 deletion examples/sync-basic/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn main() -> Result<()> {
let args = Args::parse();

let client = DeepSeekClientBuilder::new(args.api_key.clone())
.timeout(300)
.with_timeout(300)
.build()?;

let mut history = vec![];
Expand Down