From 29cb5c085d026b9595f935a6498b6f802cd97910 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Fri, 26 Jan 2024 10:44:30 +0100 Subject: [PATCH] httpd: Add `/nodes/:nid` and `/nodes/:nid/inventory` endpoints to get info on other nodes --- radicle-httpd/src/api/error.rs | 4 ++++ radicle-httpd/src/api/v1/node.rs | 28 +++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/radicle-httpd/src/api/error.rs b/radicle-httpd/src/api/error.rs index 9698ead3..90c7b4cd 100644 --- a/radicle-httpd/src/api/error.rs +++ b/radicle-httpd/src/api/error.rs @@ -46,6 +46,10 @@ pub enum Error { #[error(transparent)] Repository(#[from] radicle::storage::RepositoryError), + /// Routing error. + #[error(transparent)] + Routing(#[from] radicle::node::routing::Error), + /// Project doc error. #[error(transparent)] ProjectDoc(#[from] radicle::identity::doc::PayloadError), diff --git a/radicle-httpd/src/api/v1/node.rs b/radicle-httpd/src/api/v1/node.rs index 04a9e6de..0181868d 100644 --- a/radicle-httpd/src/api/v1/node.rs +++ b/radicle-httpd/src/api/v1/node.rs @@ -7,7 +7,8 @@ use hyper::StatusCode; use serde_json::json; use radicle::identity::RepoId; -use radicle::node::{policy, Handle, DEFAULT_TIMEOUT}; +use radicle::node::routing::Store; +use radicle::node::{policy, AliasStore, Handle, NodeId, DEFAULT_TIMEOUT}; use radicle::Node; use crate::api::error::Error; @@ -22,6 +23,8 @@ pub fn router(ctx: Context) -> Router { "/node/policies/repos/:rid", put(node_policies_seed_handler).delete(node_policies_unseed_handler), ) + .route("/nodes/:nid", get(nodes_handler)) + .route("/nodes/:nid/inventory", get(nodes_inventory_handler)) .with_state(ctx) } @@ -52,6 +55,29 @@ async fn node_handler(State(ctx): State) -> impl IntoResponse { Ok::<_, Error>(Json(response)) } +/// Return stored information about other nodes. +/// `GET /nodes/:nid` +async fn nodes_handler(State(ctx): State, Path(nid): Path) -> impl IntoResponse { + let aliases = ctx.profile.aliases(); + let response = json!({ + "alias": aliases.alias(&nid), + }); + + Ok::<_, Error>(Json(response)) +} + +/// Return stored information about other nodes. +/// `GET /nodes/:nid/inventory` +async fn nodes_inventory_handler( + State(ctx): State, + Path(nid): Path, +) -> impl IntoResponse { + let db = &ctx.profile.database()?; + let resources = db.get_resources(&nid)?; + + Ok::<_, Error>(Json(resources)) +} + /// Return local repo policies information. /// `GET /node/policies/repos` async fn node_policies_repos_handler(State(ctx): State) -> impl IntoResponse {