Skip to content

Commit a7aada0

Browse files
committed
wip: feat: respect Scryfall's rate limits by default
1 parent 1e68fca commit a7aada0

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ categories = ["api-bindings", "games"]
1414
features = ["unknown_variants"]
1515

1616
[features]
17-
default = ["bulk_caching", "default-tls"]
17+
default = ["bulk_caching", "default-tls", "auto_rate_limit"]
1818
# default = []
1919
bulk_caching = ["dep:heck"]
2020
default-tls = ["reqwest/default-tls"]
2121
native-tls = ["reqwest/native-tls"]
2222
rustls-tls = ["reqwest/rustls-tls"]
2323
unknown_variants = []
2424
unknown_variants_slim = []
25+
auto_rate_limit = []
2526
bin = ["tokio/macros", "tokio/rt-multi-thread"]
2627

2728
[dependencies]

src/uri.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use std::convert::TryFrom;
77
use std::marker::PhantomData;
88

9+
use cfg_if::cfg_if;
910
use httpstatus::StatusCode;
1011
use reqwest::header::{self, HeaderValue};
1112
use serde::de::DeserializeOwned;
@@ -139,6 +140,19 @@ impl<T: DeserializeOwned> Uri<T> {
139140
}
140141

141142
pub(crate) async fn fetch_raw(&self) -> crate::Result<reqwest::Response> {
143+
// Respect Scryfall's rate limits
144+
cfg_if! {
145+
if #[cfg(feature = "auto_rate_limit")] {
146+
let limit = match self.url.path() {
147+
"/cards/search" | "/cards/named" | "cards/random" | "/cards/collection" => 500,
148+
"/cards/search/" | "/cards/named/" | "cards/random/" | "/cards/collection/" => 500,
149+
"/cards/manifest" => 6000,
150+
_ => 110,
151+
};
152+
tokio::time::sleep(tokio::time::Duration::from_millis(limit)).await;
153+
}
154+
}
155+
142156
match client().get(self.url.clone()).send().await {
143157
Ok(response) => match response.status().as_u16() {
144158
400..=599 => Err(Error::ScryfallError(response.json().await.map_err(

0 commit comments

Comments
 (0)