Skip to content

Commit

Permalink
Merge pull request #18 from bobertoyin/add-rate-limit-error
Browse files Browse the repository at this point in the history
add option for rate limit errors
  • Loading branch information
bobertoyin authored Oct 14, 2023
2 parents 6132dff + e25ff50 commit e14818e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "megamind"
version = "0.10.0"
version = "0.11.0"
edition = "2021"
description = "A library for interacting with the Genius API."
authors = ["Robert Yin <bobertoyin@gmail.com>"]
Expand Down
15 changes: 12 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use log::info;
use reqwest::{
header::{HeaderMap, HeaderValue, InvalidHeaderValue, AUTHORIZATION},
Client as ReqwestClient, Error as ReqwestError,
Client as ReqwestClient, Error as ReqwestError, StatusCode,
};
use serde::{de::DeserializeOwned, Serialize};
use thiserror::Error;
Expand All @@ -27,8 +27,14 @@ pub const BASE_URL: &str = "https://api.genius.com";

/// Client errors.
#[derive(Debug, Error)]
#[error("megamind client error: {0}")]
pub struct ClientError(#[from] pub ReqwestError);
pub enum ClientError{
/// A general client error.
#[error("megamind client error: {0}")]
General(#[from] ReqwestError),
/// A rate limit error.
#[error("megamind rate limit error")]
RateLimited,
}

/// An HTTP client for interacting with the Genius API.
///
Expand Down Expand Up @@ -78,6 +84,9 @@ impl Client {
.send()
.await?;
let resp_url = response.url().clone();
if response.status() == StatusCode::TOO_MANY_REQUESTS {
return Err(ClientError::RateLimited);
}
Ok(response
.json::<Response<T>>()
.await
Expand Down

0 comments on commit e14818e

Please sign in to comment.