klbfw 0.1.3

Comprehensive REST API client with OAuth2, API key authentication, and file upload support
Documentation
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use crate::apikey::ApiKey;
use crate::client::Config;
use crate::error::{RestError, Result};
use crate::response::Response;
use crate::token::Token;
use serde::Serialize;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::Duration;

/// Overall request timeout for REST calls.
const REST_TIMEOUT: Duration = Duration::from_secs(300);
/// Connection establishment timeout.
const CONNECT_TIMEOUT: Duration = Duration::from_secs(10);

/// Client for REST API requests.
///
/// Holds the configuration, optional authentication (token or API key), and any
/// custom headers, and exposes methods to make requests.
#[derive(Clone)]
pub struct Client {
    /// Configuration
    config: Config,
    /// Optional authentication token (shared so renewals persist across calls)
    token: Arc<Mutex<Option<Token>>>,
    /// Optional API key
    api_key: Option<ApiKey>,
    /// Extra headers applied to every request (in insertion order)
    headers: Vec<(String, String)>,
}

impl Client {
    /// Create a new REST context with default configuration
    pub fn new() -> Self {
        Client {
            config: Config::default(),
            token: Arc::new(Mutex::new(None)),
            api_key: None,
            headers: Vec::new(),
        }
    }

    /// Create a new REST context with custom configuration
    pub fn with_config(config: Config) -> Self {
        Client {
            config,
            token: Arc::new(Mutex::new(None)),
            api_key: None,
            headers: Vec::new(),
        }
    }

    /// Set the authentication token
    pub fn with_token(self, token: Token) -> Self {
        *self.token.lock().unwrap() = Some(token);
        self
    }

    /// Set the API key
    pub fn with_api_key(mut self, api_key: ApiKey) -> Self {
        self.api_key = Some(api_key);
        self
    }

