pubmed-client 0.1.0

An async Rust client for PubMed and PMC APIs for retrieving biomedical research articles
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
use crate::cache::CacheConfig;
use crate::rate_limit::RateLimiter;
use crate::retry::RetryConfig;
use crate::time::Duration;

/// Configuration options for PubMed and PMC clients
///
/// This configuration allows customization of rate limiting, API keys,
/// timeouts, and other client behavior to comply with NCBI guidelines
/// and optimize performance.
#[derive(Clone)]
pub struct ClientConfig {
    /// NCBI E-utilities API key for increased rate limits
    ///
    /// With an API key:
    /// - Rate limit increases from 3 to 10 requests per second
    /// - Better stability and reduced chance of blocking
    /// - Required for high-volume applications
    ///
    /// Get your API key at: <https://ncbiinsights.ncbi.nlm.nih.gov/2017/11/02/new-api-keys-for-the-e-utilities/>
    pub api_key: Option<String>,

    /// Rate limit in requests per second
    ///
    /// Default values:
    /// - 3.0 without API key (NCBI guideline)
    /// - 10.0 with API key (NCBI guideline)
    ///
    /// Setting this value overrides the automatic selection based on API key presence.
    pub rate_limit: Option<f64>,

    /// HTTP request timeout
    ///
    /// Default: 30 seconds
    pub timeout: Duration,

    /// Custom User-Agent string for HTTP requests
    ///
    /// Default: "pubmed-client-rs/{version}"
    pub user_agent: Option<String>,

    /// Base URL for NCBI E-utilities
    ///
    /// Default: <https://eutils.ncbi.nlm.nih.gov/entrez/eutils>
    /// This should rarely need to be changed unless using a proxy or test environment.
    pub base_url: Option<String>,

    /// Email address for identification (recommended by NCBI)
    ///
    /// NCBI recommends including an email address in requests for contact
    /// in case of problems. This is automatically added to requests.
    pub email: Option<String>,

    /// Tool name for identification (recommended by NCBI)
    ///
    /// NCBI recommends including a tool name in requests.
    /// Default: "pubmed-client-rs"
    pub tool: Option<String>,

    /// Retry configuration for handling transient failures
    ///
    /// Default: 3 retries with exponential backoff starting at 1 second
    pub retry_config: RetryConfig,

    /// Cache configuration for response caching
    ///
    /// Default: Memory-only cache with 1000 items max
    pub cache_config: Option<CacheConfig>,
}

impl ClientConfig {
    /// Create a new configuration with default settings
    ///
    /// # Example
    ///
    /// ```
    /// use pubmed_client_rs::config::ClientConfig;
    ///
    /// let config = ClientConfig::new();
    /// ```
    pub fn new() -> Self {
        Self {
            api_key: None,
            rate_limit: None,
            timeout: Duration::from_secs(30),
            user_agent: None,
            base_url: None,
            email: None,
            tool: None,
            retry_config: RetryConfig::default(),
            cache_config: None,
        }
    }

    /// Set the NCBI API key
    ///
    /// # Arguments
    ///
    /// * `api_key` - Your NCBI E-utilities API key
    ///
    /// # Example
    ///
    /// ```
    /// use pubmed_client_rs::config::ClientConfig;
    ///
    /// let config = ClientConfig::new()
    ///     .with_api_key("your_api_key_here");
    /// ```
    pub fn with_api_key<S: Into<String>>(mut self, api_key: S) -> Self {
        self.api_key = Some(api_key.into());
        self
    }

    /// Set a custom rate limit
    ///
    /// # Arguments
    ///
    /// * `rate` - Requests per second (must be positive)
    ///
    /// # Example
    ///
    /// ```
    /// use pubmed_client_rs::config::ClientConfig;
    ///
    /// // Custom rate limit of 5 requests per second
    /// let config = ClientConfig::new()
    ///     .with_rate_limit(5.0);
    /// ```
    pub fn with_rate_limit(mut self, rate: f64) -> Self {
        if rate > 0.0 {
            self.rate_limit = Some(rate);
        }
        self
    }

    /// Set the HTTP request timeout
    ///
    /// # Arguments
    ///
    /// * `timeout` - Maximum time to wait for HTTP responses
    ///
    /// # Example
    ///
    /// ```
    /// use pubmed_client_rs::config::ClientConfig;
    /// use pubmed_client_rs::time::Duration;
    ///
    /// let config = ClientConfig::new()
    ///     .with_timeout(Duration::from_secs(60));
    /// ```
    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Set the HTTP request timeout in seconds (convenience method)
    ///
    /// # Arguments
    ///
    /// * `timeout_seconds` - Maximum time to wait for HTTP responses in seconds
    ///
    /// # Example
    ///
    /// ```
    /// use pubmed_client_rs::config::ClientConfig;
    ///
    /// let config = ClientConfig::new()
    ///     .with_timeout_seconds(60);
    /// ```
    pub fn with_timeout_seconds(mut self, timeout_seconds: u64) -> Self {
        self.timeout = Duration::from_secs(timeout_seconds);
        self
    }

