-
-
Notifications
You must be signed in to change notification settings - Fork 325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use tower-service
to build more robust clients
#100
Comments
Hey there, thank you! And thanks for all your work on tower, even though I haven't actually gotten to use it yet. Short answer; Yes, possibly!Being able to replace reqwest has certainly been asked requested before 🙂 Long answerThe ideas that That said, embedding a bunch more traits herein certainly looks like it'll make things a lot more complicated, and I have a few questions if you have are able to answer:
|
So for server side of things, Im not sure exactly what you are looking for around this but it shouldn't affect anything because in the end it all turns into a future. For streaming calls, that would be abstracted via the So Also sean's latest blog post talks about tower service which should explain it a bit better https://seanmonstar.com/ I am hopping to write soon a blog post that should explain how I think these pieces should fit together. I would also be happy to submit a PR at some point in the future to add this if no one else does :D |
It makes a lot more sense now, thank you. I would be happy to take a PR for this. |
Hi @LucioFranco, I've been experimenting to swap I'm thinking of providing let client: HyperClient<_, Body> = HyperClient::builder().build(connector);
let inner = ServiceBuilder::new()
.map_request(move |r| set_cluster_url(r, &cluster_url))
.map_request(move |r| set_default_headers(r, default_headers.clone()))
.service(client); I'd like to handle This is currently done in let (mut parts, body) = request.into_parts();
if let Some(auth_header) = self.auth_header.to_header().await? {
parts.headers.insert(http::header::AUTHORIZATION, auth_header);
} Because of some restrictions to make this fit in Buffer<BoxService<Request<hyper::Body>, Response<hyper::Body>, hyper::Error>, Request<hyper::Body>>
|
I'm not good at this either; I would've hoped a |
Creating
fn auth_layer<S>(
auth: Authentication,
) -> impl Layer<
S,
Service = impl Service<
Request<Body>,
Response = Response<Body>,
Error = BoxError,
Future = impl Future<Output = Result<Response<Body>, BoxError>> + Send,
> + Clone,
> + Clone
where
S: 'static + Service<Request<Body>, Response = Response<Body>> + Clone + Send,
S::Error: Send + Sync,
BoxError: From<S::Error>,
S::Future: Send,
{
tower::layer::layer_fn(move |svc: S| {
let auth = auth.clone();
tower::service_fn(move |mut req: Request<Body>| {
let auth = auth.clone();
let mut svc = svc.clone();
async move {
if let Some(auth_header) = auth.to_header().await? {
req.headers_mut().insert(AUTHORIZATION, auth_header);
}
Ok(svc.call(req).await?)
}
})
})
} |
I don't think we can get rid of |
Hi this project is fantastic!
I was curious if you'd be interested in possibly making the api use
tower_service::Service
intead of just usingreqwest
internally.A couple big reasons this might be good is that you don't need to depend on
reqwest
directly which can be a heavy dep. You can let users easily configure how they might wanna do retries, timeouts, etc. It is easier to use a mock liketower-test
to let users test their kube code.Now tower is still getting worked on as you can currently see with the master branch but I know at least for some of my use cases I would love to have this support.
Example code that I have used before with the older tower-service but should work just the same with
0.3
is here https://github.com/LucioFranco/tower-consul/blob/master/src/lib.rs#L33If you notice this entire crate doesn't depend on hyper but allows you to plug hyper in. Hyper
0.13
will also supportService
out of the box.I wrote this a bit quick but just wanted to start a discussion!
The text was updated successfully, but these errors were encountered: