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
use arangors::connection::role::Normal;
use arangors::ClientError;
use mobc::{async_trait, Manager};

#[cfg(feature = "reqwest")]
use arangors::client::reqwest::ReqwestClient;
#[cfg(feature = "surf")]
use arangors::client::surf::SurfClient;

#[derive(Clone, Debug)]
pub struct ArangoDBConnectionManager {
    url: String,
    username: String,
    password: String,
    use_jwt: bool,
    validate: bool,
}

impl ArangoDBConnectionManager {
    pub fn new(
        url: &str,
        username: &str,
        password: &str,
        use_jwt: bool,
        validate: bool,
    ) -> ArangoDBConnectionManager {
        ArangoDBConnectionManager {
            url: url.to_owned(),
            username: username.to_owned(),
            password: password.to_owned(),
            use_jwt,
            validate,
        }
    }
}

#[async_trait]
impl Manager for ArangoDBConnectionManager {
    type Connection = arangors::Connection;
    type Error = ClientError;

    async fn connect(&self) -> Result<Self::Connection, Self::Error> {
        if self.use_jwt == true {
            let client =
                arangors::Connection::establish_jwt(&self.url, &self.username, &self.password)
                    .await?;
            return Ok(client);
        } else {
            let client = arangors::Connection::establish_basic_auth(
                &self.url,
                &self.username,
                &self.password,
            )
            .await?;
            return Ok(client);
        }
    }

    #[cfg(feature = "surf")]
    async fn check(&self, conn: Self::Connection) -> Result<Self::Connection, Self::Error> {
        if self.validate {
            arangors::connection::GenericConnection::<SurfClient, Normal>::validate_server(
                &self.url,
            )
            .await?;
        }

        Ok(conn)
    }
    #[cfg(feature = "reqwest")]
    async fn check(&self, conn: Self::Connection) -> Result<Self::Connection, Self::Error> {
        if self.validate {
            arangors::connection::GenericConnection::<ReqwestClient, Normal>::validate_server(
                &self.url,
            )
            .await?;
        }

        Ok(conn)
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
}