pub struct PmcClient { /* private fields */ }Expand description
Client for interacting with PMC (PubMed Central) API
Implementations§
Source§impl PmcClient
impl PmcClient
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new PMC client with default configuration
Uses default NCBI rate limiting (3 requests/second) and no API key.
For production use, consider using with_config() to set an API key.
§Example
use pubmed_client::PmcClient;
let client = PmcClient::new();pub fn get_pmc_config(&self) -> &ClientConfig
pub fn get_cloud_client_config(&self) -> &ClientConfig
Sourcepub fn with_config(config: ClientConfig) -> Self
pub fn with_config(config: ClientConfig) -> Self
Create a new PMC client with custom configuration
§Arguments
config- Client configuration including rate limits, API key, etc.
§Example
use pubmed_client::{PmcClient, ClientConfig};
let config = ClientConfig::new()
.with_api_key("your_api_key_here")
.with_email("researcher@university.edu");
let client = PmcClient::with_config(config);Sourcepub fn with_client(client: Client) -> Self
pub fn with_client(client: Client) -> Self
Create a new PMC client with custom HTTP client and default configuration
§Arguments
client- Custom reqwest client with specific configuration
§Example
use pubmed_client::PmcClient;
use reqwest::Client;
use std::time::Duration;
let http_client = Client::builder()
.timeout(Duration::from_secs(30))
.build()
.unwrap();
let client = PmcClient::with_client(http_client);Sourcepub fn with_base_url(self, base_url: String) -> Self
pub fn with_base_url(self, base_url: String) -> Self
Sourcepub async fn fetch_full_text(&self, pmcid: &str) -> Result<PmcArticle>
pub async fn fetch_full_text(&self, pmcid: &str) -> Result<PmcArticle>
Fetch full text from PMC using PMCID
§Arguments
pmcid- PMC ID (with or without “PMC” prefix)
§Returns
Returns a Result<PmcArticle> containing the structured full text
§Errors
ParseError::PmcNotAvailable- If PMC full text is not availablePubMedError::RequestError- If the HTTP request failsParseError::XmlError- If XML parsing fails
§Example
use pubmed_client::PmcClient;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = PmcClient::new();
let full_text = client.fetch_full_text("PMC7906746").await?;
println!("Title: {}", full_text.title().unwrap_or("Untitled"));
println!("Sections: {}", full_text.sections().len());
Ok(())
}Sourcepub async fn check_pmc_availability(&self, pmid: &str) -> Result<Option<String>>
pub async fn check_pmc_availability(&self, pmid: &str) -> Result<Option<String>>
Check if PMC full text is available for a given PMID
§Arguments
pmid- PubMed ID
§Returns
Returns Result<Option<String>> containing the PMCID if available
§Example
use pubmed_client::PmcClient;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = PmcClient::new();
if let Some(pmcid) = client.check_pmc_availability("33515491").await? {
println!("PMC available: {}", pmcid);
let full_text = client.fetch_full_text(&pmcid).await?;
println!("Title: {}", full_text.title().unwrap_or("Untitled"));
} else {
println!("PMC not available");
}
Ok(())
}Sourcepub async fn is_oa_subset(&self, pmcid: &str) -> Result<OaSubsetInfo>
pub async fn is_oa_subset(&self, pmcid: &str) -> Result<OaSubsetInfo>
Check if a PMC article is in the OA (Open Access) subset
The OA subset contains articles with programmatic access to full-text XML. Some publishers restrict programmatic access even though the article may be viewable on the PMC website.
§Arguments
pmcid- PMC ID (with or without “PMC” prefix)
§Returns
Returns Result<OaSubsetInfo> containing detailed information about OA availability
§Example
use pubmed_client::PmcClient;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = PmcClient::new();
let oa_info = client.is_oa_subset("PMC7906746").await?;
if oa_info.is_oa_subset {
println!("Article is in OA subset");
if let Some(link) = oa_info.download_link {
println!("Download: {}", link);
}
} else {
println!("Article is NOT in OA subset");
if let Some(code) = oa_info.error_code {
println!("Reason: {}", code);
}
}
Ok(())
}Sourcepub async fn download_files<P: AsRef<Path>>(
&self,
pmcid: &str,
output_dir: P,
) -> Result<Vec<String>>
pub async fn download_files<P: AsRef<Path>>( &self, pmcid: &str, output_dir: P, ) -> Result<Vec<String>>
Download a PMC article’s files from the PMC OA Cloud (AWS S3) service
§Arguments
pmcid- PMC ID (with or without “PMC” prefix)output_dir- Directory to download the article’s files into
§Returns
Returns a Result<Vec<String>> containing the list of downloaded file paths
§Errors
ParseError::InvalidPmid- If the PMCID format is invalidPubMedError::RequestError- If the HTTP request failsParseError::IoError- If file operations failParseError::PmcNotAvailable- If the article is not available in OA
§Example
use pubmed_client::PmcClient;
use std::path::Path;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = PmcClient::new();
let output_dir = Path::new("./extracted_articles");
let files = client.download_files("PMC7906746", output_dir).await?;
for file in files {
println!("Downloaded: {}", file);
}
Ok(())
}Sourcepub async fn extract_figures_with_captions<P: AsRef<Path>>(
&self,
pmcid: &str,
output_dir: P,
) -> Result<Vec<ExtractedFigure>>
pub async fn extract_figures_with_captions<P: AsRef<Path>>( &self, pmcid: &str, output_dir: P, ) -> Result<Vec<ExtractedFigure>>
Download the article’s files and match figures with their captions from XML
§Arguments
pmcid- PMC ID (with or without “PMC” prefix)output_dir- Directory to download the article’s files into
§Returns
Returns a Result<Vec<ExtractedFigure>> containing figures with both XML metadata and file paths
§Errors
ParseError::InvalidPmid- If the PMCID format is invalidPubMedError::RequestError- If the HTTP request failsParseError::IoError- If file operations failParseError::PmcNotAvailable- If the article is not available in OA
§Example
use pubmed_client::PmcClient;
use std::path::Path;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = PmcClient::new();
let output_dir = Path::new("./extracted_articles");
let figures = client.extract_figures_with_captions("PMC7906746", output_dir).await?;
for figure in figures {
println!("Figure {}: {:?}", figure.figure.id, figure.figure.caption);
println!("File: {}", figure.extracted_file_path);
}
Ok(())
}Sourcepub async fn clear_cache(&self)
pub async fn clear_cache(&self)
Clear all cached PMC data
§Example
use pubmed_client::PmcClient;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = PmcClient::new();
client.clear_cache().await;
Ok(())
}Sourcepub fn cache_entry_count(&self) -> u64
pub fn cache_entry_count(&self) -> u64
Get cache statistics
Returns the number of items in cache, or 0 if caching is disabled
§Example
use pubmed_client::PmcClient;
let client = PmcClient::new();
let count = client.cache_entry_count();
println!("Cache entries: {}", count);Sourcepub async fn sync_cache(&self)
pub async fn sync_cache(&self)
Synchronize cache operations to ensure all pending operations are flushed
This is useful for testing to ensure cache statistics are accurate