implement http sward
This commit is contained in:
@@ -6,4 +6,7 @@ edition = "2024"
|
||||
[dependencies]
|
||||
tokio = { workspace = true }
|
||||
tower = { workspace = true }
|
||||
reqwest = {workspace = true}
|
||||
reqwest = {workspace = true}
|
||||
url = {workspace = true}
|
||||
rand = {workspace = true}
|
||||
bytes = {workspace = true}
|
||||
53
ubw-sward/src/http.rs
Normal file
53
ubw-sward/src/http.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
use bytes::Bytes;
|
||||
use reqwest::header::HeaderMap;
|
||||
use reqwest::{Client, Method};
|
||||
use std::pin::Pin;
|
||||
use std::task::{Context, Poll};
|
||||
use tower::Service;
|
||||
use url::Url;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SimpleHttpSward {
|
||||
client: Client,
|
||||
method: Method,
|
||||
headers: HeaderMap,
|
||||
sent_count: usize,
|
||||
}
|
||||
|
||||
impl SimpleHttpSward {
|
||||
pub fn new(client: Client, method: Method, headers: HeaderMap) -> Self {
|
||||
Self {
|
||||
client,
|
||||
method,
|
||||
headers,
|
||||
sent_count: 0,
|
||||
}
|
||||
}
|
||||
pub fn sent_count(&self) -> usize {
|
||||
self.sent_count
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SimpleHttpRequest {
|
||||
pub body: Option<Bytes>,
|
||||
pub url: Url,
|
||||
}
|
||||
|
||||
impl Service<SimpleHttpRequest> for SimpleHttpSward {
|
||||
type Response = reqwest::Response;
|
||||
type Error = reqwest::Error;
|
||||
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;
|
||||
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
Poll::Ready(Ok(()))
|
||||
}
|
||||
fn call(&mut self, req: SimpleHttpRequest) -> Self::Future {
|
||||
self.sent_count += 1;
|
||||
Box::pin(
|
||||
self.client
|
||||
.request(self.method.clone(), req.url)
|
||||
.headers(self.headers.clone())
|
||||
.send(),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
pub mod http;
|
||||
Reference in New Issue
Block a user