diff options
Diffstat (limited to 'src/apis/modrinth.rs')
-rw-r--r-- | src/apis/modrinth.rs | 51 |
1 files changed, 19 insertions, 32 deletions
diff --git a/src/apis/modrinth.rs b/src/apis/modrinth.rs index 7f1fb52..afd7b69 100644 --- a/src/apis/modrinth.rs +++ b/src/apis/modrinth.rs | |||
@@ -3,7 +3,7 @@ use reqwest::Client; | |||
3 | use serde::{Deserialize, Serialize}; | 3 | use serde::{Deserialize, Serialize}; |
4 | 4 | ||
5 | use crate::{ | 5 | use crate::{ |
6 | error::{EType, MLErr, MLE}, | 6 | errors::{Error, MLE}, |
7 | List, | 7 | List, |
8 | }; | 8 | }; |
9 | 9 | ||
@@ -131,10 +131,7 @@ pub enum GameVersionType { | |||
131 | } | 131 | } |
132 | 132 | ||
133 | /// # Errors | 133 | /// # Errors |
134 | async fn get( | 134 | async fn get(api: &str, path: &str) -> Result<Vec<u8>, Error> { |
135 | api: &str, | ||
136 | path: &str, | ||
137 | ) -> Result<Option<Vec<u8>>, Box<dyn std::error::Error>> { | ||
138 | let url = format!(r#"{api}{path}"#); | 135 | let url = format!(r#"{api}{path}"#); |
139 | 136 | ||
140 | let client = Client::builder() | 137 | let client = Client::builder() |
@@ -145,23 +142,19 @@ async fn get( | |||
145 | .build()?; | 142 | .build()?; |
146 | let res = client.get(url).send().await?; | 143 | let res = client.get(url).send().await?; |
147 | 144 | ||
148 | let mut data: Option<Vec<u8>> = None; | 145 | if res.status() != 200 { |
149 | 146 | return Err(Error::RequestNotOK(res.status())); | |
150 | if res.status() == 200 { | ||
151 | data = Some(res.bytes().await?.to_vec()); | ||
152 | } | 147 | } |
153 | 148 | ||
154 | Ok(data) | 149 | Ok(res.bytes().await?.to_vec()) |
155 | } | 150 | } |
156 | 151 | ||
157 | /// # Errors | 152 | /// # Errors |
158 | pub async fn project(api: &str, name: &str) -> MLE<Project> { | 153 | pub async fn project(api: &str, name: &str) -> MLE<Project> { |
159 | let url = format!("project/{name}"); | 154 | let url = format!("project/{name}"); |
160 | let data = get(api, &url).await | 155 | let data = get(api, &url).await?; |
161 | .map_err(|_| MLErr::new(EType::Other, "geterr"))? | ||
162 | .ok_or(MLErr::new(EType::Other, "geterr2"))?; | ||
163 | 156 | ||
164 | serde_json::from_slice(&data).map_err(|_| MLErr::new(EType::LibJson, "from project")) | 157 | Ok(serde_json::from_slice(&data)?) |
165 | } | 158 | } |
166 | 159 | ||
167 | /// # Errors | 160 | /// # Errors |
@@ -169,11 +162,9 @@ pub async fn projects(api: &str, ids: Vec<String>) -> MLE<Vec<Project>> { | |||
169 | let all = ids.join(r#"",""#); | 162 | let all = ids.join(r#"",""#); |
170 | let url = format!(r#"projects?ids=["{all}"]"#); | 163 | let url = format!(r#"projects?ids=["{all}"]"#); |
171 | 164 | ||
172 | let data = get(api, &url).await | 165 | let data = get(api, &url).await?; |
173 | .map_err(|_| MLErr::new(EType::Other, "geterr"))? | ||
174 | .ok_or(MLErr::new(EType::Other, "geterr2"))?; | ||
175 | 166 | ||
176 | serde_json::from_slice(&data).map_err(|_| MLErr::new(EType::LibJson, "from projects")) | 167 | Ok(serde_json::from_slice(&data)?) |
177 | } | 168 | } |
178 | 169 | ||
179 | ///Get applicable versions from `mod_id` with list context | 170 | ///Get applicable versions from `mod_id` with list context |
@@ -184,12 +175,12 @@ pub async fn versions(api: &str, id: String, list: List) -> MLE<Vec<Version>> { | |||
184 | id, list.modloader, list.mc_version | 175 | id, list.modloader, list.mc_version |
185 | ); | 176 | ); |
186 | 177 | ||
187 | let data = get(api, &url).await | 178 | let data = get(api, &url).await?; |
188 | .map_err(|_| MLErr::new(EType::Other, "geterr"))?; | ||
189 | 179 | ||
190 | Ok(match data { | 180 | Ok(if data.is_empty() { |
191 | Some(data) => serde_json::from_slice(&data).map_err(|_| MLErr::new(EType::LibJson, "from version"))?, | 181 | Vec::new() |
192 | None => Vec::new(), | 182 | } else { |
183 | serde_json::from_slice(&data)? | ||
193 | }) | 184 | }) |
194 | } | 185 | } |
195 | 186 | ||
@@ -201,17 +192,15 @@ pub async fn get_raw_versions( | |||
201 | ) -> MLE<Vec<Version>> { | 192 | ) -> MLE<Vec<Version>> { |
202 | let url = format!(r#"versions?ids=["{}"]"#, versions.join(r#"",""#)); | 193 | let url = format!(r#"versions?ids=["{}"]"#, versions.join(r#"",""#)); |
203 | 194 | ||
204 | let data = get(api, &url).await | 195 | let data = get(api, &url).await?; |
205 | .map_err(|_| MLErr::new(EType::Other, "geterr"))? | ||
206 | .ok_or(MLErr::new(EType::Other, "geterr2"))?; | ||
207 | 196 | ||
208 | serde_json::from_slice(&data).map_err(|_| MLErr::new(EType::LibJson, "from raw version")) | 197 | Ok(serde_json::from_slice(&data)?) |
209 | } | 198 | } |
210 | 199 | ||
211 | /// # Errors | 200 | /// # Errors |
212 | pub fn extract_current_version(versions: Vec<Version>) -> MLE<String> { | 201 | pub fn extract_current_version(versions: Vec<Version>) -> MLE<String> { |
213 | match versions.len() { | 202 | match versions.len() { |
214 | 0 => Err(MLErr::new(EType::ModError, "NO_VERSIONS_AVAILABLE")), | 203 | 0 => Err(Error::ModError("NO_VERSIONS_AVAILABLE".to_string())), |
215 | 1.. => { | 204 | 1.. => { |
216 | let mut times: Vec<(String, DateTime<FixedOffset>)> = vec![]; | 205 | let mut times: Vec<(String, DateTime<FixedOffset>)> = vec![]; |
217 | for ver in versions { | 206 | for ver in versions { |
@@ -228,9 +217,7 @@ pub fn extract_current_version(versions: Vec<Version>) -> MLE<String> { | |||
228 | /// # Errors | 217 | /// # Errors |
229 | pub async fn get_game_versions() -> MLE<Vec<GameVersion>> { | 218 | pub async fn get_game_versions() -> MLE<Vec<GameVersion>> { |
230 | let data = get("https://api.modrinth.com/v2/", "tag/game_version") | 219 | let data = get("https://api.modrinth.com/v2/", "tag/game_version") |
231 | .await | 220 | .await?; |
232 | .map_err(|_| MLErr::new(EType::Other, "geterr"))? | ||
233 | .ok_or(MLErr::new(EType::Other, "geterr2"))?; | ||
234 | 221 | ||
235 | serde_json::from_slice(&data).map_err(|_| MLErr::new(EType::LibJson, "from game version")) | 222 | Ok(serde_json::from_slice(&data)?) |
236 | } | 223 | } |