#vtubestudio

vtubestudio

A library for interacting with the VTube Studio API

13 releases (7 breaking)

0.8.0 Oct 29, 2022
0.6.1 Jun 18, 2022
0.5.1 Feb 20, 2022
0.4.0 Nov 21, 2021

#201 in Authentication

Download history 29/week @ 2023-02-01 41/week @ 2023-02-08 58/week @ 2023-02-15 14/week @ 2023-02-22 8/week @ 2023-03-01 22/week @ 2023-03-08 19/week @ 2023-03-15 39/week @ 2023-03-22 9/week @ 2023-03-29 22/week @ 2023-04-05 24/week @ 2023-04-12 14/week @ 2023-04-19 21/week @ 2023-04-26 25/week @ 2023-05-03 27/week @ 2023-05-10 11/week @ 2023-05-17

85 downloads per month
Used in vtubestudio-cli

MIT license

185KB
3.5K SLoC

vtubestudio-rs

crates.io docs.rs MIT licensed

A library for interacting with the VTube Studio API.

Basic usage

The example below creates a Client using the provided builder, which:

  • connects to ws://localhost:8001 using tokio_tungstenite
  • authenticates with an existing token (if present and valid)
  • reconnects when disconnected, and retries the failed request on reconnection success
  • requests a new auth token on receiving an auth error, and retries the initial failed request on authentication success
use vtubestudio::data::StatisticsRequest;
use vtubestudio::{Client, ClientEvent, Error};

#[tokio::main]
async fn main() -> Result<(), Error> {
    // An auth token from a previous successful authentication request
    let stored_token = Some("...".to_string());

    let (mut client, mut events) = Client::builder()
        .auth_token(stored_token)
        .authentication("Plugin name", "Developer name", None)
        .build_tungstenite();

    tokio::spawn(async move {
        while let Some(event) = events.next().await {
            match event {
                ClientEvent::NewAuthToken(new_token) => {
                    // This returns whenever the authentication middleware receives a new auth
                    // token. We can handle it by saving it somewhere, etc.
                    println!("Got new auth token: {new_token}");
                }
                _ => {
                    // Other events, such as connections/disconnections, API events, etc
                    println!("Got event: {:?}", event);
                }
            }
        }
    });

    // Use the client to send a `StatisticsRequest`, handling authentication if necessary.
    // The return type is inferred from the input type to be `StatisticsResponse`.
    let resp = client.send(&StatisticsRequest {}).await?;
    println!("VTube Studio has been running for {}ms", resp.uptime);

    Ok(())
}

For more details, please check the documentation on docs.rs.

Dependencies

~8–14MB
~258K SLoC