6 releases (breaking)

0.5.0 Sep 8, 2024
0.4.0 Feb 11, 2024
0.3.0 Dec 6, 2023
0.2.0 Dec 2, 2023
0.1.1 Nov 24, 2023

#1078 in Web programming

Download history 173/week @ 2024-09-06 23/week @ 2024-09-13 6/week @ 2024-09-20 13/week @ 2024-09-27 4/week @ 2024-10-04

232 downloads per month

MIT license

130KB
2.5K SLoC

tetrio-api

codecov

A library to interact with the TETR.IO public API, with both a simple http client that doesn't manage cache and a "cached-http-client" that holds the data fetched from the API in memory. A library already exists to interact with the API in rust here,but I still needed an easier way to interact with the cache so I had to work on this library in the end.

The library tries to stay accurate to the API specs but I had to take some liberties because:

  • I needed to access replay data, which are not documented
  • Sometimes the documentation was partly innacurate which would cause errors when trying to use the library.

This library might not be perfect, but I did write tests, and since rust models are strict, the models have to comply with the current state of the API for the tests to pass, making it essentially accurate and usable.

THIS IS NOT AN OFFICIAL TETR.IO AFFILIATED LIBRARY.

Quick start

Install

You can add the package by cargo:

cargo add tetrio-api

Or add this to your Cargo.toml

[dependencies]
tetrio-api = { "git" = "https://github.com/Takathediscordbot/tetrio-api" }

This will automatically add the latest version of this api to your dependencies.

You can also use a specific commit like so:

[dependencies]
tetrio-api = { "git" = "https://github.com/Takathediscordbot/tetrio-api", rev="64a0516" }

How to use

Fetching a user

use std::f64::NAN;

use tetrio_api::{http::client, models::packet::Packet};

#[tokio::main]
async fn main() {
    let user = client::fetch_user_info("taka").await.expect("Couldn't fetch the CH.TETR.IO API to find the error! This could have been an error while parsing the data or while trying to send the HTTP request.");

    match user {
        Packet { data: Some(data), .. } => {

            println!("{} (id: {}): {}pps, {}apm {}vs", data.user.username, data.user.id, data.user.league.pps.unwrap_or(NAN), data.user.league.apm.unwrap_or(NAN), data.user.league.vs.unwrap_or(NAN));
        },
        Packet { success, error, .. } => {
            if success {
                eprintln!("The API didn't return an error but no data was found somehow!"); 
                // According to the API documentation that is not a possible case.
                unreachable!();
            }

            eprintln!("An error has occured while trying to fetch the user! {:?}", error)            
        }
    };
}

Examples

There are code examples in the examples folder of this git repository. The readme in there will indicate how to run examples.

Fetching a User

Running tests

To make sure the library functionalities are working before using it, you can run tests by running

cargo test

However, there is a good chance running the tests fail because of rate limit, you can limit the number of threads running at the same time by using

cargo test -- --test-threads=2

The tests will run significantly slower but should not fail due to rate limit. You might also be able to put a higher number, and if the tests still don't work because of rate limit you can limit the threads to 1 to remove parallelism completely.

Dependencies

~7–19MB
~255K SLoC