1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//! This module contains the implementation of the Dify client.
//!
//! The `client` module provides a `Client` struct that represents a client for interacting with the Dify API.
//! It also includes a `Config` struct that holds the configuration for the client.
//! The client supports creating requests, executing them, and returning the response.
//! Additionally, it provides methods for creating form requests and handling multipart data.
//!
//! # Examples
//!
//! Creating a new client with default configuration:
//!
//! ```rust
//! use dify_client::client::Client;
//!
//! let client = Client::new("https://api.dify.ai", "API_KEY");
//! ```
//!
//! Creating a new client with custom configuration:
//!
//! ```rust
//! use dify_client::client::{Client, Config};
//! use std::time::Duration;
//!
//! let config = Config {
//!     base_url: "https://api.dify.ai".into(),
//!     api_key: "API_KEY".into(),
//!     timeout: Duration::from_secs(30),
//! };
//!
//! let client = Client::new_with_config(config);
//! ```
use super::{
    api::Api,
    http::{header, multipart, Method, Request, Response},
};
use anyhow::{bail, Result as AnyResult};
use std::{sync::Arc, time::Duration};

#[derive(Clone, Debug)]
/// The configuration for the Dify client.
pub struct Config {
    /// The base URL of the Dify API.
    pub base_url: String,
    /// The API key for the Dify API.
    pub api_key: String,
    /// The timeout for the client requests.
    pub timeout: Duration,
}

/// Implements the default configuration for the client.
impl Default for Config {
    /// Returns a new instance of `ClientConfig` with default values.
    fn default() -> Self {
        Self {
            base_url: "https://api.dify.ai".into(),
            api_key: "API_KEY".into(),
            timeout: Duration::from_secs(30),
        }
    }
}

/// The `Client` struct represents a client for interacting with the Dify API.
#[derive(Clone, Debug)]
pub struct Client {
    /// The configuration for the client.
    pub config: Arc<Config>,
    /// The HTTP client for sending requests.
    http_client: reqwest::Client,
}

/// The `Client` struct represents a client for interacting with the Dify API.
impl Client {
    /// Creates a new `Client` instance with the specified base URL and API key.
    ///
    /// # Arguments
    /// * `base_url` - The base URL of the Dify API.
    /// * `api_key` - The API key for authentication.
    ///
    /// # Returns
    /// A new `Client` instance.
    pub fn new(base_url: &str, api_key: &str) -> Self {
        Self::new_with_config(Config {
            base_url: base_url.into(),
            api_key: api_key.into(),
            ..Config::default()
        })
    }

    /// Creates a new `Client` instance with the specified configuration.
    ///
    /// # Arguments
    /// * `c` - The configuration for the client.
    ///
    /// # Returns
    /// A new `Client` instance.
    pub fn new_with_config(mut c: Config) -> Self {
        // format the base URL
        c.base_url = c.base_url.trim_end_matches("/").into();
        // build the http client
        let mut builder = reqwest::ClientBuilder::new();
        if !c.timeout.is_zero() {
            builder = builder.timeout(c.timeout);
        }
        let http_client = builder
            .default_headers(Self::default_headers(&c))
            .build()
            .expect("Failed to create http client");

        Self {
            config: Arc::new(c),
            http_client,
        }
    }

    /// Returns the default headers for the client.
    ///
    /// # Arguments
    /// * `c` - The configuration for the client.
    ///
    /// # Returns
    /// The default headers for the client.
    fn default_headers(c: &Config) -> header::HeaderMap {
        let mut headers = header::HeaderMap::new();
        headers.insert(
            header::CACHE_CONTROL,
            header::HeaderValue::from_static("no-cache"),
        );
        headers.insert(
            header::CONTENT_TYPE,
            header::HeaderValue::from_static("application/json; charset=utf-8"),
        );

        let auth = format!("Bearer {}", c.api_key);
        let mut bearer_auth = header::HeaderValue::from_str(&auth).unwrap();
        bearer_auth.set_sensitive(true);
        headers.insert(header::AUTHORIZATION, bearer_auth);
        headers
    }

    /// Returns the API for the client.
    /// The API provides methods for interacting with the Dify API.
    ///
    /// # Returns
    /// The API for the client.
    pub fn api(&self) -> Api {
        Api::new(self)
    }

    /// Creates a request with the specified URL, method, and data.
    ///
    /// # Arguments
    /// * `url` - The URL for the request.
    /// * `method` - The HTTP method for the request.
    /// * `data` - The data for the request.
    ///
    /// # Returns
    /// A `Result` containing the request or an error.
    ///
    /// # Errors
    /// Returns an error if the request cannot be created.
    ///
    /// # Panics
    /// Panics if the method is not supported.
    pub(crate) fn create_request<T>(
        &self,
        url: String,
        method: Method,
        data: T,
    ) -> AnyResult<Request>
    where
        T: serde::Serialize,
    {
        match method {
            Method::POST => {
                let r = self.http_client.post(url).json(&data).build()?;
                Ok(r)
            }
            Method::GET => {
                let r = self.http_client.get(url).query(&data).build()?;
                Ok(r)
            }
            Method::DELETE => {
                let r = self.http_client.delete(url).json(&data).build()?;
                Ok(r)
            }
            _ => bail!("Method not supported"),
        }
    }

    /// Creates a form request with the specified URL and data.
    ///
    /// # Arguments
    /// * `url` - The URL for the request.
    /// * `data` - The data for the request.
    ///
    /// # Returns
    /// A `Result` containing the request or an error.
    pub(crate) fn create_multipart_request(
        &self,
        url: String,
        form_data: multipart::Form,
    ) -> AnyResult<Request> {
        let r = self.http_client.post(url).multipart(form_data).build()?;
        Ok(r)
    }

    /// Executes the specified request and returns the response.
    ///
    /// # Arguments
    /// * `request` - The request to execute.
    ///
    /// # Returns
    /// A `Result` containing the response or an error.
    pub(crate) async fn execute(&self, request: Request) -> AnyResult<Response> {
        self.http_client.execute(request).await.map_err(Into::into)
    }
}