pub struct GuardianContentClient { /* private fields */ }
Expand description

The main asynchronous client used to build requests to send to the Guardian’s content API. This client maintains a private internal asynchronous client implemented by reqwest::Client

Implementations§

Constructor for the client. The constructor takes an API key which is then stored internally in the struct. The client then uses the builder pattern to add query parameters to the request. The query-building methods as well as the terminal operation take a mutable borrow of self. This allows the client not to be consumed after a request. As a result, the client should be initialised with the mut keyword.

API keys for the Guardian’s content API can be requested at https://open-platform.theguardian.com/access/

Example
let mut client = aletheia::GuardianContentClient::new("api-key-here");

Specify the Guardian API endpoint to target.

Can be one of:

  • Endpoint::Content (default): returns all pieces of content in the API.
  • Endpoint::Tags: returns all tags in the API. All Guardian content is manually categorised using these tags, of which there are more than 50,000.
  • Endpoint::Sections: returns all sections in the API.
  • Endpoint::Editions: returns all editions in the API. Editions are the different front pages of the Guardian site (currently, there are editions for the United Kingdom, the United States and Australia).
  • Endpoint::SingleItem: returns all the data for a given single item id. Here the term ‘item’ refers to either a piece of content, a tag, or a section. The item endpoint matches the paths on theguardian.com.
Example 1
let response = client
        .endpoint(Endpoint::Tags)
        .search("food")
        .send()
        .await?;
Example 2
let response = client
        .endpoint(Endpoint::SingleItem)
        .search("books/2022/jan/01/2022-in-books-highlights-for-the-year-ahead")
        .send()
        .await?;

Add a search query to the request.

Supports AND, OR and NOT operators, and exact phrase queries using double quotes. Examples of valid queries:

  • “Barack Obama”
  • Music
  • Programming AND coding

This field is only valid for the following endpoints:

Calling this method on Endpoint::SingleItem will have no effect.

Example
let response = client
        .search("Elections")
        .send()
        .await?;

Add a page number to the request.

Results are returned as a paginated list, with a default of 10 results. In order to page through the results, you can pass the page number as a parameter to this function.

Example
let response = client
        .search("Elections")
        .page(10)
        .send()
        .await?;

Attach a page size to the request.

Results are returned as a paginated list, with a default of 10 results. This function overrides the default. The page value must be between 0 and 200 for a successful response.

Example
let response = client
        .search("Elections")
        .page_size(20)
        .send()
        .await?;

Return results in the specified order.

The function only accepts one of three enums::OrderBy variants:

Example
let response = client
        .search("Elections")
        .order_by(OrderBy::Oldest)
        .send()
        .await?;

Change which type of date is used to order the results

The function only accepts one of three enums::OrderDate variants:

Example
let response = client
        .search("Elections")
        .order_by(OrderDate::NewspaperEdition)
        .send()
        .await?;

Add fields associated with the content.

The function accepts a vector of enums::Field variants, e.g.

If Field::All is included in the vector, it will override all other fields.

See https://open-platform.theguardian.com/documentation/search for more information on all the possible fields, or check the crate::enums section of the documentation.

Example
let response = client
        .search("Elections")
        .show_fields(vec![Field::StarRating, Field::ShortUrl])
        .send()
        .await?;

Add associated metadata tags.

The function accepts a vector of enums::Tag variants, e.g.

If Tag::All is included in the vector, it will override all other tags.

See https://open-platform.theguardian.com/documentation/search for more information on all the possible tags, or check the crate::enums section of the documentation.

Example
let response = client
        .search("Elections")
        .show_tags(vec![Tag::Contributor, Tag::Type, Tag::Tone])
        .send()
        .await?;

Specify in which indexed fields query terms should be searched on

The function accepts a vector of enums::Field variants, e.g.

See https://open-platform.theguardian.com/documentation/search for more information on all the possible fields, or check the crate::enums section of the documentation.

Example
let response = client
        .search("Elections")
        .query_fields(vec![Field::Body])
        .send()
        .await?;

