diff options
author | fxqnlr <[email protected]> | 2023-05-02 20:37:23 +0200 |
---|---|---|
committer | fxqnlr <[email protected]> | 2023-05-02 20:37:23 +0200 |
commit | 202ed4fcaa69e7de23e83fab0bae4ced6209eee4 (patch) | |
tree | 94c907a04f357f71f35e0d4a27b7e272dc00c476 /src/apis | |
parent | e8cac6786bc2e91382316ef1023a494c3e812013 (diff) | |
download | modlist-202ed4fcaa69e7de23e83fab0bae4ced6209eee4.tar modlist-202ed4fcaa69e7de23e83fab0bae4ced6209eee4.tar.gz modlist-202ed4fcaa69e7de23e83fab0bae4ced6209eee4.zip |
added config option for default mc version
changed version input system
Diffstat (limited to 'src/apis')
-rw-r--r-- | src/apis/modrinth.rs | 66 |
1 files changed, 26 insertions, 40 deletions
diff --git a/src/apis/modrinth.rs b/src/apis/modrinth.rs index 9afe7f3..13e7a6d 100644 --- a/src/apis/modrinth.rs +++ b/src/apis/modrinth.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | use chrono::{DateTime, FixedOffset}; | 1 | use chrono::{DateTime, FixedOffset}; |
2 | use reqwest::Client; | 2 | use reqwest::Client; |
3 | use serde::Deserialize; | 3 | use serde::{Deserialize, Serialize}; |
4 | 4 | ||
5 | use crate::{ | 5 | use crate::{ |
6 | error::{ErrorType, MLError, MLE}, | 6 | error::{ErrorType, MLError, MLE}, |
@@ -113,7 +113,24 @@ pub struct Hash { | |||
113 | pub sha1: String, | 113 | pub sha1: String, |
114 | } | 114 | } |
115 | 115 | ||
116 | async fn get(api: &str, path: String) -> Result<Option<Vec<u8>>, Box<dyn std::error::Error>> { | 116 | #[derive(Debug, Clone, Serialize, Deserialize)] |
117 | pub struct GameVersion { | ||
118 | pub version: String, | ||
119 | pub version_type: GameVersionType, | ||
120 | pub date: String, | ||
121 | pub major: bool, | ||
122 | } | ||
123 | |||
124 | #[allow(non_camel_case_types)] | ||
125 | #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] | ||
126 | pub enum GameVersionType { | ||
127 | release, | ||
128 | snapshot, | ||
129 | alpha, | ||
130 | beta | ||
131 | } | ||
132 | |||
133 | async fn get(api: &str, path: &str) -> Result<Option<Vec<u8>>, Box<dyn std::error::Error>> { | ||
117 | let url = format!(r#"{}{}"#, api, path); | 134 | let url = format!(r#"{}{}"#, api, path); |
118 | 135 | ||
119 | let client = Client::builder() | 136 | let client = Client::builder() |
@@ -135,7 +152,7 @@ async fn get(api: &str, path: String) -> Result<Option<Vec<u8>>, Box<dyn std::er | |||
135 | 152 | ||
136 | pub async fn project(api: &str, name: &str) -> Project { | 153 | pub async fn project(api: &str, name: &str) -> Project { |
137 | let url = format!("project/{}", name); | 154 | let url = format!("project/{}", name); |
138 | let data = get(api, url).await.unwrap().unwrap(); | 155 | let data = get(api, &url).await.unwrap().unwrap(); |
139 | 156 | ||
140 | serde_json::from_slice(&data).unwrap() | 157 | serde_json::from_slice(&data).unwrap() |
141 | } | 158 | } |
@@ -144,7 +161,7 @@ pub async fn projects(api: &str, ids: Vec<String>) -> Vec<Project> { | |||
144 | let all = ids.join(r#"",""#); | 161 | let all = ids.join(r#"",""#); |
145 | let url = format!(r#"projects?ids=["{}"]"#, all); | 162 | let url = format!(r#"projects?ids=["{}"]"#, all); |
146 | 163 | ||
147 | let data = get(api, url).await.unwrap().unwrap(); | 164 | let data = get(api, &url).await.unwrap().unwrap(); |
148 | 165 | ||
149 | serde_json::from_slice(&data).unwrap() | 166 | serde_json::from_slice(&data).unwrap() |
150 | } | 167 | } |
@@ -161,7 +178,7 @@ pub async fn versions(api: &str, id: String, list: List) -> Vec<Version> { | |||
161 | id, loaderstr, list.mc_version | 178 | id, loaderstr, list.mc_version |
162 | ); | 179 | ); |
163 | 180 | ||
164 | let data = get(api, url).await.unwrap(); | 181 | let data = get(api, &url).await.unwrap(); |
165 | 182 | ||
166 | match data { | 183 | match data { |
167 | Some(data) => serde_json::from_slice(&data).unwrap(), | 184 | Some(data) => serde_json::from_slice(&data).unwrap(), |
@@ -173,7 +190,7 @@ pub async fn versions(api: &str, id: String, list: List) -> Vec<Version> { | |||
173 | pub async fn get_raw_versions(api: &str, versions: Vec<String>) -> Vec<Version> { | 190 | pub async fn get_raw_versions(api: &str, versions: Vec<String>) -> Vec<Version> { |
174 | let url = format!(r#"versions?ids=["{}"]"#, versions.join(r#"",""#)); | 191 | let url = format!(r#"versions?ids=["{}"]"#, versions.join(r#"",""#)); |
175 | 192 | ||
176 | let data = get(api, url).await.unwrap().unwrap(); | 193 | let data = get(api, &url).await.unwrap().unwrap(); |
177 | 194 | ||
178 | serde_json::from_slice(&data).unwrap() | 195 | serde_json::from_slice(&data).unwrap() |
179 | } | 196 | } |
@@ -195,39 +212,8 @@ pub fn extract_current_version(versions: Vec<Version>) -> MLE<String> { | |||
195 | } | 212 | } |
196 | } | 213 | } |
197 | 214 | ||
198 | pub enum MCVersionType { | 215 | pub async fn get_game_versions() -> Vec<GameVersion> { |
199 | Release, | 216 | let data = get("https://api.modrinth.com/v2/", "tag/game_version").await.unwrap().unwrap(); |
200 | Latest, | ||
201 | Specific, | ||
202 | } | ||
203 | 217 | ||
204 | #[derive(Debug, Deserialize)] | 218 | serde_json::from_slice(&data).unwrap() |
205 | pub struct MCVersion { | ||
206 | pub version: String, | ||
207 | pub version_type: String, | ||
208 | pub date: String, | ||
209 | pub major: bool, | ||
210 | } | ||
211 | |||
212 | pub async fn get_minecraft_version(api: &str, version: MCVersionType) -> String { | ||
213 | let data = get(api, String::from("tag/game_version")) | ||
214 | .await | ||
215 | .unwrap() | ||
216 | .unwrap(); | ||
217 | let mc_versions: Vec<MCVersion> = serde_json::from_slice(&data).unwrap(); | ||
218 | let ver = match version { | ||
219 | MCVersionType::Release => { | ||
220 | let mut i = 0; | ||
221 | while !mc_versions[i].major { | ||
222 | i += 1; | ||
223 | } | ||
224 | &mc_versions[i] | ||
225 | } | ||
226 | MCVersionType::Latest => &mc_versions[0], | ||
227 | MCVersionType::Specific => { | ||
228 | println!("Not inplemented"); | ||
229 | &mc_versions[0] | ||
230 | } | ||
231 | }; | ||
232 | String::from(&ver.version) | ||
233 | } | 219 | } |