aboutsummaryrefslogtreecommitdiffstats
path: root/lib/auth-service/src/client/http.rs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/auth-service/src/client/http.rs')
-rw-r--r--lib/auth-service/src/client/http.rs56
1 files changed, 56 insertions, 0 deletions
diff --git a/lib/auth-service/src/client/http.rs b/lib/auth-service/src/client/http.rs
new file mode 100644
index 0000000..5621fb0
--- /dev/null
+++ b/lib/auth-service/src/client/http.rs
@@ -0,0 +1,56 @@
+use std::ops::Deref;
+
+use oauth2::http;
+
+#[derive(Clone, Debug)]
+pub struct HttpAuthClient(reqwest::Client);
+
+impl From<reqwest::Client> for HttpAuthClient {
+ fn from(value: reqwest::Client) -> Self {
+ Self(value)
+ }
+}
+
+impl Deref for HttpAuthClient {
+ type Target = reqwest::Client;
+
+ fn deref(&self) -> &Self::Target {
+ &self.0
+ }
+}
+
+impl<'c> oauth2::AsyncHttpClient<'c> for HttpAuthClient {
+ type Error = oauth2::HttpClientError<reqwest::Error>;
+
+ #[cfg(target_arch = "wasm32")]
+ type Future = Pin<Box<dyn Future<Output = Result<HttpResponse, Self::Error>> + 'c>>;
+ #[cfg(not(target_arch = "wasm32"))]
+ type Future = std::pin::Pin<
+ Box<dyn Future<Output = Result<oauth2::HttpResponse, Self::Error>> + Send + Sync + 'c>,
+ >;
+
+ fn call(&'c self, request: oauth2::HttpRequest) -> Self::Future {
+ Box::pin(async move {
+ let response = self
+ .0
+ .execute(request.try_into().map_err(Box::new)?)
+ .await
+ .map_err(Box::new)?;
+
+ let mut builder = http::Response::builder().status(response.status());
+
+ #[cfg(not(target_arch = "wasm32"))]
+ {
+ builder = builder.version(response.version());
+ }
+
+ for (name, value) in response.headers().iter() {
+ builder = builder.header(name, value);
+ }
+
+ builder
+ .body(response.bytes().await.map_err(Box::new)?.to_vec())
+ .map_err(oauth2::HttpClientError::Http)
+ })
+ }
+}