2023-08-20 00:46:55 +02:00
|
|
|
//! ### client
|
2024-01-25 02:13:45 +01:00
|
|
|
//!
|
2023-08-20 00:46:55 +02:00
|
|
|
//! codemp client manager, containing grpc services
|
|
|
|
|
2024-08-08 03:59:28 +02:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2024-02-07 01:12:05 +01:00
|
|
|
use dashmap::DashMap;
|
2024-08-22 00:57:24 +02:00
|
|
|
use tonic::{service::interceptor::InterceptedService, transport::{Channel, Endpoint}};
|
2024-01-25 02:13:45 +01:00
|
|
|
|
2024-08-22 00:57:24 +02:00
|
|
|
use crate::{api::User, ext::InternallyMutable, workspace::Workspace};
|
|
|
|
use codemp_proto::{
|
|
|
|
auth::{auth_client::AuthClient, LoginRequest},
|
|
|
|
common::{Empty, Token}, session::{session_client::SessionClient, InviteRequest, WorkspaceRequest},
|
|
|
|
};
|
2024-08-08 23:58:45 +02:00
|
|
|
|
|
|
|
#[cfg(feature = "python")]
|
|
|
|
use pyo3::prelude::*;
|
2024-08-08 02:30:34 +02:00
|
|
|
|
2023-08-20 00:46:55 +02:00
|
|
|
/// codemp client manager
|
|
|
|
///
|
|
|
|
/// contains all required grpc services and the unique user id
|
|
|
|
/// will disconnect when dropped
|
|
|
|
/// can be used to interact with server
|
2024-08-08 03:59:28 +02:00
|
|
|
#[derive(Debug, Clone)]
|
2024-08-08 19:54:47 +02:00
|
|
|
#[cfg_attr(feature = "js", napi_derive::napi)]
|
2024-08-08 23:58:45 +02:00
|
|
|
#[cfg_attr(feature = "python", pyclass)]
|
2024-08-08 03:59:28 +02:00
|
|
|
pub struct Client(Arc<ClientInner>);
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct ClientInner {
|
2024-08-22 00:57:24 +02:00
|
|
|
user: User,
|
2024-08-08 02:30:34 +02:00
|
|
|
host: String,
|
2024-08-08 00:25:22 +02:00
|
|
|
workspaces: DashMap<String, Workspace>,
|
2024-08-22 00:57:24 +02:00
|
|
|
auth: AuthClient<Channel>,
|
|
|
|
session: SessionClient<InterceptedService<Channel, SessionInterceptor>>,
|
|
|
|
claims: InternallyMutable<Token>,
|
2024-01-25 02:13:45 +01:00
|
|
|
}
|
|
|
|
|
2023-08-17 02:58:55 +02:00
|
|
|
impl Client {
|
2023-08-20 00:46:55 +02:00
|
|
|
/// instantiate and connect a new client
|
2024-08-22 00:57:24 +02:00
|
|
|
pub async fn connect(
|
2024-08-08 02:30:34 +02:00
|
|
|
host: impl AsRef<str>,
|
|
|
|
username: impl AsRef<str>,
|
2024-08-08 23:58:45 +02:00
|
|
|
password: impl AsRef<str>,
|
2024-08-08 02:30:34 +02:00
|
|
|
) -> crate::Result<Self> {
|
|
|
|
let host = if host.as_ref().starts_with("http") {
|
|
|
|
host.as_ref().to_string()
|
|
|
|
} else {
|
|
|
|
format!("https://{}", host.as_ref())
|
|
|
|
};
|
2024-01-25 02:13:45 +01:00
|
|
|
|
2024-08-22 00:57:24 +02:00
|
|
|
let channel = Endpoint::from_shared(host.clone())?.connect().await?;
|
|
|
|
let mut auth = AuthClient::new(channel.clone());
|
|
|
|
|
|
|
|
let resp = auth.login(LoginRequest {
|
|
|
|
username: username.as_ref().to_string(),
|
|
|
|
password: password.as_ref().to_string(),
|
|
|
|
})
|
|
|
|
.await?
|
|
|
|
.into_inner();
|
|
|
|
|
|
|
|
let claims = InternallyMutable::new(resp.token);
|
|
|
|
|
|
|
|
let session = SessionClient::with_interceptor(
|
|
|
|
channel, SessionInterceptor(claims.channel())
|
|
|
|
);
|
2023-08-19 04:02:21 +02:00
|
|
|
|
2024-08-08 03:59:28 +02:00
|
|
|
Ok(Client(Arc::new(ClientInner {
|
2024-08-08 02:30:34 +02:00
|
|
|
host,
|
2024-08-22 00:57:24 +02:00
|
|
|
user: resp.user.into(),
|
2024-08-08 00:25:22 +02:00
|
|
|
workspaces: DashMap::default(),
|
2024-08-22 00:57:24 +02:00
|
|
|
claims,
|
|
|
|
auth, session,
|
2024-08-08 03:59:28 +02:00
|
|
|
})))
|
2023-08-16 23:09:47 +02:00
|
|
|
}
|
|
|
|
|
2024-08-22 00:57:24 +02:00
|
|
|
/// refresh session token
|
|
|
|
pub async fn refresh(&self) -> crate::Result<()> {
|
|
|
|
let new_token = self.0.auth.clone().refresh(self.0.claims.get())
|
|
|
|
.await?
|
|
|
|
.into_inner();
|
|
|
|
self.0.claims.set(new_token);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// attempts to create a new workspace with given name
|
|
|
|
pub async fn create_workspace(&self, name: impl AsRef<str>) -> crate::Result<()> {
|
|
|
|
self.0.session
|
|
|
|
.clone()
|
|
|
|
.create_workspace(WorkspaceRequest { workspace: name.as_ref().to_string() })
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// delete an existing workspace if possible
|
|
|
|
pub async fn delete_workspace(&self, name: impl AsRef<str>) -> crate::Result<()> {
|
|
|
|
self.0.session
|
|
|
|
.clone()
|
|
|
|
.delete_workspace(WorkspaceRequest { workspace: name.as_ref().to_string() })
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// invite user associated with username to workspace, if possible
|
|
|
|
pub async fn invite_to_workspace(&self, workspace_name: impl AsRef<str>, user_name: impl AsRef<str>) -> crate::Result<()> {
|
|
|
|
self.0.session
|
|
|
|
.clone()
|
|
|
|
.invite_to_workspace(InviteRequest {
|
|
|
|
workspace: workspace_name.as_ref().to_string(),
|
|
|
|
user: user_name.as_ref().to_string(),
|
|
|
|
})
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// list all available workspaces, filtering between those owned and those invited to
|
|
|
|
pub async fn list_workspaces(&self, owned: bool, invited: bool) -> crate::Result<Vec<String>> {
|
|
|
|
let mut workspaces = self.0.session
|
|
|
|
.clone()
|
|
|
|
.list_workspaces(Empty {})
|
|
|
|
.await?
|
|
|
|
.into_inner();
|
|
|
|
|
|
|
|
let mut out = Vec::new();
|
|
|
|
|
|
|
|
if owned { out.append(&mut workspaces.owned) }
|
|
|
|
if invited { out.append(&mut workspaces.invited) }
|
|
|
|
|
|
|
|
Ok(out)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// join a workspace, returns [Workspace]
|
2024-08-08 02:30:34 +02:00
|
|
|
pub async fn join_workspace(&self, workspace: impl AsRef<str>) -> crate::Result<Workspace> {
|
2024-08-22 00:57:24 +02:00
|
|
|
let token = self.0.session
|
|
|
|
.clone()
|
|
|
|
.access_workspace(WorkspaceRequest { workspace: workspace.as_ref().to_string() })
|
|
|
|
.await?
|
|
|
|
.into_inner();
|
2023-08-16 23:09:47 +02:00
|
|
|
|
2024-08-08 02:30:34 +02:00
|
|
|
let ws = Workspace::try_new(
|
|
|
|
workspace.as_ref().to_string(),
|
2024-08-22 00:57:24 +02:00
|
|
|
self.0.user.clone(),
|
2024-08-08 03:59:28 +02:00
|
|
|
&self.0.host,
|
2024-08-22 00:57:24 +02:00
|
|
|
token,
|
|
|
|
self.0.claims.channel(),
|
2024-08-08 23:58:45 +02:00
|
|
|
)
|
|
|
|
.await?;
|
2024-01-25 16:31:38 +01:00
|
|
|
|
2024-08-08 23:58:45 +02:00
|
|
|
self.0
|
|
|
|
.workspaces
|
|
|
|
.insert(workspace.as_ref().to_string(), ws.clone());
|
2024-02-07 01:12:05 +01:00
|
|
|
|
|
|
|
Ok(ws)
|
2023-08-16 23:09:47 +02:00
|
|
|
}
|
2024-01-25 03:25:45 +01:00
|
|
|
|
2024-08-08 00:28:35 +02:00
|
|
|
/// leaves a [Workspace] by name
|
|
|
|
pub fn leave_workspace(&self, id: &str) -> bool {
|
2024-08-08 03:59:28 +02:00
|
|
|
self.0.workspaces.remove(id).is_some()
|
2024-08-08 00:28:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// gets a [Workspace] by name
|
2024-08-05 19:13:48 +02:00
|
|
|
pub fn get_workspace(&self, id: &str) -> Option<Workspace> {
|
2024-08-08 03:59:28 +02:00
|
|
|
self.0.workspaces.get(id).map(|x| x.clone())
|
2024-02-07 03:47:37 +01:00
|
|
|
}
|
|
|
|
|
2024-08-08 04:14:13 +02:00
|
|
|
/// get name of all active [Workspace]s
|
|
|
|
pub fn active_workspaces(&self) -> Vec<String> {
|
2024-08-08 23:58:45 +02:00
|
|
|
self.0
|
|
|
|
.workspaces
|
|
|
|
.iter()
|
|
|
|
.map(|x| x.key().to_string())
|
|
|
|
.collect()
|
2024-08-08 04:14:13 +02:00
|
|
|
}
|
|
|
|
|
2024-01-25 16:31:38 +01:00
|
|
|
/// accessor for user id
|
2024-08-22 00:57:24 +02:00
|
|
|
pub fn user(&self) -> &User {
|
|
|
|
&self.0.user
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct SessionInterceptor(tokio::sync::watch::Receiver<codemp_proto::common::Token>);
|
|
|
|
impl tonic::service::Interceptor for SessionInterceptor {
|
|
|
|
fn call(
|
|
|
|
&mut self,
|
|
|
|
mut request: tonic::Request<()>,
|
|
|
|
) -> Result<tonic::Request<()>, tonic::Status> {
|
|
|
|
if let Ok(token) = self.0.borrow().token.parse() {
|
|
|
|
request.metadata_mut().insert("session", token);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(request)
|
2024-01-25 03:25:45 +01:00
|
|
|
}
|
2023-08-16 23:09:47 +02:00
|
|
|
}
|