Only return content published on or after the specified date.

Example
let response = client
        .search("Elections")
        .date_from(2020, 1, 1)
        .send()
        .await?;

Only return content published on or after the specified date.

It is more specific than date_from() as it accepts hours, minutes, seconds as well as a timezone offset.

Note: Providing invalid YMD-HMS does not append query parameters to the API request.

Example
let response = client
        .search("Elections")
        .datetime_from(2020, 1, 1, 12, 0, 0, 2)
        .send()
        .await?;

Only return content published on or before the specified date.

Example
let response = client
        .search("Elections")
        .date_from(2008, 1, 1)
        .date_to(2010, 12, 31)
        .send()
        .await?;

Only return content published on or before the specified date.

It is more specific than date_to() as it accepts hours, minutes, seconds as well as a timezone offset.

Note: Providing invalid YMD-HMS does not append query parameters to the API request.

Example
let response = client
        .search("Elections")
        .datetime_to(2016, 1, 1, 12, 0, 0, 5)
        .send()
        .await?;

Change which type of date is used to filter the results using date_from(), datetime_from(), date_to() and datetime_to().

The function only accepts one of four enums::UseDate variants:

Example
let response = client
        .search("Elections")
        .date_from(2015, 1, 1)
        .date_to(2018, 12, 31)
        .use_date(UseDate::FirstPublication)
        .send()
        .await?;

Add associated metadata section.

Example
let response = client
        .search("Elections")
        .show_section(true)
        .send()
        .await?;

Return only content in those sections.

Example
let response = client
        .search("Elections")
        .section("football")
        .send()
        .await?;

Return only content with those references.

Example
let response = client
        .search("Elections")
        .reference("isbn/9780718178949")
        .send()
        .await?;

Return only content with references of those types.

Example
let response = client
        .search("Elections")
        .reference_type("isbn")
        .send()
        .await?;

Return only content with those tags.

Example
let response = client
        .search("Elections")
        .tag("technology/apple")
        .send()
        .await?;

Return only content with those IDs.

Example
let response = client
        .search("Elections")
        .ids("world/2022/jan/01/funeral-of-desmond-tutu-takes-place-in-cape-town")
        .send()
        .await?;

Return only content from those production offices.

Example
let response = client
        .search("Elections")
        .production_office("UK")
        .send()
        .await?;

Return only content in those languages. Accepts ISO language codes, e.g. en, fr.

Example
let response = client
        .search("Elections")
        .lang("en")
        .send()
        .await?;

Return only content with a given star rating ranging from 1 to 5.

Example
let response = client
        .search("Elections")
        .star_rating(5)
        .send()
        .await?;

Only return tags of the specified type. Only valid if the endpoint is set to Endpoint::Tags

Example
let response = client
        .endpoint(Endpoint::Tags)
        .search("Elections")
        .tag_type("tv-and-radio/us-television")
        .send()
        .await?;

Add associated blocks (single block for content, one or more for liveblogs).

Supports the following enums::Block variants:

  • Block::Main
  • Block::Body
  • Block::All
  • Block::BodyLatest (limit defaults to 20)
  • [Block::BodyLatestWith(i32)] (override the limits)
  • Block::BodyOldest
  • [Block::BodyOldestWith(i32)]
  • [Block::BodyBlockId(&'a str)] (only the block with that ID)
  • [Block::BodyAroundBlockId(&'a str)] (the specified block and 20 blocks either side of it)
  • [Block::BodyAroundBlockIdWith(&'a str, i32)] (the specified block and n blocks either side of it)
  • Block::BodyKeyEvents
  • [Block::BodyPublishedSince(i64)] (only blocks since given timestamp)
Example
let response = client
        .endpoint(Endpoint::Tags)
        .search("Elections")
        .show_blocks(Block::BodyPublishedSince(1556529318000))
        .send()
        .await?;

Terminal operation that sends a GET request to the Guardian API. Once this function is called, all the query parameters constructed via the building methods are dropped.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

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

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more