Skip to content

Commit

Permalink
feat(admin-api): refactor context data endpoints (#375)
Browse files Browse the repository at this point in the history
  • Loading branch information
frdomovic authored Jun 14, 2024
1 parent 8980d65 commit 354e851
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 32 deletions.
8 changes: 8 additions & 0 deletions crates/primitives/src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct RootKey {
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ClientKey {
#[serde(rename = "wallet")]
pub wallet_type: WalletType,
Expand All @@ -25,6 +26,13 @@ pub struct ClientKey {
pub context_id: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ContextUser {
pub user_id: String,
pub joined_at: u64,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Context {
Expand Down
95 changes: 64 additions & 31 deletions crates/server/src/admin/handlers/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::Arc;
use axum::extract::Path;
use axum::response::IntoResponse;
use axum::{Extension, Json};
use calimero_primitives::identity::{ClientKey, Context};
use calimero_primitives::identity::{ClientKey, Context, ContextUser};
use calimero_server_primitives::admin::ContextStorage;
use rand::RngCore;
use reqwest::StatusCode;
Expand All @@ -14,17 +14,9 @@ use crate::admin::service::{parse_api_error, AdminState, ApiError, ApiResponse};
use crate::admin::storage::client_keys::get_context_client_key;
use crate::admin::storage::context::{add_context, delete_context, get_context, get_contexts};

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ContextData {
context: Context,
client_keys: Vec<ClientKey>,
users: Vec<String>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct GetContextResponse {
data: ContextData,
data: Context,
}

pub async fn get_context_handler(
Expand All @@ -36,24 +28,10 @@ pub async fn get_context_handler(

match context_result {
Ok(ctx) => match ctx {
Some(context) => {
let client_keys_result =
get_context_client_key(&state.store, &context.application_id)
.map_err(|err| parse_api_error(err).into_response());
match client_keys_result {
Ok(client_keys) => ApiResponse {
payload: GetContextResponse {
data: ContextData {
context,
client_keys,
users: vec![],
},
},
}
.into_response(),
Err(err) => err.into_response(),
}
Some(context) => ApiResponse {
payload: GetContextResponse { data: context },
}
.into_response(),
None => ApiError {
status_code: StatusCode::NOT_FOUND,
message: "Context not found".into(),
Expand All @@ -64,6 +42,59 @@ pub async fn get_context_handler(
}
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ClientKeys {
client_keys: Vec<ClientKey>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct GetContextClientKeysResponse {
data: ClientKeys,
}

pub async fn get_context_client_keys_handler(
Path(context_id): Path<String>,
Extension(state): Extension<Arc<AdminState>>,
) -> impl IntoResponse {
let client_keys_result = get_context_client_key(&state.store, &context_id)
.map_err(|err| parse_api_error(err).into_response());
match client_keys_result {
Ok(client_keys) => ApiResponse {
payload: GetContextClientKeysResponse {
data: ClientKeys { client_keys },
},
}
.into_response(),
Err(err) => err.into_response(),
}
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct ContextUsers {
context_users: Vec<ContextUser>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct GetContextUsersResponse {
data: ContextUsers,
}

pub async fn get_context_users_handler(
Path(context_id): Path<String>,
Extension(state): Extension<Arc<AdminState>>,
) -> impl IntoResponse {
ApiResponse {
payload: GetContextUsersResponse {
data: ContextUsers {
context_users: vec![],
},
},
}
.into_response()
}

#[derive(Debug, Serialize, Deserialize)]
pub struct GetContextsResponse {
data: Vec<Context>,
Expand Down Expand Up @@ -149,8 +180,10 @@ pub async fn get_context_storage_handler(
Path(_context_id): Path<String>,
Extension(_state): Extension<Arc<AdminState>>,
) -> impl IntoResponse {
ApiResponse {
payload: GetContextStorageResponse { data: ContextStorage { size_in_bytes: 0}},
}
.into_response()
ApiResponse {
payload: GetContextStorageResponse {
data: ContextStorage { size_in_bytes: 0 },
},
}
.into_response()
}
4 changes: 3 additions & 1 deletion crates/server/src/admin/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use super::handlers::add_client_key::add_client_key_handler;
use super::handlers::challenge::request_challenge_handler;
use super::handlers::context::{
create_context_handler, delete_context_handler, get_context_handler, get_contexts_handler,
get_context_storage_handler,
get_context_storage_handler, get_context_client_keys_handler, get_context_users_handler
};
use super::handlers::fetch_did::fetch_did_handler;
use super::handlers::root_keys::create_root_key_handler;
Expand Down Expand Up @@ -72,6 +72,8 @@ pub(crate) fn setup(
.route("/contexts", post(create_context_handler))
.route("/contexts/:context_id", delete(delete_context_handler))
.route("/contexts/:context_id", get(get_context_handler))
.route("/contexts/:context_id/users", get(get_context_users_handler))
.route("/contexts/:context_id/client-keys", get(get_context_client_keys_handler))
.route("/contexts/:context_id/storage", get(get_context_storage_handler))
.route("/contexts", get(get_contexts_handler))
.layer(Extension(shared_state))
Expand Down

0 comments on commit 354e851

Please sign in to comment.