summaryrefslogtreecommitdiff
path: root/src/apis
diff options
context:
space:
mode:
authorfxqnlr <[email protected]>2024-09-04 17:32:19 +0200
committerfxqnlr <[email protected]>2024-09-04 17:32:19 +0200
commitecc4743fdec43eb578e9c35bb008c68909f1517e (patch)
tree73916114bc2bff8c72f759f5aae11a95d4dede22 /src/apis
parent11e64fc7560de3cd0def718edf68c31e3dc8be72 (diff)
downloadmodlist-ecc4743fdec43eb578e9c35bb008c68909f1517e.tar
modlist-ecc4743fdec43eb578e9c35bb008c68909f1517e.tar.gz
modlist-ecc4743fdec43eb578e9c35bb008c68909f1517e.zip
better error handlingrefactor
Diffstat (limited to 'src/apis')
-rw-r--r--src/apis/modrinth.rs51
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;
3use serde::{Deserialize, Serialize}; 3use serde::{Deserialize, Serialize};
4 4
5use crate::{ 5use 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
134async fn get( 134async 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
158pub async fn project(api: &str, name: &str) -> MLE<Project> { 153pub 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
212pub fn extract_current_version(versions: Vec<Version>) -> MLE<String> { 201pub 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
229pub async fn get_game_versions() -> MLE<Vec<GameVersion>> { 218pub 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}