1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use std::time::Duration;

use anyhow::Result;
use reqwest::{Client, Method, Request, Url};
use tokio::time::sleep;

use crate::{WaitOptions, Waitable};

pub struct HttpWaiter {
    pub method: Method,
    pub url: Url,
}

impl HttpWaiter {
    pub fn new(method: Method, url: Url) -> Self {
        Self { method, url }
    }
}

impl Waitable for HttpWaiter {
    async fn wait(self, _: WaitOptions) -> Result<()> {
        let client = Client::new();
        let request = Request::new(self.method, self.url);

        loop {
            if let Some(req) = request.try_clone() {
                match client.execute(req).await {
                    Ok(res) => {
                        println!("Got {}", res.status());
                        break;
                    }
                    Err(err) => {
                        println!("Rec {}", err);
                        sleep(Duration::from_secs(1)).await;
                        continue;
                    }
                }
            }
        }

        Ok(())
    }
}