Skip to content

Commit

Permalink
curseforge works now (also fix a caching bug)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAlan404 committed Jul 13, 2024
1 parent aa10794 commit 5229fed
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 11 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ roxmltree = "0.19"
semver = "1.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde_repr = "0.1"
sha1 = "0.10"
sha2 = "0.10"
tempfile = "3.9"
Expand Down
13 changes: 9 additions & 4 deletions src/api/app/http.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use anyhow::Result;
use anyhow::{Context, Result};
use reqwest::{IntoUrl, Response};
use serde::de::DeserializeOwned;
use tokio::time::sleep;
Expand All @@ -13,6 +13,8 @@ impl App {
url: impl IntoUrl,
f: F,
) -> Result<Response> {
println!("HTTP GET {}", url.as_str());

let req = self.http_client.get(url.as_str());

let req = f(req);
Expand Down Expand Up @@ -50,12 +52,15 @@ impl App {
}

pub async fn http_get_json<T: DeserializeOwned>(&self, url: impl IntoUrl) -> Result<T> {
let res = self.http_get(url).await?;
Ok(res.json().await?)
self.http_get_json_with(url, |x| x).await
}

pub async fn http_get_json_with<T: DeserializeOwned, F: FnOnce(reqwest::RequestBuilder) -> reqwest::RequestBuilder>(&self, url: impl IntoUrl, f: F) -> Result<T> {
let res = self.http_get_with(url, f).await?;
Ok(res.json().await?)

let full = res.bytes().await?;

Ok(serde_json::from_slice(&full)
.with_context(|| format!("JSON parsing error: {}", String::from_utf8_lossy(&full)))?)

Check failure on line 64 in src/api/app/http.rs

View workflow job for this annotation

GitHub Actions / clippy

question mark operator is useless here

error: question mark operator is useless here --> src/api/app/http.rs:63:9 | 63 | / Ok(serde_json::from_slice(&full) 64 | | .with_context(|| format!("JSON parsing error: {}", String::from_utf8_lossy(&full)))?) | |_________________________________________________________________________________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_question_mark = note: `-D clippy::needless-question-mark` implied by `-D clippy::all` = help: to override `-D clippy::all` add `#[allow(clippy::needless_question_mark)]` help: try removing question mark and `Ok()` | 63 ~ serde_json::from_slice(&full) 64 + .with_context(|| format!("JSON parsing error: {}", String::from_utf8_lossy(&full))) |
}
}
10 changes: 7 additions & 3 deletions src/api/sources/curseforge/models.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Data<T> {
Expand Down Expand Up @@ -54,7 +55,8 @@ pub struct CurseforgeDependency {
pub relation_type: CurseforgeDependencyType,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize_repr, Deserialize_repr, Clone)]
#[repr(u8)]
pub enum CurseforgeDependencyType {
EmbeddedLibrary = 1,
OptionalDependency = 2,
Expand All @@ -70,13 +72,15 @@ pub struct CurseforgeFileHash {
pub algo: CurseforgeHashAlgo,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize_repr, Deserialize_repr, Clone)]
#[repr(u8)]
pub enum CurseforgeHashAlgo {
Sha1 = 1,
Md5 = 2,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize_repr, Deserialize_repr, Clone)]
#[repr(u8)]
pub enum FileReleaseType {
Release = 1,
Beta = 2,
Expand Down
7 changes: 3 additions & 4 deletions src/api/sources/modrinth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,9 @@ impl<'a> ModrinthAPI<'a> {

let res: ModrinthProjectCheckResponse = self.fetch_api(format!("project/{slug}/check")).await?;

if let Some(mut ids) = store {
ids.insert(slug.to_owned(), res.id.clone());
self.0.cache.try_write_json(path, &ids)?;
}
let mut ids = store.unwrap_or_default();
ids.insert(slug.to_owned(), res.id.clone());
self.0.cache.try_write_json(path, &ids)?;

Ok(res.id)
}
Expand Down

0 comments on commit 5229fed

Please sign in to comment.