Skip to content

Commit fada3f4

Browse files
authored
minor: add snippets for connecting with sync API and x509 authentication (#492)
1 parent 8768f63 commit fada3f4

File tree

1 file changed

+99
-11
lines changed

1 file changed

+99
-11
lines changed

tests/connection_snippets.rs

Lines changed: 99 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,104 @@
55
extern crate mongodb;
66

77
#[cfg(feature = "tokio-runtime")]
8-
// CONNECTION EXAMPLE STARTS HERE
9-
#[tokio::main]
10-
async fn main() -> mongodb::error::Result<()> {
8+
mod async_scram {
9+
// ASYNC SCRAM CONNECTION EXAMPLE STARTS HERE
1110
use mongodb::{options::ClientOptions, Client};
12-
let client_options = ClientOptions::parse(
13-
"mongodb+srv://<username>:<password>@<cluster-url>/<dbname>?w=majority",
14-
)
15-
.await?;
16-
let client = Client::with_options(client_options)?;
17-
let database = client.database("test");
18-
Ok(())
11+
12+
#[tokio::main]
13+
async fn main() -> mongodb::error::Result<()> {
14+
let client_options = ClientOptions::parse(
15+
"mongodb+srv://<username>:<password>@<cluster-url>/<dbname>?w=majority",
16+
)
17+
.await?;
18+
let client = Client::with_options(client_options)?;
19+
let database = client.database("test");
20+
// do something with database
21+
22+
Ok(())
23+
}
24+
// CONNECTION EXAMPLE ENDS HERE
25+
}
26+
27+
#[cfg(feature = "tokio-runtime")]
28+
mod async_x509 {
29+
// ASYNC X509 CONNECTION EXAMPLE STARTS HERE
30+
use mongodb::{
31+
options::{AuthMechanism, ClientOptions, Credential, Tls, TlsOptions},
32+
Client,
33+
};
34+
use std::path::PathBuf;
35+
36+
#[tokio::main]
37+
async fn main() -> mongodb::error::Result<()> {
38+
let mut client_options =
39+
ClientOptions::parse("mongodb+srv://<cluster-url>/<dbname>?w=majority").await?;
40+
client_options.credential = Some(
41+
Credential::builder()
42+
.mechanism(AuthMechanism::MongoDbX509)
43+
.build(),
44+
);
45+
let tls_options = TlsOptions::builder()
46+
.ca_file_path(PathBuf::from("/path/to/ca-cert"))
47+
.cert_key_file_path(PathBuf::from("/path/to/cert"))
48+
.build();
49+
client_options.tls = Some(Tls::Enabled(tls_options));
50+
let client = Client::with_options(client_options)?;
51+
52+
let database = client.database("test");
53+
// do something with database
54+
55+
Ok(())
56+
}
57+
// CONNECTION EXAMPLE ENDS HERE
58+
}
59+
60+
#[cfg(feature = "sync")]
61+
mod sync_scram {
62+
// SYNC SCRAM CONNECTION EXAMPLE STARTS HERE
63+
use mongodb::{options::ClientOptions, sync::Client};
64+
65+
fn main() -> mongodb::error::Result<()> {
66+
let client_options = ClientOptions::parse(
67+
"mongodb+srv://<username>:<password>@<cluster-url>/<dbname>?w=majority",
68+
)?;
69+
let client = Client::with_options(client_options)?;
70+
let database = client.database("test");
71+
// do something with database
72+
73+
Ok(())
74+
}
75+
// CONNECTION EXAMPLE ENDS HERE
76+
}
77+
78+
#[cfg(feature = "sync")]
79+
mod sync_x509 {
80+
// SYNC X509 CONNECTION EXAMPLE STARTS HERE
81+
use mongodb::{
82+
options::{AuthMechanism, ClientOptions, Credential, Tls, TlsOptions},
83+
sync::Client,
84+
};
85+
use std::path::PathBuf;
86+
87+
fn main() -> mongodb::error::Result<()> {
88+
let mut client_options =
89+
ClientOptions::parse("mongodb+srv://<cluster-url>/<dbname>?w=majority")?;
90+
client_options.credential = Some(
91+
Credential::builder()
92+
.mechanism(AuthMechanism::MongoDbX509)
93+
.build(),
94+
);
95+
let tls_options = TlsOptions::builder()
96+
.ca_file_path(PathBuf::from("/path/to/ca-cert"))
97+
.cert_key_file_path(PathBuf::from("/path/to/cert"))
98+
.build();
99+
client_options.tls = Some(Tls::Enabled(tls_options));
100+
let client = Client::with_options(client_options)?;
101+
102+
let database = client.database("test");
103+
// do something with database
104+
105+
Ok(())
106+
}
107+
// CONNECTION EXAMPLE ENDS HERE
19108
}
20-
// CONNECTION EXAMPLE ENDS HERE

0 commit comments

Comments
 (0)