Skip to content

Commit

Permalink
fix modrinth ratelimit #36
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAlan404 committed Dec 16, 2023
1 parent 264d965 commit 39b2700
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/commands/init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::app::BaseApp;
use crate::interop::mrpack::MRPackReader;
use crate::interop::packwiz::FileProvider;
use crate::model::Server;
use crate::model::{Network, PresetFlags, ServerEntry, ServerType, SoftwareType};
use crate::model::{Network, ServerEntry, ServerType, SoftwareType};
use crate::util::env::{get_docker_version, write_dockerfile, write_dockerignore, write_git};
use crate::util::SelectItem;
use anyhow::{bail, Context, Result};
Expand Down
33 changes: 30 additions & 3 deletions src/sources/modrinth.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use std::collections::HashMap;
use std::{collections::HashMap, time::Duration};

use anyhow::{anyhow, Result};
use async_trait::async_trait;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use tokio::time::sleep;

use crate::{
app::{App, CacheStrategy, ResolvedFile},
model::SoftwareType,
};

use super::github::GithubWaitRatelimit;

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct ModrinthProject {
pub slug: String,
Expand Down Expand Up @@ -103,6 +103,33 @@ pub struct ModrinthFile {
// file_type omitted
}


#[async_trait]
pub trait ModrinthWaitRatelimit<T> {
async fn wait_ratelimit(self) -> Result<T>;
}

#[async_trait]
impl ModrinthWaitRatelimit<reqwest::Response> for reqwest::Response {
async fn wait_ratelimit(self) -> Result<Self> {
let res = if let Some(h) = self.headers().get("x-ratelimit-remaining") {
if String::from_utf8_lossy(h.as_bytes()) == "1" {
let ratelimit_reset =
String::from_utf8_lossy(self.headers()["x-ratelimit-reset"].as_bytes())
.parse::<u64>()?;
let amount = ratelimit_reset;
println!(" (!) Ratelimit exceeded. sleeping for {amount} seconds...");
sleep(Duration::from_secs(amount)).await;
}
self
} else {
self.error_for_status()?
};

Ok(res)
}
}

pub struct ModrinthAPI<'a>(pub &'a App);

static API_URL: &str = "https://api.modrinth.com/v2";
Expand Down

0 comments on commit 39b2700

Please sign in to comment.