From 0beee744b286cb8a7363376ec80c1600a9ac9596 Mon Sep 17 00:00:00 2001 From: Arnab Chakraborty <11457760+Rocky43007@users.noreply.github.com> Date: Wed, 7 Aug 2024 01:17:38 +0300 Subject: [PATCH] Completed `cloud.devices` rspc route Everything is done except "hello", "register", and "update", which are being handled by Ericson --- core/src/api/cloud.rs | 147 +++++++++++++++++- core/src/api/utils/mod.rs | 27 +++- core/src/node/config.rs | 2 +- .../settings/client/account/index.tsx | 1 - 4 files changed, 162 insertions(+), 15 deletions(-) diff --git a/core/src/api/cloud.rs b/core/src/api/cloud.rs index f8a781d3c..04f8c1e5f 100644 --- a/core/src/api/cloud.rs +++ b/core/src/api/cloud.rs @@ -7,6 +7,7 @@ use serde::de::DeserializeOwned; use uuid::Uuid; use super::{utils::library, Ctx, R}; +use crate::{api::utils::get_access_token, util::MaybeUndefined}; #[allow(unused)] async fn parse_json_body(response: Response) -> Result { @@ -22,6 +23,7 @@ pub(crate) fn mount() -> AlphaRouter { R.router() .merge("library.", library::mount()) .merge("locations.", locations::mount()) + .merge("devices.", devices::mount()) .procedure("getApiOrigin", { R.query(|node, _: ()| async move { Ok(node.env.api_url.lock().await.to_string()) }) }) @@ -47,9 +49,7 @@ mod library { use std::str::FromStr; use sd_p2p::RemoteIdentity; - use tracing::debug; - - use crate::{api::utils::get_access_token, util::MaybeUndefined}; + use tracing::{debug, error}; use super::*; @@ -70,7 +70,7 @@ mod library { let client = match node.cloud_services.client().await { Ok(client) => client, Err(e) => { - tracing::error!(?e, "Failed to get cloud services client"); + error!(?e, "Failed to get cloud services client"); return Err(rspc::Error::new( rspc::ErrorCode::InternalServerError, "Failed to get cloud services client".to_string(), @@ -80,7 +80,7 @@ mod library { let token = match get_access_token() { Ok(token) => token, Err(e) => { - tracing::error!(?e, "Failed to get access token"); + error!(?e, "Failed to get access token"); return Err(rspc::Error::new( rspc::ErrorCode::InternalServerError, "Failed to get access token".to_string(), @@ -97,7 +97,7 @@ mod library { Ok(res) => match res { Ok(res) => res, Err(e) => { - tracing::error!(?e, "Failed to list devices"); + error!(?e, "Failed to list devices"); return Err(rspc::Error::new( rspc::ErrorCode::InternalServerError, "Failed to list devices".to_string(), @@ -105,7 +105,7 @@ mod library { } }, Err(e) => { - tracing::error!(?e, "Quinn error: "); + error!(?e, "Quinn error: "); return Err(rspc::Error::new( rspc::ErrorCode::InternalServerError, "Failed to list devices -> Quin RPC Layer Error Detected." @@ -370,3 +370,136 @@ mod locations { }) } } + +mod devices { + use tracing::{debug, error}; + + use super::*; + + pub fn mount() -> AlphaRouter { + R.router() + .procedure("get", { + R.query(|node, device_id: Uuid| async move { + let client = node.cloud_services.client().await.map_err(|e| { + error!(?e, "Failed to get cloud services client;"); + e + })?; + + let token = match get_access_token() { + Ok(token) => token, + Err(e) => { + return Err(e); + } + }; + + let device = match client + .devices() + .get(sd_cloud_schema::devices::get::Request { + pub_id: sd_cloud_schema::devices::PubId(device_id), + access_token: sd_cloud_schema::auth::AccessToken(token), + }) + .await + { + Ok(device) => match device { + Ok(sd_cloud_schema::devices::get::Response(device)) => device, + Err(e) => { + error!(?e, "Failed to get device"); + return Err(e.into()); + } + }, + Err(e) => { + error!(?e, "Failed to get device"); + return Err(rspc::Error::new( + rspc::ErrorCode::InternalServerError, + "Failed to get device".to_string(), + )); + } + }; + + debug!(?device, "Got device"); + + Ok(device) + }) + }) + .procedure("list", { + R.query(|node, _: ()| async move { + let client = node.cloud_services.client().await.map_err(|e| { + error!(?e, "Failed to get cloud services client;"); + e + })?; + + let token = match get_access_token() { + Ok(token) => token, + Err(e) => { + return Err(e); + } + }; + + let devices = match client + .devices() + .list(sd_cloud_schema::devices::list::Request { + access_token: sd_cloud_schema::auth::AccessToken(token), + }) + .await + { + Ok(devices) => match devices { + Ok(sd_cloud_schema::devices::list::Response(devices)) => devices, + Err(e) => { + tracing::error!(?e, "Failed to list devices"); + return Err(e.into()); + } + }, + Err(e) => { + tracing::error!(?e, "Quinn error: "); + return Err(rspc::Error::new( + rspc::ErrorCode::InternalServerError, + "Failed to list devices -> Quin RPC Layer Error Detected." + .to_string(), + )); + } + }; + + debug!(?devices, "Listed devices"); + + Ok(devices) + }) + }) + .procedure("delete", { + R.query(|node, device_id: Uuid| async move { + let client = node.cloud_services.client().await.map_err(|e| { + error!(?e, "Failed to get cloud services client;"); + e + })?; + + let token = match get_access_token() { + Ok(token) => token, + Err(e) => { + return Err(e); + } + }; + + let _ = client + .devices() + .delete(sd_cloud_schema::devices::delete::Request { + pub_id: sd_cloud_schema::devices::PubId(device_id), + access_token: sd_cloud_schema::auth::AccessToken(token), + }) + .await + .map_err(|e| { + error!(?e, "Failed to delete device"); + rspc::Error::new( + rspc::ErrorCode::InternalServerError, + "Failed to delete device".to_string(), + ) + }); + + debug!("Deleted device"); + + Ok(()) + }) + }) + .procedure("register", { R.query(|node, _: ()| async move { Ok(()) }) }) + .procedure("hello", { R.query(|node, _: ()| async move { Ok(()) }) }) + .procedure("update", { R.query(|node, _: ()| async move { Ok(()) }) }) + } +} diff --git a/core/src/api/utils/mod.rs b/core/src/api/utils/mod.rs index a99d8f3fa..f7bf924a3 100644 --- a/core/src/api/utils/mod.rs +++ b/core/src/api/utils/mod.rs @@ -56,7 +56,10 @@ pub fn get_access_token() -> Result { Ok(key) => key, Err(e) => { error!("Error retrieving key: {}. Does the key exist yet?", e); - return Ok("".to_string()); + return Err(rspc::Error::new( + rspc::ErrorCode::InternalServerError, + "Error retrieving key".to_string(), + )); } }; @@ -64,7 +67,10 @@ pub fn get_access_token() -> Result { Ok(re) => re, Err(e) => { error!("Error creating regex: {}", e); - return Ok("".to_string()); + return Err(rspc::Error::new( + rspc::ErrorCode::InternalServerError, + "Error creating regex".to_string(), + )); } }; @@ -73,14 +79,23 @@ pub fn get_access_token() -> Result { Some(token) => token.as_str(), None => { error!("Error parsing Cookie String value: {}", "No token found"); - return Ok("".to_string()); + return Err(rspc::Error::new( + rspc::ErrorCode::InternalServerError, + "Error parsing Cookie String value".to_string(), + )); } }, None => { - error!("Error parsing Cookie String value: {}", "No token cookie string found"); - return Ok("".to_string()); + error!( + "Error parsing Cookie String value: {}", + "No token cookie string found" + ); + return Err(rspc::Error::new( + rspc::ErrorCode::InternalServerError, + "Error parsing Cookie String value".to_string(), + )); } }; Ok(token.to_string()) -} +} \ No newline at end of file diff --git a/core/src/node/config.rs b/core/src/node/config.rs index ecc2543f0..565146ab7 100644 --- a/core/src/node/config.rs +++ b/core/src/node/config.rs @@ -140,7 +140,7 @@ pub struct NodeConfig { // Model version for the image labeler pub image_labeler_version: Option, // Operating System of the node -> "linux", "macos", "windows", "android", "ios" - os: String, + pub os: String, version: NodeConfigVersion, } diff --git a/interface/app/$libraryId/settings/client/account/index.tsx b/interface/app/$libraryId/settings/client/account/index.tsx index 16ce25574..0985029b2 100644 --- a/interface/app/$libraryId/settings/client/account/index.tsx +++ b/interface/app/$libraryId/settings/client/account/index.tsx @@ -19,7 +19,6 @@ export const Component = () => { const { t } = useLocale(); const [userInfo, setUserInfo] = useState(null); const me = useBridgeQuery(['auth.me'], { retry: false }); - const token = useBridgeQuery(['keys.getAccessToken'], { retry: false }); const authStore = auth.useStateSnapshot(); useEffect(() => { async function _() {