RateLimit header fields for HTTP
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
|
||||
use hyper::Method;
|
||||
use reqwest::header::HeaderMap;
|
||||
use serde::{Serialize, de::DeserializeOwned};
|
||||
use std::time::Duration;
|
||||
|
||||
@@ -14,6 +15,30 @@ pub struct HttpRequest {
|
||||
pub password: Option<String>,
|
||||
}
|
||||
|
||||
pub struct HttpResponseFull {
|
||||
pub status: reqwest::StatusCode,
|
||||
pub headers: HeaderMap,
|
||||
pub body: String,
|
||||
}
|
||||
|
||||
impl HttpResponseFull {
|
||||
pub fn header(&self, name: &str) -> Option<&str> {
|
||||
self.headers.get(name).and_then(|v| v.to_str().ok())
|
||||
}
|
||||
|
||||
pub fn rate_limit_policy(&self) -> Option<&str> {
|
||||
self.header("RateLimit-Policy")
|
||||
}
|
||||
|
||||
pub fn rate_limit(&self) -> Option<&str> {
|
||||
self.header("RateLimit")
|
||||
}
|
||||
|
||||
pub fn retry_after(&self) -> Option<u64> {
|
||||
self.header("Retry-After").and_then(|v| v.parse().ok())
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for HttpRequest {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
@@ -94,6 +119,43 @@ impl HttpRequest {
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn send_full(
|
||||
&self,
|
||||
method: Method,
|
||||
query: &str,
|
||||
body: Option<Vec<u8>>,
|
||||
content_type: Option<&str>,
|
||||
) -> HttpResponseFull {
|
||||
let mut request = reqwest::Client::builder()
|
||||
.timeout(Duration::from_secs(5))
|
||||
.danger_accept_invalid_certs(true)
|
||||
.build()
|
||||
.unwrap()
|
||||
.request(method, format!("https://127.0.0.1:{}{query}", self.port));
|
||||
|
||||
if let Some(body) = body {
|
||||
request = request.body(body);
|
||||
}
|
||||
|
||||
if let Some(ct) = content_type {
|
||||
request = request.header(hyper::header::CONTENT_TYPE, ct);
|
||||
}
|
||||
|
||||
if let (Some(username), Some(password)) = (&self.username, &self.password) {
|
||||
request = request.basic_auth(username, Some(password));
|
||||
}
|
||||
|
||||
let response = request.send().await.expect("HTTP request failed");
|
||||
let status = response.status();
|
||||
let headers = response.headers().clone();
|
||||
let body = response.text().await.unwrap_or_default();
|
||||
HttpResponseFull {
|
||||
status,
|
||||
headers,
|
||||
body,
|
||||
}
|
||||
}
|
||||
|
||||
async fn request_raw(
|
||||
&self,
|
||||
method: Method,
|
||||
|
||||
Reference in New Issue
Block a user