Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ blake2 = "0.10"
bs58 = "0.5"
caryatid_sdk = "0.12"
caryatid_module_clock = "0.12"
caryatid_module_rest_server = "0.13"
caryatid_module_rest_server = "0.14"
chrono = "0.4"
gcd = "2.3"
fraction = "0.15"
Expand Down
33 changes: 31 additions & 2 deletions common/src/rest_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use anyhow::{anyhow, Result};
use caryatid_sdk::Context;
use futures::future::Future;
use num_traits::ToPrimitive;
use std::sync::Arc;
use std::{collections::HashMap, sync::Arc};
use tokio::task::JoinHandle;
use tracing::{error, info};

Expand Down Expand Up @@ -44,7 +44,7 @@ where
}

/// Handle a REST request with path parameters
pub fn handle_rest_with_parameter<F, Fut>(
pub fn handle_rest_with_path_parameter<F, Fut>(
context: Arc<Context<Message>>,
topic: &str,
handler: F,
Expand Down Expand Up @@ -83,6 +83,35 @@ where
})
}

// Handle a REST request with query parameters
pub fn handle_rest_with_query_parameter<F, Fut>(
context: Arc<Context<Message>>,
topic: &str,
handler: F,
) -> JoinHandle<()>
where
F: Fn(HashMap<String, String>) -> Fut + Send + Sync + Clone + 'static,
Fut: Future<Output = Result<RESTResponse>> + Send + 'static,
{
context.handle(topic, move |message: Arc<Message>| {
let handler = handler.clone();
async move {
let response = match message.as_ref() {
Message::RESTRequest(request) => {
let params = request.query_parameters.clone();
match handler(params).await {
Ok(response) => response,
Err(error) => RESTResponse::with_text(500, &format!("{error:?}")),
}
}
_ => RESTResponse::with_text(500, "Unexpected message in REST request"),
};

Arc::new(Message::RESTResponse(response))
}
})
}

/// Extract parameters from the request path based on the topic pattern.
/// Skips the first 3 parts of the topic as these are never parameters
fn extract_params_from_topic_and_path(topic: &str, path_elements: &[String]) -> Vec<String> {
Expand Down
2 changes: 1 addition & 1 deletion common/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ pub struct StakeDelegation {
}

/// SPO total delegation data (for SPDD)
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize)]
pub struct DelegatedStake {
/// Active stake - UTXO values only (used for reward calcs)
pub active: Lovelace,
Expand Down
Loading