twitch_oauth2/
client.rs

1//! Provides different http clients
2
3// This module is heavily inspired (read: copied) by twitch_api2::client.
4
5use std::error::Error;
6use std::future::Future;
7
8/// The User-Agent `product` of this crate.
9pub static TWITCH_OAUTH2_USER_AGENT: &str =
10    concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"),);
11
12/// A boxed future, mimics `futures::future::BoxFuture`
13type BoxedFuture<'a, T> = std::pin::Pin<Box<dyn Future<Output = T> + Send + 'a>>;
14
15/// A client that can do OAUTH2 requests
16pub trait Client: Sync + Send {
17    /// Error returned by the client
18    type Error: Error + Send + Sync + 'static;
19    /// Send a request
20    fn req(
21        &self,
22        request: http::Request<Vec<u8>>,
23    ) -> BoxedFuture<'_, Result<http::Response<Vec<u8>>, <Self as Client>::Error>>;
24}
25
26#[doc(hidden)]
27#[derive(Debug, thiserror::Error, Clone)]
28#[error("this client does not do anything, only used for documentation test that only checks code integrity")]
29pub struct DummyClient;
30
31#[cfg(feature = "reqwest")]
32impl Client for DummyClient {
33    type Error = DummyClient;
34
35    fn req(
36        &self,
37        _: http::Request<Vec<u8>>,
38    ) -> BoxedFuture<'_, Result<http::Response<Vec<u8>>, Self::Error>> {
39        Box::pin(async move { Err(self.clone()) })
40    }
41}
42#[cfg(feature = "reqwest")]
43use reqwest::Client as ReqwestClient;
44
45#[cfg(feature = "reqwest")]
46impl Client for ReqwestClient {
47    type Error = reqwest::Error;
48
49    fn req(
50        &self,
51        request: http::Request<Vec<u8>>,
52    ) -> BoxedFuture<'_, Result<http::Response<Vec<u8>>, Self::Error>> {
53        // Reqwest plays really nice here and has a try_from on `http::Request` -> `reqwest::Request`
54        let req = match reqwest::Request::try_from(request) {
55            Ok(req) => req,
56            Err(e) => return Box::pin(async { Err(e) }),
57        };
58        // We need to "call" the execute outside the async closure to not capture self.
59        let fut = self.execute(req);
60        Box::pin(async move {
61            // Await the request and translate to `http::Response`
62            let mut response = fut.await?;
63            let mut result = http::Response::builder().status(response.status());
64            let headers = result
65                .headers_mut()
66                // This should not fail, we just created the response.
67                .expect("expected to get headers mut when building response");
68            std::mem::swap(headers, response.headers_mut());
69            let result = result.version(response.version());
70            Ok(result
71                .body(response.bytes().await?.as_ref().to_vec())
72                .expect("mismatch reqwest -> http conversion should not fail"))
73        })
74    }
75}