refactor: use struct to represent query so we can doc it via aide

This commit is contained in:
Ning Sun
2022-10-28 10:12:39 +08:00
parent 22277bbfab
commit 5daafb26ee
3 changed files with 31 additions and 21 deletions

View File

@@ -262,15 +262,12 @@ impl HttpServer {
let sql_router = ApiRouter::with_state(self.sql_handler.clone())
.api_route(
"/sql",
apirouting::get_with(handler::sql, handler::sql_docs),
)
.api_route(
"/sql",
apirouting::post_with(handler::sql, handler::sql_docs),
apirouting::get_with(handler::sql, handler::sql_docs)
.post_with(handler::sql, handler::sql_docs),
)
.api_route("/scripts", apirouting::post(handler::scripts))
.api_route("/run-script", apirouting::post(handler::run_script))
.route("/api.json", apirouting::get(serve_api))
.route("/private/api.json", apirouting::get(serve_api))
.finish_api(&mut api)
.layer(Extension(api));

View File

@@ -9,13 +9,19 @@ use serde::{Deserialize, Serialize};
use crate::http::JsonResponse;
use crate::query_handler::SqlQueryHandlerRef;
#[derive(Debug, Default, Serialize, Deserialize, JsonSchema)]
pub struct SqlQuery {
pub database: Option<String>,
pub sql: Option<String>,
}
/// Handler to execute sql
#[axum_macros::debug_handler]
pub async fn sql(
State(sql_handler): State<SqlQueryHandlerRef>,
Query(params): Query<HashMap<String, String>>,
Query(params): Query<SqlQuery>,
) -> Json<JsonResponse> {
if let Some(sql) = params.get("sql") {
if let Some(ref sql) = params.sql {
Json(JsonResponse::from_output(sql_handler.do_query(sql).await).await)
} else {
Json(JsonResponse::with_error(Some(
@@ -25,8 +31,7 @@ pub async fn sql(
}
pub(crate) fn sql_docs(op: TransformOperation) -> TransformOperation {
op.id("sql")
.description("Execute SQL query provided by `sql` parameter")
op.description("Execute SQL query provided by `sql` parameter")
.response::<200, Json<JsonResponse>>()
}
@@ -40,7 +45,7 @@ pub async fn metrics(Query(_params): Query<HashMap<String, String>>) -> String {
}
}
#[derive(Deserialize, Serialize, JsonSchema)]
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
pub struct ScriptExecution {
pub name: String,
pub script: String,
@@ -69,13 +74,18 @@ pub async fn scripts(
Json(body)
}
#[derive(Debug, Serialize, Deserialize, JsonSchema)]
pub struct RunScriptQuery {
name: Option<String>,
}
/// Handler to execute script
#[axum_macros::debug_handler]
pub async fn run_script(
State(query_handler): State<SqlQueryHandlerRef>,
Query(params): Query<HashMap<String, String>>,
Query(params): Query<RunScriptQuery>,
) -> Json<JsonResponse> {
let name = params.get("name");
let name = params.name.as_ref();
if name.is_none() || name.unwrap().is_empty() {
return Json(JsonResponse::with_error(Some("Invalid name".to_string())));

View File

@@ -13,7 +13,11 @@ use crate::create_testing_sql_query_handler;
#[tokio::test]
async fn test_sql_not_provided() {
let query_handler = create_testing_sql_query_handler(MemTable::default_numbers_table());
let Json(json) = http_handler::sql(State(query_handler), Query(HashMap::default())).await;
let Json(json) = http_handler::sql(
State(query_handler),
Query(http_handler::SqlQuery::default()),
)
.await;
assert!(!json.success());
assert_eq!(
Some(&"sql parameter is required.".to_string()),
@@ -46,8 +50,7 @@ async fn test_metrics() {
counter!("test_metrics", 1);
let query = create_query();
let text = http_handler::metrics(query).await;
let text = http_handler::metrics(Query(HashMap::default())).await;
assert!(text.contains("test_metrics counter"));
}
@@ -76,9 +79,9 @@ def test(n):
})
}
fn create_query() -> Query<HashMap<String, String>> {
Query(HashMap::from([(
"sql".to_string(),
"select sum(uint32s) from numbers limit 20".to_string(),
)]))
fn create_query() -> Query<http_handler::SqlQuery> {
Query(http_handler::SqlQuery {
sql: Some("select sum(uint32s) from numbers limit 20".to_string()),
database: None,
})
}