summaryrefslogblamecommitdiff
path: root/src/apis/modrinth.rs
blob: ce9fdd4b6efb5bd15af77faa8892bc029ee87890 (plain) (tree)







































































































                                                                                                  
use serde::{Deserialize, Serialize};

async fn get(path: String) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
    dbg!(&path);
    let api = String::from("https://api.modrinth.com/v2/");
    //let api = String::from("localhost:8080/");
    //let api = String::from("https://www.rust-lang.org/");
    let url = format!(r#"{}{}"#, api, path);
    
    println!("{}", &url);
    

    let data = reqwest::get(r#"https://api.modrinth.com/v2/projects?ids=["kYuIpRLv","89Wsn8GD"]"#)
    .await?
    .bytes()
    .await?
    .to_vec();

    //println!("body = {:?}", data);

    Ok(data)
}

#[derive(Debug, Deserialize)]
pub struct Project {
    pub slug: String,
    pub title: String,
    pub description: String,
    pub categories: Vec<String>,
    pub client_side: Side,
    pub server_side: Side,
    pub body: String,
    pub additional_categories: Option<Vec<String>>,
    pub project_type: Type,
    pub downloads: u32,
    pub icon_url: Option<String>,
    pub id: String,
    pub team: String,
    pub moderator_message: Option<ModeratorMessage>,
    pub published: String,
    pub updated: String,
    pub approved: Option<String>,
    pub followers: u32,
    pub status: Status,
    pub license: License,
    pub versions: Vec<String>,
}

#[derive(Debug, Deserialize)]
pub struct License {
    pub id: String,
    pub name: String,
    pub url: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct ModeratorMessage {
    pub message: String,
    pub body: Option<String>,
}

#[allow(non_camel_case_types)]
#[derive(Debug, Deserialize)]
pub enum Side {
    required,
    optional,
    unsupported
}

#[allow(non_camel_case_types)]
#[derive(Debug, Deserialize)]
pub enum Type {
    r#mod,
    modpack,
    recourcepack
}

#[allow(non_camel_case_types)]
#[derive(Debug, Deserialize)]
pub enum Status {
    approved,
    rejected,
    draft,
    unlisted,
    archived,
    processing,
    unknown
}
pub async fn project(name: &str) -> Project {
    let url = format!("project/{}", name);
    let data = get(url);

    serde_json::from_slice(&data.await.unwrap()).unwrap()
}

pub async fn projects(ids: Vec<&str>) -> Vec<Project> {
    let all = ids.join(r#"",""#);
    let url = format!(r#"projects?ids=["{}"]"#, all);
    println!("{}", url);

    let data = get(url);
    
    serde_json::from_slice(&data.await.unwrap()).unwrap()
}