1use arangors::connection::role::Normal;
2use arangors::ClientError;
3use mobc::{async_trait, Manager};
4
5#[cfg(feature = "reqwest")]
6use arangors::client::reqwest::ReqwestClient;
7#[cfg(feature = "surf")]
8use arangors::client::surf::SurfClient;
9
10#[derive(Clone, Debug)]
11pub struct ArangoDBConnectionManager {
12 url: String,
13 username: String,
14 password: String,
15 use_jwt: bool,
16 validate: bool,
17}
18
19impl ArangoDBConnectionManager {
20 pub fn new(
21 url: &str,
22 username: &str,
23 password: &str,
24 use_jwt: bool,
25 validate: bool,
26 ) -> ArangoDBConnectionManager {
27 ArangoDBConnectionManager {
28 url: url.to_owned(),
29 username: username.to_owned(),
30 password: password.to_owned(),
31 use_jwt,
32 validate,
33 }
34 }
35}
36
37#[async_trait]
38impl Manager for ArangoDBConnectionManager {
39 type Connection = arangors::Connection;
40 type Error = ClientError;
41
42 async fn connect(&self) -> Result<Self::Connection, Self::Error> {
43 if self.use_jwt == true {
44 let client =
45 arangors::Connection::establish_jwt(&self.url, &self.username, &self.password)
46 .await?;
47 return Ok(client);
48 } else {
49 let client = arangors::Connection::establish_basic_auth(
50 &self.url,
51 &self.username,
52 &self.password,
53 )
54 .await?;
55 return Ok(client);
56 }
57 }
58
59 #[cfg(feature = "surf")]
60 async fn check(&self, conn: Self::Connection) -> Result<Self::Connection, Self::Error> {
61 if self.validate {
62 arangors::connection::GenericConnection::<SurfClient, Normal>::validate_server(
63 &self.url,
64 )
65 .await?;
66 }
67
68 Ok(conn)
69 }
70 #[cfg(feature = "reqwest")]
71 async fn check(&self, conn: Self::Connection) -> Result<Self::Connection, Self::Error> {
72 if self.validate {
73 arangors::connection::GenericConnection::<ReqwestClient, Normal>::validate_server(
74 &self.url,
75 )
76 .await?;
77 }
78
79 Ok(conn)
80 }
81}
82
83#[cfg(test)]
84mod tests {
85 #[test]
86 fn it_works() {
87 assert_eq!(2 + 2, 4);
88 }
89}