axum_security_oauth2/http/
mod.rs1#[cfg(feature = "reqwest")]
2pub(crate) mod dep_reqwest;
3
4use std::fmt;
5
6use url::Url;
7
8use crate::error::HttpError;
9
10#[derive(Clone)]
21#[non_exhaustive]
22pub enum HttpClient {
23 #[cfg(feature = "reqwest")]
24 Reqwest(reqwest::Client),
25}
26
27#[cfg(feature = "reqwest")]
28impl HttpClient {
29 pub fn default_reqwest() -> Self {
32 HttpClient::Reqwest(dep_reqwest::default_client())
33 }
34}
35
36#[cfg(feature = "reqwest")]
37impl From<reqwest::Client> for HttpClient {
38 fn from(client: reqwest::Client) -> Self {
39 HttpClient::Reqwest(client)
40 }
41}
42
43impl fmt::Debug for HttpClient {
44 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45 let _ = f;
46 match *self {
47 #[cfg(feature = "reqwest")]
48 HttpClient::Reqwest(_) => f.write_str("HttpClient::Reqwest(..)"),
49 }
50 }
51}
52
53pub(crate) struct FormResponse {
56 pub(crate) status: u16,
57 pub(crate) content_type: Option<String>,
58 pub(crate) body: Vec<u8>,
59}
60
61pub struct HttpResponse {
66 pub status: u16,
68 pub body: Vec<u8>,
70}
71
72impl HttpResponse {
73 pub fn is_success(&self) -> bool {
75 (200..300).contains(&self.status)
76 }
77}
78
79impl HttpClient {
80 #[cfg_attr(not(feature = "reqwest"), allow(unused_variables))]
83 pub(crate) async fn post_form(
84 &self,
85 url: &Url,
86 form: &[(&str, &str)],
87 authorization: Option<&str>,
88 ) -> Result<FormResponse, HttpError> {
89 match *self {
90 #[cfg(feature = "reqwest")]
91 HttpClient::Reqwest(ref client) => {
92 dep_reqwest::post_form(client, url, form, authorization).await
93 }
94 }
95 }
96
97 #[cfg_attr(not(feature = "reqwest"), allow(unused_variables))]
101 pub async fn get(&self, url: &Url) -> Result<HttpResponse, HttpError> {
102 match *self {
103 #[cfg(feature = "reqwest")]
104 HttpClient::Reqwest(ref client) => dep_reqwest::get(client, url).await,
105 }
106 }
107}