    /// Set a custom User-Agent string
    ///
    /// # Arguments
    ///
    /// * `user_agent` - Custom User-Agent for HTTP requests
    ///
    /// # Example
    ///
    /// ```
    /// use pubmed_client_rs::config::ClientConfig;
    ///
    /// let config = ClientConfig::new()
    ///     .with_user_agent("MyApp/1.0");
    /// ```
    pub fn with_user_agent<S: Into<String>>(mut self, user_agent: S) -> Self {
        self.user_agent = Some(user_agent.into());
        self
    }

    /// Set a custom base URL for NCBI E-utilities
    ///
    /// # Arguments
    ///
    /// * `base_url` - Base URL for E-utilities API
    ///
    /// # Example
    ///
    /// ```
    /// use pubmed_client_rs::config::ClientConfig;
    ///
    /// let config = ClientConfig::new()
    ///     .with_base_url("https://proxy.example.com/eutils");
    /// ```
    pub fn with_base_url<S: Into<String>>(mut self, base_url: S) -> Self {
        self.base_url = Some(base_url.into());
        self
    }

    /// Set email address for NCBI identification
    ///
    /// # Arguments
    ///
    /// * `email` - Your email address for NCBI contact
    ///
    /// # Example
    ///
    /// ```
    /// use pubmed_client_rs::config::ClientConfig;
    ///
    /// let config = ClientConfig::new()
    ///     .with_email("researcher@university.edu");
    /// ```
    pub fn with_email<S: Into<String>>(mut self, email: S) -> Self {
        self.email = Some(email.into());
        self
    }

    /// Set tool name for NCBI identification
    ///
    /// # Arguments
    ///
    /// * `tool` - Your application/tool name
    ///
    /// # Example
    ///
    /// ```
    /// use pubmed_client_rs::config::ClientConfig;
    ///
    /// let config = ClientConfig::new()
    ///     .with_tool("BioinformaticsApp");
    /// ```
    pub fn with_tool<S: Into<String>>(mut self, tool: S) -> Self {
        self.tool = Some(tool.into());
        self
    }

    /// Set retry configuration for handling transient failures
    ///
    /// # Arguments
    ///
    /// * `retry_config` - Custom retry configuration
    ///
    /// # Example
    ///
    /// ```
    /// use pubmed_client_rs::config::ClientConfig;
    /// use pubmed_client_rs::retry::RetryConfig;
    /// use pubmed_client_rs::time::Duration;
    ///
    /// let retry_config = RetryConfig::new()
    ///     .with_max_retries(5)
    ///     .with_initial_delay(Duration::from_secs(2));
    ///
    /// let config = ClientConfig::new()
    ///     .with_retry_config(retry_config);
    /// ```
    pub fn with_retry_config(mut self, retry_config: RetryConfig) -> Self {
        self.retry_config = retry_config;
        self
    }

    /// Enable caching with default configuration
    ///
    /// # Example
    ///
    /// ```
    /// use pubmed_client_rs::config::ClientConfig;
    ///
    /// let config = ClientConfig::new()
    ///     .with_cache();
    /// ```
    pub fn with_cache(mut self) -> Self {
        self.cache_config = Some(CacheConfig::default());
        self
    }

    /// Set cache configuration
    ///
    /// # Arguments
    ///
    /// * `cache_config` - Custom cache configuration
    ///
    /// # Example
    ///
    /// ```
    /// use pubmed_client_rs::config::ClientConfig;
    /// use pubmed_client_rs::cache::CacheConfig;
    ///
    /// let cache_config = CacheConfig {
    ///     max_capacity: 5000,
    ///     ..Default::default()
    /// };
    ///
    /// let config = ClientConfig::new()
    ///     .with_cache_config(cache_config);
    /// ```
    pub fn with_cache_config(mut self, cache_config: CacheConfig) -> Self {
        self.cache_config = Some(cache_config);
        self
    }

    /// Disable all caching
    ///
    /// # Example
    ///
    /// ```
    /// use pubmed_client_rs::config::ClientConfig;
    ///
    /// let config = ClientConfig::new()
    ///     .without_cache();
    /// ```
    pub fn without_cache(mut self) -> Self {
        self.cache_config = None;
        self
    }

    /// Get the effective rate limit based on configuration
    ///
    /// Returns the configured rate limit, or the appropriate default
    /// based on whether an API key is present.
    ///
    /// # Returns
    ///
    /// - Custom rate limit if set
    /// - 10.0 requests/second if API key is present
    /// - 3.0 requests/second if no API key
    pub fn effective_rate_limit(&self) -> f64 {
        self.rate_limit.unwrap_or_else(|| {
            if self.api_key.is_some() {
                10.0 // NCBI rate limit with API key
            } else {
                3.0 // NCBI rate limit without API key
            }
        })
    }

