summaryrefslogtreecommitdiff
path: root/src/client/connector/connectors/httpconnector.rs
blob: 5a5b0dfeb0ef87b490778054a2f835e69bac3805 (plain)
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
44
45
use crate::connector::*;
use std::{thread::sleep, time::Duration};

pub(crate) struct HTTPConnector {
    prev: String,
    url: reqwest::Url,
}

impl HTTPConnector {
    pub fn new(url: &str) -> Result<Self, url::ParseError> {
        Ok(Self {
            prev: String::new(),
            url: reqwest::Url::parse(url)?,
        })
    }
}

impl Connection for HTTPConnector {
    type Err = reqwest::Error;

    fn update(&self) -> Option<Result<Self::Success, Self::Err>> {
        let res = match reqwest::blocking::get(self.url.as_str()) {
            Ok(resp) => {
                match resp.status() {
                    reqwest::StatusCode::TOO_MANY_REQUESTS(e) => {
                        sleep(Duration::from_secs(5));
                        return e;
                    }
                    status => return Some(Err(status)),
                }
                match resp.text() {
                    Ok(text) => text,
                    Err(e) => return Some(Err(e)),
                }
            }
            Err(e) => return Some(Err(e)),
        };

        if res == self.prev {
            return None;
        }

        Some(Ok(res))
    }
}