1pub mod nakama;
2
3pub mod util {
4
5 use crate::nakama::{self, api::Session};
6 use nakama::api::nakama_client::NakamaClient;
7 use tonic::{metadata::MetadataValue, transport::*, Request};
8
9 pub async fn connect_with_server_key(
10 address: Option<&'static str>,
11 server_key: Option<&'static str>,
12 ) -> Result<NakamaClient<Channel>, tonic::transport::Error> {
13 let address = address.unwrap_or("https://[::1]:7349");
14 let server_key = server_key.unwrap_or("defaultkey");
15 let channel = tonic::transport::Channel::from_static(address)
16 .connect()
17 .await?;
18 let auth_string = format!("Basic {}", base64::encode(format!("{}:", server_key)));
19 let client = NakamaClient::with_interceptor(channel, move |mut req: Request<()>| {
20 req.metadata_mut()
21 .insert(
22 "authorization",
23 MetadataValue::from_str(auth_string.as_str()).unwrap(),
24 )
25 .map(|map| println!("{:?}", map));
26 Ok(req)
27 });
28 Ok(client)
29 }
30
31 pub async fn connect_with_session_token(
32 address: Option<&'static str>,
33 session: &Session,
34 ) -> Result<NakamaClient<Channel>, tonic::transport::Error> {
35 let address = address.unwrap_or("https://[::1]:7349");
36 let token = session.token.clone();
37 let channel = tonic::transport::Channel::from_static(address)
38 .connect()
39 .await?;
40 let auth_string = format!("Bearer {}", token);
41 let client = NakamaClient::with_interceptor(channel, move |mut req: Request<()>| {
42 req.metadata_mut()
43 .insert(
44 "authorization",
45 MetadataValue::from_str(auth_string.as_str()).unwrap(),
46 )
47 .map(|map| println!("{:?}", map));
48 Ok(req)
49 });
50 Ok(client)
51 }
52}
53
54#[cfg(test)]
55mod tests {
56 #[test]
57 fn it_works() {
58 assert_eq!(2 + 2, 4);
59 }
60}