Skip to content

Commit 4567829

Browse files
committed
Remove the TwirpClient trait
1 parent 62fc5fd commit 4567829

File tree

4 files changed

+20
-25
lines changed

4 files changed

+20
-25
lines changed

crates/twirp-build/src/lib.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,7 @@ where
108108
service_fqn, m.proto_name,
109109
)
110110
.unwrap();
111-
writeln!(
112-
buf,
113-
" twirp::client::TwirpClient::request(self, url, req).await",
114-
)
115-
.unwrap();
111+
writeln!(buf, " self.request(url, req).await",).unwrap();
116112
writeln!(buf, " }}").unwrap();
117113
}
118114
writeln!(buf, "}}").unwrap();

crates/twirp/src/client.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,6 @@ impl std::fmt::Debug for HttpTwirpClient {
102102
}
103103
}
104104

105-
#[async_trait]
106-
pub trait TwirpClient {
107-
fn with<M>(&self, middleware: M) -> Self
108-
where
109-
M: Middleware;
110-
111-
async fn request<I, O>(&self, url: Url, body: I) -> Result<O>
112-
where
113-
I: prost::Message,
114-
O: prost::Message + Default;
115-
}
116-
117105
impl HttpTwirpClient {
118106
/// Creates a TwirpClient with the default `reqwest::ClientBuilder`.
119107
///
@@ -145,11 +133,9 @@ impl HttpTwirpClient {
145133
middlewares,
146134
})
147135
}
148-
}
149136

150-
#[async_trait]
151-
impl TwirpClient for HttpTwirpClient {
152-
fn with<M>(&self, middleware: M) -> Self
137+
/// Add some middleware to the request stack.
138+
pub fn with<M>(&self, middleware: M) -> Self
153139
where
154140
M: Middleware,
155141
{
@@ -162,7 +148,7 @@ impl TwirpClient for HttpTwirpClient {
162148
}
163149
}
164150

165-
async fn request<I, O>(&self, url: Url, body: I) -> Result<O>
151+
pub async fn request<I, O>(&self, url: Url, body: I) -> Result<O>
166152
where
167153
I: prost::Message,
168154
O: prost::Message + Default,

crates/twirp/src/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ pub trait TestAPIClient {
105105
impl TestAPIClient for HttpTwirpClient {
106106
async fn ping(&self, req: PingRequest) -> Result<PingResponse, TwirpClientError> {
107107
let url = self.base_url.join("test.TestAPI/Ping")?;
108-
client::TwirpClient::request(self, url, req).await
108+
self.request(url, req).await
109109
}
110110

111111
async fn boom(&self, _req: PingRequest) -> Result<PingResponse, TwirpClientError> {

example/src/bin/example-client.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use async_trait::async_trait;
2-
use twirp::client::{HttpTwirpClient, Middleware, Next, TwirpClient, TwirpClientBuilder};
2+
use twirp::client::{HttpTwirpClient, Middleware, Next, TwirpClientBuilder};
33
use twirp::reqwest::{Request, Response};
44
use twirp::url::Url;
55
use twirp::GenericError;
@@ -12,7 +12,7 @@ pub mod service {
1212
}
1313
}
1414

15-
use service::haberdash::v1::MakeHatRequest;
15+
use service::haberdash::v1::{HaberdasherAPIClient, MakeHatRequest, MakeHatResponse};
1616

1717
#[tokio::main]
1818
pub async fn main() -> Result<(), GenericError> {
@@ -65,3 +65,16 @@ impl Middleware for RequestHeaders {
6565
next.run(req).await
6666
}
6767
}
68+
69+
#[derive(Debug)]
70+
struct MockHaberdasherAPIClient;
71+
72+
#[async_trait]
73+
impl HaberdasherAPIClient for MockHaberdasherAPIClient {
74+
async fn make_hat(
75+
&self,
76+
_req: MakeHatRequest,
77+
) -> Result<MakeHatResponse, twirp::client::TwirpClientError> {
78+
todo!()
79+
}
80+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy