pub struct NewsApiClient { /* private fields */ }Expand description
The main NewsAPI client.
Holds your API key, HTTP client configuration, and provides methods for all API endpoints.
All methods are asynchronous and must be awaited.
§Examples
use newsapi_rust::{NewsApiClient, TopHeadlinesParams};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = NewsApiClient::new("YOUR_API_KEY");
let params = TopHeadlinesParams::new().country("us").category("technology");
let headlines = client.top_headlines(¶ms).await?;
for article in headlines.articles {
println!("{} - {}", article.title, article.url);
}
Ok(())
}Implementations§
Source§impl NewsApiClient
impl NewsApiClient
Sourcepub fn new(api_key: impl Into<String>) -> Self
pub fn new(api_key: impl Into<String>) -> Self
Create a new NewsApiClient with the given API key.
Uses default settings for timeout (30 seconds) and API version (v2).
Sourcepub fn with_config(
api_key: impl Into<String>,
timeout: Duration,
version: ApiVersion,
) -> Self
pub fn with_config( api_key: impl Into<String>, timeout: Duration, version: ApiVersion, ) -> Self
Create a new NewsApiClient with custom configuration.
Sourcepub fn with_timeout(api_key: impl Into<String>, timeout: Duration) -> Self
pub fn with_timeout(api_key: impl Into<String>, timeout: Duration) -> Self
Create a new NewsApiClient with a custom timeout.
Sourcepub fn with_version(api_key: impl Into<String>, version: ApiVersion) -> Self
pub fn with_version(api_key: impl Into<String>, version: ApiVersion) -> Self
Create a new NewsApiClient with a specific API version.
Sourcepub fn set_version(&mut self, version: ApiVersion)
pub fn set_version(&mut self, version: ApiVersion)
Set the API version for this client.
Sourcepub fn version(&self) -> ApiVersion
pub fn version(&self) -> ApiVersion
Get the current API version.
Sourcepub fn set_timeout(&mut self, timeout: Duration) -> Result<()>
pub fn set_timeout(&mut self, timeout: Duration) -> Result<()>
Set the timeout for HTTP requests. Rebuilds the internal HTTP client with the new timeout.
Sourcepub async fn top_headlines(
&self,
params: &TopHeadlinesParams,
) -> Result<SuccessResponse>
pub async fn top_headlines( &self, params: &TopHeadlinesParams, ) -> Result<SuccessResponse>
Sourcepub async fn top_headlines_with_timeout(
&self,
params: &TopHeadlinesParams,
timeout: Option<Duration>,
) -> Result<SuccessResponse>
pub async fn top_headlines_with_timeout( &self, params: &TopHeadlinesParams, timeout: Option<Duration>, ) -> Result<SuccessResponse>
Fetch top headlines using /top-headlines endpoint with the given parameters and an optional timeout.
§Arguments
params- A reference toTopHeadlinesParamscontaining the query parameters.timeout- An optionalDurationspecifying the timeout for the request.
§Returns
A Result containing SuccessResponse on success or NewsApiError on failure.
Sourcepub async fn everything(
&self,
params: &EverythingParams,
) -> Result<SuccessResponse>
pub async fn everything( &self, params: &EverythingParams, ) -> Result<SuccessResponse>
Sourcepub async fn everything_with_timeout(
&self,
params: &EverythingParams,
timeout: Option<Duration>,
) -> Result<SuccessResponse>
pub async fn everything_with_timeout( &self, params: &EverythingParams, timeout: Option<Duration>, ) -> Result<SuccessResponse>
Fetch all articles using the /everything endpoint with the given parameters and an optional timeout.
§Arguments
params- A reference toEverythingParamscontaining the query parameters.timeout- An optionalDurationspecifying the timeout for the request.
§Returns
A Result containing SuccessResponse on success or NewsApiError on failure.
Sourcepub async fn sources(&self, params: &SourceParams) -> Result<Vec<SourceDetail>>
pub async fn sources(&self, params: &SourceParams) -> Result<Vec<SourceDetail>>
Sourcepub async fn sources_with_timeout(
&self,
params: &SourceParams,
timeout: Option<Duration>,
) -> Result<Vec<SourceDetail>>
pub async fn sources_with_timeout( &self, params: &SourceParams, timeout: Option<Duration>, ) -> Result<Vec<SourceDetail>>
Fetch news sources using the /sources endpoint with the given parameters and an optional timeout.
§Arguments
params- A reference toSourceParamscontaining the query parameters.timeout- An optionalDurationspecifying the timeout for the request.
§Returns
A Result containing a vector of SourceDetail on success or NewsApiError on failure.