mangadex-api 0.2.3

Unofficial client/wrapper for the MangaDex API
Documentation

MangaDex API

Unofficial Rust client/wrapper for the MangaDex API. This also includes some conveniences that are not provided from the API directly such as logging in.

Warning: This is still in the early development stage. Some breaking API changes may occur as the project matures. Consequently, this code is not yet ready for use in production.

Table of Contents

Requirements

Back to top

Features

Back to top

Optional Features

Back to top

  • time - Use Chrono for the timestamps instead of integers and arbitrary strings. Included by default.

Examples

Back to top

Fetch manga by ID

use mangadex_api::v2::{responses::ResponseType, MangaDexV2};

#[tokio::main]
async fn main() -> Result<(), reqwest::Error> {
    let mangadex_client = MangaDexV2::default();

    let manga  = mangadex_client
        .manga(1)
        .send()
        .await?
        .ok()
        .unwrap();

    // Fields are hidden behind getter methods to prevent field mutation.
    // The getters return a referenced value so comparisons require de-referencing.
    assert_eq!(*manga.data().id(), 1);

    Ok(())
}

Logging in

Some features on MangaDex are restricted to registered users such as searching by manga title.

In order to use these features, you must log in first and the Reqwest library will store the session cookie in the client. Once that's done, you can use call the other endpoints normally and get the data you need.

use std::io;

use mangadex_api::v2::MangaDexV2;
use mangadex_api::web_scrape::responses::LoginAjaxResponseType;

#[tokio::main]
async fn main() -> Result<(), reqwest::Error> {
    let mangadex_client = MangaDexV2::default();

    let mut username = String::new();
    println!("Enter your MangaDex username:");
    io::stdin()
        .read_line(&mut username)
        .expect("Failed to read username");

    let mut password = String::new();
    println!("Enter your MangaDex password:");
    io::stdin()
        .read_line(&mut password)
        .expect("Failed to read password");

    // If you know that you need a 2-factor authentication code, you can add it directly before
    // sending the first login attempt and not check the `TwoFactorAuthenticationRequired` variant.
    match mangadex_client
        .login_ajax(&username, &password)
        .send()
        .await?
    {
        LoginAjaxResponseType::Ok => {
            println!("Login successful!")
        }
        LoginAjaxResponseType::TwoFactorAuthenticationRequired => {
            let mut two_factor = String::new();
            loop {
                println!("Enter your 2FA code:");
                io::stdin()
                    .read_line(&mut two_factor)
                    .expect("Failed to read 2FA code");

                match mangadex_client
                    .login_ajax(&username, &password)
                    .two_factor(&two_factor)
                    .send()
                    .await?
                {
                    LoginAjaxResponseType::Ok => {
                        println!("Login successful!");
                        break;
                    }
                    LoginAjaxResponseType::IncorrectTwoFactorCodeLength
                    | LoginAjaxResponseType::FailedTwoFactorVerification => {
                        println!("Incorrect 2FA code")
                    }
                    _ => panic!("There was an error submitting the 2FA code"),
                }
            }
        }
        _ => panic!("There was an error logging in."),
    }

    Ok(())
}

Running Examples

Back to top

The examples can be run with the following:

cargo run --example [example_name]

More details about the examples can be found in the examples README file.

Changelog

Back to top

The changelog can be found here.

Changes are added manually to keep the changelog human-readable with summaries of the changes from each version.

License

Back to top

Licensed under either of

at your option.

Contribution

Back to top

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Contributing

Back to top

We welcome contributions from everyone. There are many ways to contribute and the CONTRIBUTING.md document explains how you can contribute and get started.