Skip to main content

NewsApiClient

Struct NewsApiClient 

Source
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(&params).await?;
    for article in headlines.articles {
        println!("{} - {}", article.title, article.url);
    }
    Ok(())
}

Implementations§

Source§

impl NewsApiClient

Source

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).

Source

pub fn with_config( api_key: impl Into<String>, timeout: Duration, version: ApiVersion, ) -> Self

Create a new NewsApiClient with custom configuration.

Source

pub fn with_timeout(api_key: impl Into<String>, timeout: Duration) -> Self

Create a new NewsApiClient with a custom timeout.

Source

pub fn with_version(api_key: impl Into<String>, version: ApiVersion) -> Self

Create a new NewsApiClient with a specific API version.

Source

pub fn set_version(&mut self, version: ApiVersion)

Set the API version for this client.

Source

pub fn version(&self) -> ApiVersion

Get the current API version.

Source

pub fn set_timeout(&mut self, timeout: Duration) -> Result<()>

Set the timeout for HTTP requests. Rebuilds the internal HTTP client with the new timeout.

Source

pub async fn top_headlines( &self, params: &TopHeadlinesParams, ) -> Result<SuccessResponse>

Fetch top headlines using /top-headlines endpoint with the given parameters.

§Arguments
  • params - A reference to TopHeadlinesParams containing the query parameters.
§Returns

A Result containing SuccessResponse on success or NewsApiError on failure.

Source

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 to TopHeadlinesParams containing the query parameters.
  • timeout - An optional Duration specifying the timeout for the request.
§Returns

A Result containing SuccessResponse on success or NewsApiError on failure.

Source

pub async fn everything( &self, params: &EverythingParams, ) -> Result<SuccessResponse>

Fetch all articles using the /everything endpoint with the given parameters.

§Arguments
  • params - A reference to EverythingParams containing the query parameters.
§Returns

A Result containing SuccessResponse on success or NewsApiError on failure.

Source

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 to EverythingParams containing the query parameters.
  • timeout - An optional Duration specifying the timeout for the request.
§Returns

A Result containing SuccessResponse on success or NewsApiError on failure.

Source

pub async fn sources(&self, params: &SourceParams) -> Result<Vec<SourceDetail>>

Fetch news sources using the /sources endpoint with the given parameters.

§Arguments
  • params - A reference to SourceParams containing the query parameters.
§Returns

A Result containing a vector of SourceDetail on success or NewsApiError on failure.

Source

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 to SourceParams containing the query parameters.
  • timeout - An optional Duration specifying the timeout for the request.
§Returns

A Result containing a vector of SourceDetail on success or NewsApiError on failure.

Trait Implementations§

Source§

impl Clone for NewsApiClient

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for NewsApiClient

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more