drand_client_rs/
http.rs

1//! # http
2//!
3//! basic HTTP connectors
4//!
5
6use crate::{Transport, TransportError};
7use reqwest::blocking::Client;
8
9use reqwest::StatusCode;
10
11pub struct HttpTransport {
12    pub client: Client,
13}
14
15impl Transport for HttpTransport {
16    fn fetch(&self, url: &str) -> Result<String, TransportError> {
17        let res = self
18            .client
19            .get(url)
20            .send()
21            .map_err(|_| TransportError::Unexpected)?;
22
23        match res.status() {
24            StatusCode::OK => res.text().map_err(|_| TransportError::Unexpected),
25
26            StatusCode::NOT_FOUND => Err(TransportError::NotFound),
27
28            _ => Err(TransportError::Unexpected),
29        }
30    }
31}
32
33/// a simple implementation of the `Transport` trait using `reqwest` for HTTP endpoints
34pub fn new_http_transport() -> HttpTransport {
35    HttpTransport {
36        client: Client::new(),
37    }
38}