2022-04-03 19:01:58 +00:00
|
|
|
use std::io;
|
2022-01-26 18:55:08 +00:00
|
|
|
|
2022-01-25 18:47:08 +00:00
|
|
|
use reqwest::Method;
|
2022-04-03 19:01:58 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
use super::{client::api_url, send_api_request, ImgurClient};
|
2022-01-22 21:02:51 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
pub struct RateLimitInfo {
|
|
|
|
pub data: RateLimitData,
|
2022-01-23 12:05:32 +00:00
|
|
|
pub success: bool,
|
2022-01-24 18:33:44 +00:00
|
|
|
pub status: i32,
|
2022-01-22 21:02:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
pub struct RateLimitData {
|
|
|
|
#[serde(rename = "UserLimit")]
|
|
|
|
pub user_limit: i32,
|
|
|
|
#[serde(rename = "UserRemaining")]
|
|
|
|
pub user_remaining: i32,
|
|
|
|
#[serde(rename = "UserReset")]
|
|
|
|
pub user_reset: i32,
|
|
|
|
#[serde(rename = "ClientLimit")]
|
|
|
|
pub client_limit: i32,
|
|
|
|
#[serde(rename = "ClientRemaining")]
|
|
|
|
pub client_remaining: i32,
|
|
|
|
}
|
|
|
|
|
2022-05-18 18:16:42 +00:00
|
|
|
pub async fn rate_limit(client: &ImgurClient) -> anyhow::Result<RateLimitInfo> {
|
2022-04-03 19:01:58 +00:00
|
|
|
// get imgur api url
|
2022-01-25 18:47:08 +00:00
|
|
|
let uri = api_url!("credits");
|
2022-01-22 21:02:51 +00:00
|
|
|
|
2022-04-03 19:01:58 +00:00
|
|
|
// send request to imgur api
|
|
|
|
let res = send_api_request(client, Method::GET, uri, None).await?;
|
|
|
|
|
|
|
|
// get response http code
|
2022-01-22 21:02:51 +00:00
|
|
|
let status = res.status();
|
|
|
|
|
2022-04-03 19:01:58 +00:00
|
|
|
// check if an error has occurred
|
2022-01-22 21:02:51 +00:00
|
|
|
if status.is_client_error() || status.is_server_error() {
|
2022-04-03 19:01:58 +00:00
|
|
|
let body = res.text().await?;
|
|
|
|
|
|
|
|
let err = io::Error::new(
|
|
|
|
io::ErrorKind::Other,
|
2022-01-26 19:57:51 +00:00
|
|
|
format!("server returned non-successful status code = {status}, body = {body}"),
|
|
|
|
);
|
|
|
|
|
2022-04-03 19:11:11 +00:00
|
|
|
Err(err.into())
|
2022-01-22 21:02:51 +00:00
|
|
|
} else {
|
2022-04-03 19:01:58 +00:00
|
|
|
let content = res.json::<RateLimitInfo>().await?;
|
2022-01-22 21:02:51 +00:00
|
|
|
Ok(content)
|
|
|
|
}
|
|
|
|
}
|