Schema endpoint
All checks were successful
CI / Typos (push) Successful in 19s
CI / Build and test (push) Successful in 1m46s
CI / Clippy (push) Successful in 3m24s
Docker / build-and-push (push) Successful in 4m8s
CI / Build and test (all features) (push) Successful in 7m9s

This commit is contained in:
2026-03-26 20:04:33 -07:00
parent fac300431a
commit 795d2ee825
4 changed files with 49 additions and 4 deletions

View File

@@ -10,7 +10,9 @@ use std::pin::Pin;
use thiserror::Error; use thiserror::Error;
use tracing::{trace, warn}; use tracing::{trace, warn};
pub use pile_dataset::serve::{ItemsResponse, LookupRequest, LookupResponse}; pub use pile_dataset::serve::{
FieldSpec, FieldsResponse, ItemsResponse, LookupRequest, LookupResponse,
};
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum ClientError { pub enum ClientError {
@@ -197,6 +199,14 @@ impl DatasetClient {
Ok(FieldResponse { content_type, data }) Ok(FieldResponse { content_type, data })
} }
/// `GET /schema` — retrieve this dataset's schema.
pub async fn schema(&self) -> Result<FieldsResponse, ClientError> {
let url = format!("{}/schema", self.base_url);
trace!(url, "GET /schema");
let resp = self.client.get(url).send().await?;
check_status(resp).await?.json().await.map_err(Into::into)
}
/// `GET /items` — paginate over all items in this dataset, ordered by (source, key). /// `GET /items` — paginate over all items in this dataset, ordered by (source, key).
pub async fn list_items( pub async fn list_items(
&self, &self,

View File

@@ -1,4 +1,4 @@
use serde::Deserialize; use serde::{Deserialize, Serialize};
use std::{collections::HashMap, fmt::Debug, path::PathBuf}; use std::{collections::HashMap, fmt::Debug, path::PathBuf};
use crate::{objectpath::ObjectPath, pattern::GroupPattern}; use crate::{objectpath::ObjectPath, pattern::GroupPattern};
@@ -61,7 +61,7 @@ pub enum Source {
// MARK: schema // MARK: schema
// //
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize, Serialize)]
pub struct FieldSpec { pub struct FieldSpec {
/// How to find this field in a data entry /// How to find this field in a data entry
pub path: Vec<ObjectPath>, pub path: Vec<ObjectPath>,

View File

@@ -23,10 +23,13 @@ pub use field::*;
mod items; mod items;
pub use items::*; pub use items::*;
mod schema;
pub use schema::*;
#[derive(OpenApi)] #[derive(OpenApi)]
#[openapi( #[openapi(
tags(), tags(),
paths(lookup, item_get, get_extract, items_list, get_field), paths(lookup, item_get, get_extract, items_list, get_field, get_schema),
components(schemas( components(schemas(
LookupRequest, LookupRequest,
LookupResponse, LookupResponse,
@@ -55,6 +58,7 @@ impl Datasets {
.route("/extract", get(get_extract)) .route("/extract", get(get_extract))
.route("/field", get(get_field)) .route("/field", get(get_field))
.route("/items", get(items_list)) .route("/items", get(items_list))
.route("/get_schema", get(get_schema))
.with_state(self.clone()); .with_state(self.clone());
if let Some(prefix) = prefix { if let Some(prefix) = prefix {

View File

@@ -0,0 +1,31 @@
use axum::{
Json,
extract::State,
http::StatusCode,
response::{IntoResponse, Response},
};
use std::{collections::HashMap, sync::Arc};
pub use pile_config::FieldSpec;
use crate::Datasets;
pub type FieldsResponse = HashMap<String, FieldSpec>;
/// Retrieve this dataset's schema.
#[utoipa::path(
get,
path = "/schema",
responses(
(status = 200, description = "This dataset's schema"),
)
)]
pub async fn get_schema(State(state): State<Arc<Datasets>>) -> Response {
let fields: FieldsResponse = state
.config
.schema
.iter()
.map(|(k, v)| (k.as_str().to_owned(), v.clone()))
.collect();
(StatusCode::OK, Json(fields)).into_response()
}