    /// Create a rate limiter based on this configuration
    ///
    /// # Returns
    ///
    /// A `RateLimiter` configured with the appropriate rate limit
    ///
    /// # Example
    ///
    /// ```
    /// use pubmed_client_rs::config::ClientConfig;
    ///
    /// let config = ClientConfig::new().with_api_key("your_key");
    /// let rate_limiter = config.create_rate_limiter();
    /// ```
    pub fn create_rate_limiter(&self) -> RateLimiter {
        RateLimiter::new(self.effective_rate_limit())
    }

    /// Get the base URL for E-utilities
    ///
    /// Returns the configured base URL or the default NCBI E-utilities URL.
    pub fn effective_base_url(&self) -> &str {
        self.base_url
            .as_deref()
            .unwrap_or("https://eutils.ncbi.nlm.nih.gov/entrez/eutils")
    }

    /// Get the User-Agent string
    ///
    /// Returns the configured User-Agent or a default based on the crate name and version.
    pub fn effective_user_agent(&self) -> String {
        self.user_agent.clone().unwrap_or_else(|| {
            let version = env!("CARGO_PKG_VERSION");
            format!("pubmed-client-rs/{version}")
        })
    }

    /// Get the tool name for NCBI identification
    ///
    /// Returns the configured tool name or the default.
    pub fn effective_tool(&self) -> &str {
        self.tool.as_deref().unwrap_or("pubmed-client-rs")
    }

    /// Build query parameters for NCBI API requests
    ///
    /// This includes API key, email, and tool parameters when configured.
    pub fn build_api_params(&self) -> Vec<(String, String)> {
        let mut params = Vec::new();

        if let Some(ref api_key) = self.api_key {
            params.push(("api_key".to_string(), api_key.clone()));
        }

        if let Some(ref email) = self.email {
            params.push(("email".to_string(), email.clone()));
        }

        params.push(("tool".to_string(), self.effective_tool().to_string()));

        params
    }
}

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

#[cfg(test)]
mod tests {
    use std::mem;

    use super::*;

    #[test]
    fn test_default_config() {
        let config = ClientConfig::new();
        assert!(config.api_key.is_none());
        assert!(config.rate_limit.is_none());
        assert_eq!(config.timeout, Duration::from_secs(30));
        assert_eq!(config.effective_rate_limit(), 3.0);
    }

    #[test]
    fn test_config_with_api_key() {
        let config = ClientConfig::new().with_api_key("test_key");
        assert_eq!(config.api_key.as_ref().unwrap(), "test_key");
        assert_eq!(config.effective_rate_limit(), 10.0);
    }

    #[test]
    fn test_custom_rate_limit() {
        let config = ClientConfig::new().with_rate_limit(5.0);
        assert_eq!(config.effective_rate_limit(), 5.0);

        // Custom rate limit overrides API key default
        let config_with_key = ClientConfig::new()
            .with_api_key("test")
            .with_rate_limit(7.0);
        assert_eq!(config_with_key.effective_rate_limit(), 7.0);
    }

    #[test]
    fn test_invalid_rate_limit() {
        let config = ClientConfig::new().with_rate_limit(-1.0);
        assert!(config.rate_limit.is_none());
        assert_eq!(config.effective_rate_limit(), 3.0);
    }

    #[test]
    fn test_fluent_interface() {
        let config = ClientConfig::new()
            .with_api_key("test_key")
            .with_rate_limit(5.0)
            .with_timeout(Duration::from_secs(60))
            .with_email("test@example.com")
            .with_tool("TestApp");

        assert_eq!(config.api_key.as_ref().unwrap(), "test_key");
        assert_eq!(config.effective_rate_limit(), 5.0);
        assert_eq!(config.timeout, Duration::from_secs(60));
        assert_eq!(config.email.as_ref().unwrap(), "test@example.com");
        assert_eq!(config.effective_tool(), "TestApp");
    }

    #[test]
    fn test_api_params() {
        let config = ClientConfig::new()
            .with_api_key("test_key")
            .with_email("test@example.com")
            .with_tool("TestApp");

        let params = config.build_api_params();
        assert_eq!(params.len(), 3);

        assert!(params.contains(&("api_key".to_string(), "test_key".to_string())));
        assert!(params.contains(&("email".to_string(), "test@example.com".to_string())));
        assert!(params.contains(&("tool".to_string(), "TestApp".to_string())));
    }

    #[test]
    fn test_effective_values() {
        let config = ClientConfig::new();

        assert_eq!(
            config.effective_base_url(),
            "https://eutils.ncbi.nlm.nih.gov/entrez/eutils"
        );
        assert!(config
            .effective_user_agent()
            .starts_with("pubmed-client-rs/"));
        assert_eq!(config.effective_tool(), "pubmed-client-rs");
    }

    #[test]
    fn test_rate_limiter_creation() {
        let config = ClientConfig::new().with_rate_limit(5.0);
        let rate_limiter = config.create_rate_limiter();
        // The rate limiter creation should succeed
        assert!(mem::size_of_val(&rate_limiter) > 0);
    }
}