summaryrefslogtreecommitdiff
path: root/src/apis/modrinth.rs
diff options
context:
space:
mode:
authorfx <[email protected]>2023-04-23 14:13:03 +0200
committerfx <[email protected]>2023-04-23 14:13:03 +0200
commit4300ad2eb05dddfa4274e04b204f2ad28c87da05 (patch)
treea2fd059e3aefff812d0d25e23fc85203dc9d122a /src/apis/modrinth.rs
parent2711f05669e353fbf452156d54855e9ba454f4a8 (diff)
parent64958cc9ff0858dbf068625e35b8d5dae249d4a4 (diff)
downloadmodlist-4300ad2eb05dddfa4274e04b204f2ad28c87da05.tar
modlist-4300ad2eb05dddfa4274e04b204f2ad28c87da05.tar.gz
modlist-4300ad2eb05dddfa4274e04b204f2ad28c87da05.zip
Merge pull request 'clap' (#1) from clap into master
Reviewed-on: http://raspberrypi.fritz.box:7920/fx/modlist/pulls/1
Diffstat (limited to 'src/apis/modrinth.rs')
-rw-r--r--src/apis/modrinth.rs56
1 files changed, 32 insertions, 24 deletions
diff --git a/src/apis/modrinth.rs b/src/apis/modrinth.rs
index bb5ee19..9afe7f3 100644
--- a/src/apis/modrinth.rs
+++ b/src/apis/modrinth.rs
@@ -2,7 +2,10 @@ use chrono::{DateTime, FixedOffset};
2use reqwest::Client; 2use reqwest::Client;
3use serde::Deserialize; 3use serde::Deserialize;
4 4
5use crate::{Modloader, List, error::{MLE, MLError, ErrorType}}; 5use crate::{
6 error::{ErrorType, MLError, MLE},
7 List, Modloader,
8};
6 9
7#[derive(Debug, Deserialize, Clone)] 10#[derive(Debug, Deserialize, Clone)]
8pub struct Project { 11pub struct Project {
@@ -47,7 +50,7 @@ pub struct ModeratorMessage {
47pub enum Side { 50pub enum Side {
48 required, 51 required,
49 optional, 52 optional,
50 unsupported 53 unsupported,
51} 54}
52 55
53#[allow(non_camel_case_types)] 56#[allow(non_camel_case_types)]
@@ -55,7 +58,7 @@ pub enum Side {
55pub enum Type { 58pub enum Type {
56 r#mod, 59 r#mod,
57 modpack, 60 modpack,
58 recourcepack 61 recourcepack,
59} 62}
60 63
61#[allow(non_camel_case_types)] 64#[allow(non_camel_case_types)]
@@ -63,9 +66,11 @@ pub enum Type {
63pub enum Status { 66pub enum Status {
64 approved, 67 approved,
65 rejected, 68 rejected,
66 draft, unlisted, archived, 69 draft,
70 unlisted,
71 archived,
67 processing, 72 processing,
68 unknown 73 unknown,
69} 74}
70 75
71#[derive(Debug, Clone, Deserialize)] 76#[derive(Debug, Clone, Deserialize)]
@@ -90,7 +95,7 @@ pub struct Version {
90pub enum VersionType { 95pub enum VersionType {
91 release, 96 release,
92 beta, 97 beta,
93 alpha 98 alpha,
94} 99}
95 100
96#[derive(Debug, Clone, Deserialize)] 101#[derive(Debug, Clone, Deserialize)]
@@ -110,22 +115,19 @@ pub struct Hash {
110 115
111async fn get(api: &str, path: String) -> Result<Option<Vec<u8>>, Box<dyn std::error::Error>> { 116async fn get(api: &str, path: String) -> Result<Option<Vec<u8>>, Box<dyn std::error::Error>> {
112 let url = format!(r#"{}{}"#, api, path); 117 let url = format!(r#"{}{}"#, api, path);
113 118
114 let client = Client::builder() 119 let client = Client::builder()
115 .user_agent(format!("fxqnlr/modlistcli/{} ([email protected])", env!("CARGO_PKG_VERSION"))) 120 .user_agent(format!(
121 "fxqnlr/modlistcli/{} ([email protected])",
122 env!("CARGO_PKG_VERSION")
123 ))
116 .build()?; 124 .build()?;
117 let res = client.get(url) 125 let res = client.get(url).send().await?;
118 .send() 126
119 .await?;
120
121 let mut data: Option<Vec<u8>> = None; 127 let mut data: Option<Vec<u8>> = None;
122 128
123 if res.status() == 200 { 129 if res.status() == 200 {
124 data = Some(res 130 data = Some(res.bytes().await?.to_vec());
125 .bytes()
126 .await?
127 .to_vec()
128 );
129 } 131 }
130 132
131 Ok(data) 133 Ok(data)
@@ -143,7 +145,7 @@ pub async fn projects(api: &str, ids: Vec<String>) -> Vec<Project> {
143 let url = format!(r#"projects?ids=["{}"]"#, all); 145 let url = format!(r#"projects?ids=["{}"]"#, all);
144 146
145 let data = get(api, url).await.unwrap().unwrap(); 147 let data = get(api, url).await.unwrap().unwrap();
146 148
147 serde_json::from_slice(&data).unwrap() 149 serde_json::from_slice(&data).unwrap()
148} 150}
149 151
@@ -154,7 +156,10 @@ pub async fn versions(api: &str, id: String, list: List) -> Vec<Version> {
154 Modloader::Fabric => String::from("fabric"), 156 Modloader::Fabric => String::from("fabric"),
155 }; 157 };
156 158
157 let url = format!(r#"project/{}/version?loaders=["{}"]&game_versions=["{}"]"#, id, loaderstr, list.mc_version); 159 let url = format!(
160 r#"project/{}/version?loaders=["{}"]&game_versions=["{}"]"#,
161 id, loaderstr, list.mc_version
162 );
158 163
159 let data = get(api, url).await.unwrap(); 164 let data = get(api, url).await.unwrap();
160 165
@@ -185,7 +190,7 @@ pub fn extract_current_version(versions: Vec<Version>) -> MLE<String> {
185 times.sort_by_key(|t| t.1); 190 times.sort_by_key(|t| t.1);
186 times.reverse(); 191 times.reverse();
187 Ok(times[0].0.to_string()) 192 Ok(times[0].0.to_string())
188 }, 193 }
189 _ => panic!("available_versions should never be negative"), 194 _ => panic!("available_versions should never be negative"),
190 } 195 }
191} 196}
@@ -205,16 +210,19 @@ pub struct MCVersion {
205} 210}
206 211
207pub async fn get_minecraft_version(api: &str, version: MCVersionType) -> String { 212pub async fn get_minecraft_version(api: &str, version: MCVersionType) -> String {
208 let data = get(api, String::from("tag/game_version")).await.unwrap().unwrap(); 213 let data = get(api, String::from("tag/game_version"))
214 .await
215 .unwrap()
216 .unwrap();
209 let mc_versions: Vec<MCVersion> = serde_json::from_slice(&data).unwrap(); 217 let mc_versions: Vec<MCVersion> = serde_json::from_slice(&data).unwrap();
210 let ver = match version { 218 let ver = match version {
211 MCVersionType::Release => { 219 MCVersionType::Release => {
212 let mut i = 0; 220 let mut i = 0;
213 while !mc_versions[i].major { 221 while !mc_versions[i].major {
214 i += 1; 222 i += 1;
215 }; 223 }
216 &mc_versions[i] 224 &mc_versions[i]
217 }, 225 }
218 MCVersionType::Latest => &mc_versions[0], 226 MCVersionType::Latest => &mc_versions[0],
219 MCVersionType::Specific => { 227 MCVersionType::Specific => {
220 println!("Not inplemented"); 228 println!("Not inplemented");