| 1 | #[cfg(feature = "hyper-requester")] |
| 2 | mod hyper; |
| 3 | #[cfg(feature = "hyper-requester")] |
| 4 | pub use hyper::*; |
| 5 | |
| 6 | #[cfg(feature = "reqwest-requester")] |
| 7 | mod reqwest; |
| 8 | #[cfg(feature = "reqwest-requester")] |
| 9 | pub use reqwest::*; |
| 10 | |
| 11 | use async_trait::async_trait; |
| 12 | use std::collections::HashMap; |
| 13 | |
| 14 | /// A general trait to query data from the internet. |
| 15 | /// |
| 16 | /// This makes requests to a specified API with a specified body and headers, returning the bytes received. |
| 17 | /// It only supports limited HTTP(s) capabilities for now, further capabilities may be added if required. |
| 18 | #[cfg_attr(feature = "rt-multi-thread", async_trait)] |
| 19 | #[cfg_attr(not(feature = "rt-multi-thread"), async_trait(?Send))] |
| 20 | pub trait Requester: Send + Sync { |
| 21 | /// Errors this requester may produce. |
| 22 | type Error: std::error::Error; |
| 23 | |
| 24 | /// Make a GET request. |
| 25 | async fn get( |
| 26 | &self, |
| 27 | url: &url::Url, |
| 28 | body: &[u8], |
| 29 | headers: HashMap<&str, &str>, |
| 30 | ) -> Result<Vec<u8>, Self::Error>; |
| 31 | |
| 32 | /// Make a POST request. |
| 33 | async fn post( |
| 34 | &self, |
| 35 | url: &url::Url, |
| 36 | body: &[u8], |
| 37 | headers: HashMap<&str, &str>, |
| 38 | ) -> Result<Vec<u8>, Self::Error>; |
| 39 | } |
| 40 | |
| 41 | /// Build a [`Requester`]. |
| 42 | /// |
| 43 | /// This allows a provider to specify e.g. custom certificates. |
| 44 | pub trait RequesterBuilder { |
| 45 | /// The [`Requester`] built by this builder. |
| 46 | type Requester: Requester; |
| 47 | |
| 48 | /// Add a custom certificate, formatted as a PEM-bundle which should be accepted. |
| 49 | fn with_pem_bundle(self, bytes: &[u8]) -> Self; |
| 50 | |
| 51 | /// Build the [`Requester`]. |
| 52 | fn build(self) -> Self::Requester; |
| 53 | } |