    /// Add a custom header applied to every request (builder style).
    ///
    /// Custom headers are sent in addition to the headers the client sets
    /// automatically (`Authorization`, `Content-Type`, ...); they do not
    /// replace them. Call multiple times to add several headers, including
    /// repeated header names.
    pub fn with_header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.headers.push((name.into(), value.into()));
        self
    }

    /// Add several custom headers applied to every request (builder style).
    ///
    /// Headers are appended to any already set; see [`with_header`](Self::with_header).
    pub fn with_headers<I, K, V>(mut self, headers: I) -> Self
    where
        I: IntoIterator<Item = (K, V)>,
        K: Into<String>,
        V: Into<String>,
    {
        self.headers
            .extend(headers.into_iter().map(|(k, v)| (k.into(), v.into())));
        self
    }

    /// Add a custom header applied to every request (in place).
    pub fn set_header(&mut self, name: impl Into<String>, value: impl Into<String>) {
        self.headers.push((name.into(), value.into()));
    }

    /// The custom headers configured on this context.
    pub fn headers(&self) -> &[(String, String)] {
        &self.headers
    }

    /// Enable debug mode
    pub fn with_debug(mut self, debug: bool) -> Self {
        self.config.set_debug(debug);
        self
    }

    /// Get the configuration
    pub fn config(&self) -> &Config {
        &self.config
    }

    /// Make a REST API request and unmarshal the response data into the target type
    ///
    /// # Arguments
    /// * `path` - API endpoint path
    /// * `method` - HTTP method (GET, POST, PUT, etc.)
    /// * `param` - Request parameters or body content
    ///
    /// # Returns
    /// The unmarshaled response data of type T
    pub fn apply<T, P>(&self, path: &str, method: &str, param: P) -> Result<T>
    where
        T: serde::de::DeserializeOwned,
        P: Serialize,
    {
        let response = self.do_request(path, method, param)?;
        response.apply()
    }

    /// Execute a REST API request and return the raw Response object
    ///
    /// # Arguments
    /// * `path` - API endpoint path
    /// * `method` - HTTP method (GET, POST, PUT, etc.)
    /// * `param` - Request parameters or body content
    ///
    /// # Returns
    /// The raw Response object
    pub fn do_request<P>(&self, path: &str, method: &str, param: P) -> Result<Response>
    where
        P: Serialize,
    {
        let param_json = serde_json::to_value(param)?;
        self.request_inner(path, method, &param_json, true)
    }

    /// Inner request implementation.
    ///
    /// `allow_renew` guards token renewal so an expired token triggers exactly
    /// one retry.
    fn request_inner(
        &self,
        path: &str,
        method: &str,
        param_json: &serde_json::Value,
        allow_renew: bool,
    ) -> Result<Response> {
        // Build base URL
        let base_url = self.config.base_url();
        let url = format!("{}/_special/rest/{}", base_url, path);

        let mut query_params: HashMap<String, String> = HashMap::new();
        let mut body_bytes: Vec<u8> = Vec::new();

        match method {
            "GET" | "HEAD" | "OPTIONS" => {
                // Parameters go in query string
                let param_str = serde_json::to_string(param_json)?;
                query_params.insert("_".to_string(), param_str);
            }
            "PUT" | "POST" | "PATCH" => {
                // Parameters go in request body
                body_bytes = serde_json::to_vec(param_json)?;
            }
            "DELETE" => {
                // No parameters
            }
            _ => {
                return Err(RestError::RequestBuild(format!(
                    "Unsupported HTTP method: {}",
                    method
                )))
            }
        }

        // Apply API key authentication if present
        if let Some(ref api_key) = self.api_key {
            api_key.apply_params(method, path, &mut query_params, &body_bytes)?;
        }

        // Build the full URL with an (optional) query string.
        let full_url = if query_params.is_empty() {
            url
        } else {
            let query = form_urlencoded::Serializer::new(String::new())
                .extend_pairs(query_params.iter())
                .finish();
            format!("{}?{}", url, query)
        };

        // Snapshot the current token (used only when not authenticating by key).
        let current_token = if self.api_key.is_none() {
            self.token.lock().unwrap().clone()
        } else {
            None
        };

        // Build the request.
        let mut request = rsurl::Request::new(method, &full_url)?
            .header("Sec-Rest-Http", "false")
            .max_time(REST_TIMEOUT)
            .connect_timeout(CONNECT_TIMEOUT);

        // Apply user-supplied custom headers before the client-managed ones so
        // that Authorization/Content-Type set below take precedence.
        for (name, value) in &self.headers {
            request = request.header(name, value);
        }

        if let Some(ref token) = current_token {
            request = request.header("Authorization", &format!("Bearer {}", token.access_token));
        }

        if !body_bytes.is_empty() {
            request = request
                .header("Content-Type", "application/json")
                .body(body_bytes);
        }

        // Execute request
        let start = std::time::Instant::now();
        let http_response = request.send()?;
        let status = http_response.status;

        // Get X-Request-Id header
        let request_id = http_response.header("X-Request-Id").map(|s| s.to_string());

        let body = http_response.body;

        if self.config.debug() {
            let duration = start.elapsed();
            eprintln!(
                "[rest] {} {} => {:?} (status: {})",
                method, path, duration, status
            );
        }

        // Parse response
        let mut response: Response = serde_json::from_slice(&body).map_err(|e| {
            if !(200..400).contains(&status) {
                RestError::http(
                    status,
                    String::from_utf8_lossy(&body).to_string(),
                    Some(Box::new(e)),
                )
            } else {
                RestError::Json(e)
            }
        })?;

        response.request_id = request_id;

        // Check for token expiration and renew if needed
        if allow_renew {
            if let Some(token) = current_token {
                if response.token.as_deref() == Some("invalid_request_token")
                    && response.extra.as_deref() == Some("token_expired")
                {
                    if self.config.debug() {
                        eprintln!("[rest] Token expired, attempting renewal");
                    }

                    // Renew and persist the new token so later calls reuse it.
                    let renewed = self.renew_token(&token)?;
                    *self.token.lock().unwrap() = Some(renewed);

                    // Retry the request once with the renewed token.
                    return self.request_inner(path, method, param_json, false);
                }
            }
        }

        // Check for redirect
        if response.result == "redirect" {
            if response.exception.as_deref() == Some("Exception\\Login") {
                return Err(RestError::LoginRequired);
            }
            return Err(RestError::from_response(response));
        }

        // Check for error response
        if response.result == "error" {
            return Err(RestError::from_response(response));
        }

        Ok(response)
    }

    /// Renew an expired token, returning the renewed token.
    fn renew_token(&self, token: &Token) -> Result<Token> {
        if !token.has_client_id() {
            return Err(RestError::NoClientId);
        }
        if !token.has_refresh_token() {
            return Err(RestError::NoRefreshToken);
        }

        // Create a context without token to avoid recursion, preserving any
        // custom headers so they apply to the renewal request too.
        let ctx = Client {
            config: self.config.clone(),
            token: Arc::new(Mutex::new(None)),
            api_key: None,
            headers: self.headers.clone(),
        };

        let mut params = HashMap::new();
        params.insert("grant_type", "refresh_token");
        params.insert("client_id", &token.client_id);
        params.insert("refresh_token", &token.refresh_token);
        params.insert("noraw", "true");

        let mut renewed: Token = ctx.apply("OAuth2:token", "POST", params)?;

        // The renewal response does not echo the client_id; carry it over so
        // the token remains renewable.
        renewed.client_id = token.client_id.clone();

        Ok(renewed)
    }
}

