From fc1cb1acc0dce412e948475002666bcd1d4b0348 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Mon, 31 Oct 2022 22:41:18 +0100 Subject: add first impl --- src/apis/modrinth.rs | 101 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 76 insertions(+), 25 deletions(-) (limited to 'src/apis/modrinth.rs') diff --git a/src/apis/modrinth.rs b/src/apis/modrinth.rs index ce9fdd4..3af5bbd 100644 --- a/src/apis/modrinth.rs +++ b/src/apis/modrinth.rs @@ -1,25 +1,6 @@ -use serde::{Deserialize, Serialize}; +use serde::Deserialize; -async fn get(path: String) -> Result, Box> { - 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) -} +use crate::Modloader; #[derive(Debug, Deserialize)] pub struct Project { @@ -86,19 +67,89 @@ pub enum Status { processing, unknown } -pub async fn project(name: &str) -> Project { + +#[derive(Debug, Deserialize)] +pub struct Version { + pub name: String, + pub version_number: String, + pub changelog: Option, + pub game_versions: Vec, + pub version_type: VersionType, + pub loaders: Vec, + pub featured: bool, + pub id: String, + pub project_id: String, + pub author_id: String, + pub date_published: String, + pub downloads: u32, + pub files: Vec, +} + +#[allow(non_camel_case_types)] +#[derive(Debug, Deserialize)] +pub enum VersionType { + release, + beta, + alpha +} + +#[derive(Debug, Deserialize)] +pub struct VersionFile { + pub hashes: Hash, + pub url: String, + pub filename: String, + pub primary: bool, + pub size: u32, +} + +#[derive(Debug, Deserialize)] +pub struct Hash { + pub sha512: String, + pub sha1: String, +} + +async fn get(api: String, path: String) -> Result, Box> { + let url = format!(r#"{}{}"#, api, path); + + dbg!(&url); + + let data = reqwest::get(url) + .await? + .bytes() + .await? + .to_vec(); + + Ok(data) +} + + +pub async fn project(api: String, name: &str) -> Project { let url = format!("project/{}", name); - let data = get(url); + let data = get(api, url); serde_json::from_slice(&data.await.unwrap()).unwrap() } -pub async fn projects(ids: Vec<&str>) -> Vec { +pub async fn projects(api: String, ids: Vec<&str>) -> Vec { let all = ids.join(r#"",""#); let url = format!(r#"projects?ids=["{}"]"#, all); println!("{}", url); - let data = get(url); + let data = get(api, url); serde_json::from_slice(&data.await.unwrap()).unwrap() } + +pub async fn versions(api: String, id: String, loader: Modloader, mc_version: String) -> Vec { + + let loaderstr = match loader { + Modloader::Forge => String::from("forge"), + Modloader::Fabric => String::from("fabric"), + }; + + let url = format!(r#"project/{}/version?loaders=["{}"]&game_versions=["{}"]"#, id, loaderstr, mc_version); + + let data = get(api, url); + + serde_json::from_slice(&data.await.unwrap()).unwrap() +} -- cgit v1.2.3