From 575d2493e8e5747bf65321f7277e52211d73e387 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Tue, 29 Nov 2022 22:59:19 +0100 Subject: fixed mod without matching specific version --- src/apis/modrinth.rs | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) (limited to 'src/apis/modrinth.rs') diff --git a/src/apis/modrinth.rs b/src/apis/modrinth.rs index 7b322cb..fb1e666 100644 --- a/src/apis/modrinth.rs +++ b/src/apis/modrinth.rs @@ -111,18 +111,25 @@ pub struct Hash { pub sha1: String, } -async fn get(api: String, path: String) -> Result, Box> { +async fn get(api: String, path: String) -> Result>, Box> { let url = format!(r#"{}{}"#, api, path); let client = Client::builder() .user_agent(format!("fxqnlr/modlistcli/{} (fxqnlr@gmail.com)", env!("CARGO_PKG_VERSION"))) .build()?; - let data = client.get(url) + let res = client.get(url) .send() - .await? - .bytes() - .await? - .to_vec(); + .await?; + + let mut data: Option> = None; + + if res.status() == 200 { + data = Some(res + .bytes() + .await? + .to_vec() + ); + } Ok(data) } @@ -130,9 +137,9 @@ async fn get(api: String, path: String) -> Result, Box Project { println!("!!!PROJECT"); let url = format!("project/{}", name); - let data = get(api, url); + let data = get(api, url).await.unwrap().unwrap(); - serde_json::from_slice(&data.await.unwrap()).unwrap() + serde_json::from_slice(&data).unwrap() } pub async fn projects(api: String, ids: Vec) -> Vec { @@ -142,9 +149,9 @@ pub async fn projects(api: String, ids: Vec) -> Vec { let url = format!(r#"projects?ids=["{}"]"#, all); //println!("{}", url); - let data = get(api, url); + let data = get(api, url).await.unwrap().unwrap(); - serde_json::from_slice(&data.await.unwrap()).unwrap() + serde_json::from_slice(&data).unwrap() } pub async fn versions(api: String, id: String, list: List) -> Vec { @@ -156,9 +163,14 @@ pub async fn versions(api: String, id: String, list: List) -> Vec { let url = format!(r#"project/{}/version?loaders=["{}"]&game_versions=["{}"]"#, id, loaderstr, list.mc_version); - let data = get(api, url); + let data = get(api, url).await.unwrap(); - serde_json::from_slice(&data.await.unwrap()).unwrap() + dbg!(&data); + + match data { + Some(data) => serde_json::from_slice(&data).unwrap(), + None => Vec::new(), + } } pub async fn get_raw_versions(api: String, versions: Vec) -> Vec { @@ -167,9 +179,9 @@ pub async fn get_raw_versions(api: String, versions: Vec) -> Vec) -> Result> { -- cgit v1.2.3