impl Default for Client {
    fn default() -> Self {
        Self::new()
    }
}

/// Deprecated alias for [`Client`].
///
/// The type was renamed to [`Client`] to better match Rust conventions; this
/// alias keeps existing code compiling.
#[deprecated(
    since = "0.1.3",
    note = "renamed to `Client`; use `klbfw::Client` instead"
)]
pub type RestContext = Client;

/// Convenience function to create a new REST context and make a request
pub fn apply<T, P>(path: &str, method: &str, param: P) -> Result<T>
where
    T: serde::de::DeserializeOwned,
    P: Serialize,
{
    Client::new().apply(path, method, param)
}

/// Convenience function to create a new REST context and execute a request
pub fn do_request<P>(path: &str, method: &str, param: P) -> Result<Response>
where
    P: Serialize,
{
    Client::new().do_request(path, method, param)
}

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

    #[test]
    fn test_rest_context_creation() {
        let ctx = Client::new();
        assert_eq!(ctx.config().scheme(), "https");
        assert_eq!(ctx.config().host(), "www.atonline.com");
    }

    #[test]
    fn test_rest_context_with_config() {
        let config = Config::new("http".to_string(), "localhost:8080".to_string());
        let ctx = Client::with_config(config);
        assert_eq!(ctx.config().scheme(), "http");
        assert_eq!(ctx.config().host(), "localhost:8080");
    }

    #[test]
    fn test_custom_headers() {
        let ctx = Client::new()
            .with_header("X-Custom", "one")
            .with_headers([("X-A", "a"), ("X-B", "b")]);
        assert_eq!(
            ctx.headers(),
            &[
                ("X-Custom".to_string(), "one".to_string()),
                ("X-A".to_string(), "a".to_string()),
                ("X-B".to_string(), "b".to_string()),
            ]
        );

        let mut ctx = ctx;
        ctx.set_header("X-C", "c");
        assert_eq!(ctx.headers().len(), 4);
    }

    #[test]
    #[allow(deprecated)]
    fn test_rest_context_alias() {
        // The deprecated alias still resolves to `Client`.
        let ctx: RestContext = RestContext::new();
        assert_eq!(ctx.config().host(), "www.atonline.com");
    }
}