implement http sward

This commit is contained in:
2025-09-01 18:53:24 +09:00
parent 6e84b9b2a6
commit f441abd5eb
5 changed files with 122 additions and 2 deletions

View File

@@ -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
View 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(),
)
}
}

View File

@@ -0,0 +1 @@
pub mod http;