Skip to content

Commit 33834b1

Browse files
committed
feat(transport): Client 抽离出 HTTP Transport
1 parent 427685a commit 33834b1

14 files changed

Lines changed: 908 additions & 657 deletions

File tree

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ thiserror = "2.0.18"
4141
hex = "0.4.3"
4242
dotenv = "0.15.0"
4343
bytes = "1.11.1"
44-
rand = "0.10.1"
44+
rand = "0.10.1"
45+
retry = "2.2.0"

src/core/client/base_client.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use reqwest::{Client, Request, Response};
2+
use crate::core::client::config::ClientConfig;
3+
4+
#[derive(Clone)]
5+
pub struct BaseClient {
6+
inner: Client,
7+
}
8+
9+
impl BaseClient {
10+
pub fn new(config: ClientConfig) -> anyhow::Result<Self> {
11+
let mut builder = Client::builder()
12+
.timeout(config.timeout)
13+
.connect_timeout(config.connect_timeout)
14+
.user_agent(config.user_agent);
15+
16+
if let Some(proxy) = config.proxy {
17+
builder = builder.proxy(reqwest::Proxy::all(proxy)?);
18+
}
19+
20+
let client = builder.build()?;
21+
22+
Ok(Self {
23+
inner: client,
24+
})
25+
}
26+
27+
pub async fn execute(
28+
&self,
29+
request: Request,
30+
) -> Result<Response, reqwest::Error> {
31+
self.inner.execute(request).await
32+
}
33+
34+
pub fn inner(&self) -> &Client {
35+
&self.inner
36+
}
37+
}

src/core/client/config.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use std::time::Duration;
2+
3+
#[derive(Clone)]
4+
pub struct ClientConfig {
5+
pub timeout: Duration,
6+
pub connect_timeout: Duration,
7+
pub proxy: Option<String>,
8+
pub user_agent: String,
9+
}
10+
11+
impl Default for ClientConfig {
12+
fn default() -> Self {
13+
Self {
14+
timeout: Duration::from_secs(300),
15+
connect_timeout: Duration::from_secs(10),
16+
proxy: None,
17+
user_agent: "agent-runtime/0.1.0".into(),
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)