Skip to content

Commit

Permalink
vanilla source impl and more
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAlan404 committed Jun 3, 2024
1 parent dd06f9c commit 4149de7
Show file tree
Hide file tree
Showing 19 changed files with 901 additions and 178 deletions.
164 changes: 9 additions & 155 deletions Cargo.lock

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

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,9 @@ glob = "0.3"
hex = "0.4"
indexmap = "2.1"
indicatif = "0.17"
mcapi = { git = "https://github.com/ParadigmMC/mcapi.git" }
md-5 = "0.10"
notify-debouncer-full = { version = "0.3" }
opener = "0.6"
pathdiff = { git = "https://github.com/Manishearth/pathdiff.git" }
rpackwiz = { git = "https://github.com/vgskye/rpackwiz.git" }
regex = "1.10"
reqwest = { version = "0.11", features = ["json", "stream", "rustls-tls"], default-features = false }
roxmltree = "0.19"
Expand All @@ -62,3 +59,5 @@ toml = "0.8"
walkdir = "2.4"
zip = "0.6"
cliclack = "0.2.5"
os_info = { version = "3.7", default-features = false }
lazy_static = "1.4"
39 changes: 29 additions & 10 deletions src/api/app/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::sync::Arc;
use std::{sync::Arc, time::{Duration, SystemTime, UNIX_EPOCH}};

use anyhow::{Context, Result};
use reqwest::Url;
use reqwest::{IntoUrl, Url};

Check warning on line 4 in src/api/app/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `Url`

warning: unused import: `Url` --> src/api/app/mod.rs:4:24 | 4 | use reqwest::{IntoUrl, Url}; | ^^^
use serde::de::DeserializeOwned;
use tokio::sync::RwLock;
use tokio::{sync::RwLock, time::sleep};

use super::models::{network::Network, Addon, server::Server};

Expand Down Expand Up @@ -39,15 +39,34 @@ impl App {
})
}

pub async fn http_get_json<T: DeserializeOwned>(&self, url: impl Into<Url>) -> Result<T> {
Ok(self.http_client
.get(url.into())
pub async fn http_get_json<T: DeserializeOwned>(&self, url: impl IntoUrl) -> Result<T> {

Check warning on line 42 in src/api/app/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

methods `http_get_json` and `collect_addons` are never used

warning: methods `http_get_json` and `collect_addons` are never used --> src/api/app/mod.rs:42:18 | 27 | impl App { | -------- methods in this implementation ... 42 | pub async fn http_get_json<T: DeserializeOwned>(&self, url: impl IntoUrl) -> Result<T> { | ^^^^^^^^^^^^^ ... 72 | pub async fn collect_addons(&self) -> Result<Vec<Addon>> { | ^^^^^^^^^^^^^^
let res = self.http_client
.get(url.as_str())
.send()
.await?
.error_for_status()?
.json()
.await?
)
.error_for_status()?;

if res.headers().get("x-ratelimit-remaining").map(|x| String::from_utf8_lossy(x.as_bytes())) == Some("1".into()) {
let ratelimit_reset = String::from_utf8_lossy(res.headers()["x-ratelimit-reset"].as_bytes())
.parse::<u64>()?;
let sleep_amount = match url.into_url()?.domain() {
Some("github.com") => {
let now = SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs();
let amount = ratelimit_reset - now;
Some(amount)
},
Some("modrinth.com") => {
Some(ratelimit_reset)
},
_ => None,
};

if let Some(amount) = sleep_amount {
sleep(Duration::from_secs(amount)).await;
}
}

Ok(res.json().await?)
}

pub async fn collect_addons(&self) -> Result<Vec<Addon>> {
Expand Down
Loading

0 comments on commit 4149de7

Please sign in to comment.