1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use core::fmt;
use reqwest::RequestBuilder;
use std::fmt::Display;

/// Allow using `basic_auth` on both wasm and non-wasm targets
pub trait ReqwestExt: Sized {
    /// Drop in replacement for [`reqwest::RequestBuilder::basic_auth`].
    ///
    /// This is a drop in replacement. However, it requires that the trait is in the context. In
    /// the case of a non-wasm target, the function is present, the trait unused and a warning
    /// will be issued.
    ///
    /// To fix this warning, you can either:
    ///   * Restrict the trait import using `#[cfg(target_arch = "wasm32")]`
    ///   * Use the [`basic_auth_ext`] function
    ///
    /// On non-wasm32 targets, the function directly calls [`reqwest::RequestBuilder::basic_auth`].
    fn basic_auth<U, P>(self, username: U, password: Option<P>) -> Self
    where
        U: fmt::Display,
        P: fmt::Display;

    #[inline]
    fn basic_auth_ext<U, P>(self, username: U, password: Option<P>) -> Self
    where
        U: fmt::Display,
        P: fmt::Display,
    {
        self.basic_auth(username, password)
    }
}

#[cfg(not(target_arch = "wasm32"))]
impl ReqwestExt for RequestBuilder {
    fn basic_auth<U, P>(self, username: U, password: Option<P>) -> RequestBuilder
    where
        U: Display,
        P: Display,
    {
        self.basic_auth(username, password)
    }
}

#[cfg(target_arch = "wasm32")]
impl ReqwestExt for RequestBuilder {
    fn basic_auth<U, P>(self, username: U, password: Option<P>) -> RequestBuilder
    where
        U: Display,
        P: Display,
    {
        use base64::write::EncoderWriter as Base64Encoder;
        use std::io::Write;

        let mut header_value = b"Basic ".to_vec();
        {
            let mut encoder = Base64Encoder::new(&mut header_value, base64::STANDARD);
            // The unwraps here are fine because Vec::write* is infallible.
            write!(encoder, "{}:", username).unwrap();
            if let Some(password) = password {
                write!(encoder, "{}", password).unwrap();
            }
        }

        self.header(reqwest::header::AUTHORIZATION, header_value)
    }
}

#[cfg(test)]
mod test {

    use reqwest::Client;

    #[test]
    fn test() {
        #[cfg(target_arch = "wasm32")]
        use super::ReqwestExt;

        let client = Client::new();
        // the following line should compile on wasm and non-wasm targets
        let _req = client
            .get("http://localhost")
            .basic_auth("foo", Some("bar"));
    }

    #[test]
    fn test_ext() {
        use super::ReqwestExt;

        let client = Client::new();
        // the following line should compile on wasm and non-wasm targets
        let _req = client
            .get("http://localhost")
            .basic_auth_ext("foo", Some("bar"));
    }
}