Module api

Source
Expand description

API endpoint modules

All endpoints use a builder pattern to construct their parameters.

§Example

use futures::{StreamExt, TryStreamExt};
use serde::Deserialize;
use speedrun_api::{
    api::{self, AsyncQuery, PagedEndpointExt},
    error::SpeedrunApiResult,
    SpeedrunApiBuilder,
};

// The return type for the endpoints we are interested in. More information
// is returned by the endpoints, but you can deserialize only what is needed.
#[derive(Debug, Deserialize)]
struct Game {
    names: Names,
    weblink: String,
}

#[derive(Debug, Deserialize)]
struct Names {
    international: String,
}

#[tokio::main]
pub async fn main() -> SpeedrunApiResult<()> {
    // Create a new client
    let client = SpeedrunApiBuilder::new().build_async()?;

    // Create an endpoint. This endpoint gets Super Mario Sunshine.
    let endpoint = api::games::Game::builder().id("v1pxjz68").build().unwrap();
    // Call the endpoint. The return type decides how to represent the returned value.
    let game: Game = endpoint.query_async(&client).await?;

    // Create a paginated endpoint. This retrievs a list of all games.
    let paginated_endpoint = api::games::Games::builder().build().unwrap();
    // The `PagedEndpointExt` adapters consume the endpoint, so we create a copy.
    let async_stream = paginated_endpoint.clone();
    // Call the `PagedEndpointExt::stream()` method to get an async Stream of results.
    let games: Vec<Game> = async_stream.stream(&client).take(200).try_collect().await?;
    // Call the `PagedEndpointExt::single_page()` method to retrieve a single page builder.
    // This retrieves 100 results starting at offset 100.
    let single_page_endpoint = paginated_endpoint
        .single_page()
        .offset(100)
        .max(100)
        .build()
        .unwrap();
    // This wrapped endpoint can be queried like any normal endpoint, but always returns a
    // `Vec<T: Deserialize>`.
    let games: Vec<Game> = single_page_endpoint.query_async(&client).await?;
}

Re-exports§

pub use crate::types::Root;

Modules§

categories
Categories
developers
Developers
engines
Engines
games
Games
gametypes
Game types
genres
Genres
guests
Guests
leaderboards
Leaderboards
levels
Levels
notifications
Notifications
platforms
Platforms
profile
Profile
publishers
Publishers
regions
Regions
runs
Runs
series
Series
users
Users
variables
Variables

Structs§

PagedIter
Iterator type for the iter method on PagedEndpointExt.
SinglePage
Represents a single page of elements.
SinglePageBuilder
Builder for the SinglePage endpoint

Enums§

ApiError
Errors that occur from API endpoints.
CategoriesSorting
Sorting options for categories
Direction
Sort direction
VariablesSorting
Sorting options for variables

Traits§

AsyncClient
A trait representing an asynchronous client which can communicate with speedrun.com
AsyncQuery
Asynchronous query made to a client.
Client
A trait representing a client which can communicate with speedrun.com
Pageable
Marker trait to indicate that an endpoint is pageable.
PagedEndpointExt
Adapters specific to Pageable endpoints.
Query
Query made to a client.
RestClient
A trait representing a client which can communicate with speedrun.com via REST