|
| 1 | +#![allow(unused_imports)] |
| 2 | + |
| 3 | +cfg_if::cfg_if! { |
| 4 | + if #[cfg(all(feature = "reqwest", feature = "cyper"))] { |
| 5 | + compile_error!("The 'reqwest' and 'cyper' features cannot be enabled at the same time. To use `cyper`, disable default feature first."); |
| 6 | + } else if #[cfg(feature = "reqwest")] { |
| 7 | + pub(crate) use reqwest::{Client, Error, Method, Response, StatusCode, Url, get, header, multipart}; |
| 8 | + } else if #[cfg(feature = "cyper")] { |
| 9 | + pub(crate) use cyper::{Client, Response, Error, multipart}; |
| 10 | + pub(crate) use url::Url; |
| 11 | + pub(crate) use http::{Method, StatusCode, header}; |
| 12 | + pub(crate) use cyper_ext::*; |
| 13 | + } else { |
| 14 | + compile_error!("Either the 'reqwest' or 'compio' feature must be enabled"); |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +pub(crate) trait CheckError: Sized { |
| 19 | + type Ok; |
| 20 | + type Error; |
| 21 | + fn check(self) -> Result<Self::Ok, Self::Error>; |
| 22 | +} |
| 23 | + |
| 24 | +#[cfg(feature = "reqwest")] |
| 25 | +impl CheckError for reqwest::RequestBuilder { |
| 26 | + type Error = reqwest::Error; |
| 27 | + type Ok = reqwest::RequestBuilder; |
| 28 | + |
| 29 | + #[inline(always)] |
| 30 | + fn check(self) -> Result<Self, Self::Error> { |
| 31 | + Ok(self) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +#[cfg(feature = "cyper")] |
| 36 | +mod cyper_ext { |
| 37 | + use cyper::multipart::Part; |
| 38 | + use mime::FromStrError; |
| 39 | + |
| 40 | + use super::*; |
| 41 | + |
| 42 | + pub(crate) trait PartExt: Sized { |
| 43 | + fn mime_str(self, mime: &str) -> Result<Part, FromStrError>; |
| 44 | + } |
| 45 | + |
| 46 | + impl PartExt for multipart::Part { |
| 47 | + fn mime_str(self, mime: &str) -> Result<Part, FromStrError> { |
| 48 | + let mime = mime.parse()?; |
| 49 | + Ok(self.mime(mime)) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + #[cfg(test)] |
| 54 | + pub(crate) async fn get<T: cyper::IntoUrl>(url: T) -> Result<Response, cyper::Error> { |
| 55 | + Client::new().get(url)?.send().await |
| 56 | + } |
| 57 | + |
| 58 | + impl<T> CheckError for cyper::Result<T> { |
| 59 | + type Error = cyper::Error; |
| 60 | + type Ok = T; |
| 61 | + |
| 62 | + #[inline(always)] |
| 63 | + fn check(self) -> Result<T, Self::Error> { |
| 64 | + self |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments