I am hoping to improve some logic that crafts and returns a Future for a reqwest::Response
by adding in logic to catch, reauthenticate, and retry when the response is a 401. However, even before I get to that retry logic, my attempts to move the creation of the reqwest::Response
into an async method (in which the reauthentication logic would be placed) are failing with lifetime errors.
The original working code to be replaced is a non-async method in the struct returning a boxed future; self.Client
is a reqwest::Client
and the the project is using a tokio runtime:
pub fn demo_good(
&mut self,
url: Url,
) -> Pin<Box<dyn futures::Future<Output = Result<Response, reqwest::Error>> + Send>> {
let mut map = HashMap::new();
map.insert("fake_key".to_string(), "fake_value".to_string());
let response = self.client.post(url).json(&map).send();
Box::pin(response)
}
However, as soon as I try to start moving the self.client.post(...)...
logic into an async method of the struct, I get a lifetime error:
// POST to the execute_url, handling reauthentication
async fn demo_post(
&mut self,
url: Url,
body: HashMap<String, String>,
) -> Result<Response, reqwest::Error> {
// Run first request
let response = self.client.post(url).json(&body).send().await;
// Todo: catch 401s, reauth, and repeat
dbg!(&response);
// Return
response
}
pub fn demo_bad(
&mut self,
url: Url,
) -> Pin<Box<dyn futures::Future<Output = Result<Response, reqwest::Error>> + Send>> {
let mut map = HashMap::new();
map.insert("fake_key".to_string(), "fake_value".to_string());
let response = self.demo_post(url, map);
Box::pin(response)
}
error: lifetime may not live long enough
--> src/source.rs:618:9
|
612 | &mut self,
| - let's call the lifetime of this reference `'1`
...
618 | Box::pin(response)
| ^^^^^^^^^^^^^^^^^^ returning this value requires that `'1` must outlive `'static`
|
help: to declare that the trait object captures data from argument `self`, you can add an explicit `'_` lifetime bound
|
614 | ) -> Pin<Box<dyn futures::Future<Output = Result<Response, reqwest::Error>> + Send + '_>> {
| ++++
Is there a way to make a strategy like this work, maybe by specifying lifetime parameters in some of the method signatures? Or is it doomed to fail, because of the difference between the original reqwest methods (which are non-async and return Futures) and the wrapper method I added (which is async), and there is some different design to use?
The above examples are excerpted from a larger code base; I think the overall goal and issues are relatively self contained in this but if not I will work on a MRE.