pub struct ClientConfig {
pub api_key: Option<String>,
pub rate_limit: Option<f64>,
pub timeout: Duration,
pub user_agent: Option<String>,
pub base_url: Option<String>,
pub email: Option<String>,
pub tool: Option<String>,
pub retry_config: RetryConfig,
pub cache_config: Option<CacheConfig>,
}Expand description
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.
Fields§
§api_key: Option<String>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/
rate_limit: Option<f64>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.
timeout: DurationHTTP request timeout
Default: 30 seconds
user_agent: Option<String>Custom User-Agent string for HTTP requests
Default: “pubmed-client/{version}”
base_url: 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.
email: 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.
tool: Option<String>Tool name for identification (recommended by NCBI)
NCBI recommends including a tool name in requests. Default: “pubmed-client”
retry_config: RetryConfigRetry configuration for handling transient failures
Default: 3 retries with exponential backoff starting at 1 second
cache_config: Option<CacheConfig>Cache configuration for response caching
Default: disabled (None). Use ClientConfig::with_cache,
ClientConfig::with_redis_cache, or ClientConfig::with_sqlite_cache
to enable caching.
Implementations§
Source§impl ClientConfig
impl ClientConfig
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new configuration with default settings
§Example
use pubmed_client::config::ClientConfig;
let config = ClientConfig::new();Sourcepub fn with_api_key<S: Into<String>>(self, api_key: S) -> Self
pub fn with_api_key<S: Into<String>>(self, api_key: S) -> Self
Sourcepub fn with_rate_limit(self, rate: f64) -> Self
pub fn with_rate_limit(self, rate: f64) -> Self
Sourcepub fn with_timeout(self, timeout: Duration) -> Self
pub fn with_timeout(self, timeout: Duration) -> Self
Sourcepub fn with_timeout_seconds(self, timeout_seconds: u64) -> Self
pub fn with_timeout_seconds(self, timeout_seconds: u64) -> Self
Sourcepub fn with_user_agent<S: Into<String>>(self, user_agent: S) -> Self
pub fn with_user_agent<S: Into<String>>(self, user_agent: S) -> Self
Sourcepub fn with_base_url<S: Into<String>>(self, base_url: S) -> Self
pub fn with_base_url<S: Into<String>>(self, base_url: S) -> Self
Sourcepub fn with_email<S: Into<String>>(self, email: S) -> Self
pub fn with_email<S: Into<String>>(self, email: S) -> Self
Sourcepub fn with_retry_config(self, retry_config: RetryConfig) -> Self
pub fn with_retry_config(self, retry_config: RetryConfig) -> Self
Set retry configuration for handling transient failures
§Arguments
retry_config- Custom retry configuration
§Example
use pubmed_client::config::ClientConfig;
use pubmed_client::retry::RetryConfig;
use pubmed_client::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);Sourcepub fn with_cache(self) -> Self
pub fn with_cache(self) -> Self
Enable caching with default configuration
§Example
use pubmed_client::config::ClientConfig;
let config = ClientConfig::new()
.with_cache();Sourcepub fn with_cache_config(self, cache_config: CacheConfig) -> Self
pub fn with_cache_config(self, cache_config: CacheConfig) -> Self
Set cache configuration
§Arguments
cache_config- Custom cache configuration
§Example
use pubmed_client::config::ClientConfig;
use pubmed_client::cache::CacheConfig;
let cache_config = CacheConfig {
max_capacity: 5000,
..Default::default()
};
let config = ClientConfig::new()
.with_cache_config(cache_config);Sourcepub fn without_cache(self) -> Self
pub fn without_cache(self) -> Self
Disable all caching
§Example
use pubmed_client::config::ClientConfig;
let config = ClientConfig::new()
.without_cache();Sourcepub fn with_redis_cache(self, url: impl Into<String>) -> Self
pub fn with_redis_cache(self, url: impl Into<String>) -> Self
Enable Redis-backed caching.
Requires the cache-redis feature. The cache uses JSON serialisation
with per-entry TTL (default: 7 days from CacheConfig::default).
§Arguments
url- Redis connection URL, e.g."redis://127.0.0.1/"
§Example
use pubmed_client::config::ClientConfig;
let config = ClientConfig::new()
.with_redis_cache("redis://127.0.0.1/");Sourcepub fn with_sqlite_cache(self, path: impl Into<PathBuf>) -> Self
pub fn with_sqlite_cache(self, path: impl Into<PathBuf>) -> Self
Enable SQLite-backed caching.
Requires the cache-sqlite feature. Not available on WASM targets.
The database file is created automatically if it does not exist.
§Arguments
path- Path to the SQLite database file
§Example
use pubmed_client::config::ClientConfig;
let config = ClientConfig::new()
.with_sqlite_cache("/tmp/pubmed_cache.db");Sourcepub fn effective_rate_limit(&self) -> f64
pub fn effective_rate_limit(&self) -> f64
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
Sourcepub fn create_rate_limiter(&self) -> RateLimiter
pub fn create_rate_limiter(&self) -> RateLimiter
Sourcepub fn effective_base_url(&self) -> &str
pub fn effective_base_url(&self) -> &str
Get the base URL for E-utilities
Returns the configured base URL or the default NCBI E-utilities URL.
Sourcepub fn effective_user_agent(&self) -> String
pub fn effective_user_agent(&self) -> String
Get the User-Agent string
Returns the configured User-Agent or a default based on the crate name and version.
Sourcepub fn effective_tool(&self) -> &str
pub fn effective_tool(&self) -> &str
Get the tool name for NCBI identification
Returns the configured tool name or the default.
Sourcepub fn build_api_params(&self) -> Vec<(String, String)>
pub fn build_api_params(&self) -> Vec<(String, String)>
Build query parameters for NCBI API requests
This includes API key, email, and tool parameters when configured.
Trait Implementations§
Source§impl Clone for ClientConfig
impl Clone for ClientConfig
Source§fn clone(&self) -> ClientConfig
fn clone(&self) -> ClientConfig
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for ClientConfig
impl RefUnwindSafe for ClientConfig
impl Send for ClientConfig
impl Sync for ClientConfig
impl Unpin for ClientConfig
impl UnwindSafe for ClientConfig
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().