#api-bindings #api #beatsaver #beatsaber #beastsaber #bsaber

beatsaver-rs

A Rust library for interacting with the beatsaver.com api

5 unstable releases

0.2.0 Jan 10, 2021
0.1.2 Nov 30, 2020
0.1.1 Nov 26, 2020
0.0.2 Nov 25, 2020
0.0.1 Nov 21, 2020

#1623 in #api

MIT license

2MB
4K SLoC

beatsaver-rs

Crates.io Documentation License GitHub issues Build

This project is a Rust library for interacting with the beatsaver.com api.

Installation

Add the following to your dependencies:

beatsaver-rs = "0.2.0"

Usage

Basic usage:

use beatsaver_rs::BeatSaverApi;
use beatsaver_rs::client::BeatSaver;
use beatsaver_rs::map::Map;
use bytes::Bytes;
use std::convert::TryInto;

#[tokio::main]
async fn main() {
    // Create a new client
    let client = BeatSaver::new();

    // Get map with key `1`
    let map: Map = client.map(&"1".try_into().unwrap()).await.unwrap();
    println!("Map by key: {}", map.name);

    // Get map with hash fda568fc27c20d21f8dc6f3709b49b5cc96723be
    let map: Map = client.map(&"fda568fc27c20d21f8dc6f3709b49b5cc96723be".try_into().unwrap()).await.unwrap();
    println!("Map by hash: {}", map.name);

    // Download map
    let map_download: Bytes = client.download((&map).into()).await.unwrap();
    let map_download: Bytes = client.download(&"1".try_into().unwrap()).await.unwrap();
    // save map somewhere
}

Iterators:

use beatsaver_rs::BeatSaverApi;
use beatsaver_rs::client::BeatSaver;
use beatsaver_rs::map::Map;

#[tokio::main]
async fn main() {
    // Create a new client
    let client = BeatSaver::new();
    
    // Get the latest maps
    let mut maps = client.maps_latest();
    
    // Iterate while there are more maps
    while let Some(map) = maps.next().await {
        match map {
            // We retrieved the map
            Ok(m) => println!(" => {}", m.name),
            // We were rate limited, wait the specified time
            Err(BeatSaverApiError::RateLimitError(r)) => {
                println!("Rate Limit: {:?}", r.reset_after);
                sleep(r.reset_after).await;
            }
            // Some other error, continue to try again, break to stop
            Err(e) => {
                println!("Error: {:?}", e),
                break;
            }
        }
    }
}

Backends

Currently, this crate supports three backends:

By default, reqwest is used, but you can specify a particular backend by enabling the [backend]_backend feature (for example, surf_backend).

Testing

When testing, make sure to enable all features to ensure all backends are tested properly:

cargo test --all-features

License

MIT

Dependencies

~4–19MB
~269K SLoC