Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API schema endpoints #46

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
46 changes: 45 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ ndarray-stats = "0.5"
num-traits = "0.2.15"
serde = { version = "1.0", features = ["derive"] }
serde_json = "*"
schemars = { version = "0.8.11", features = ["url", "bytes"] }
strum_macros = "0.24"
thiserror = "1.0"
tokio = { version = "1.28", features = ["full"] }
Expand Down
25 changes: 22 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use axum::{
Router, TypedHeader,
};

use schemars::schema_for;
use tower::Layer;
use tower::ServiceBuilder;
use tower_http::normalize_path::NormalizePathLayer;
Expand Down Expand Up @@ -82,7 +83,14 @@ fn router() -> Router {
}

Router::new()
.route("/.well-known/s3-active-storage-schema", get(schema))
.route(
"/.well-known/s3-active-storage-request-schema",
get(request_schema),
)
.route(
"/.well-known/s3-active-storage-response-schema",
get(response_schema),
)
.nest("/v1", v1())
}

Expand Down Expand Up @@ -110,8 +118,19 @@ pub fn service() -> Service {
}

/// TODO: Return an OpenAPI schema
async fn schema() -> &'static str {
"Hello, world!"
async fn request_schema() -> Result<String, StatusCode> {
let result = serde_json::to_string_pretty(&schema_for!(models::RequestData));
match result {
Ok(json_schema) => Ok(json_schema),
Err(_) => Err(StatusCode::INTERNAL_SERVER_ERROR),
}
}
async fn response_schema() -> Result<String, StatusCode> {
let result = serde_json::to_string_pretty(&schema_for!(models::Response));
match result {
Ok(json_schema) => Ok(json_schema),
Err(_) => Err(StatusCode::INTERNAL_SERVER_ERROR),
}
}

/// Download an object from S3
Expand Down
14 changes: 8 additions & 6 deletions src/models.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
//! Data types and associated functions and methods

use axum::body::Bytes;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use strum_macros::Display;
use url::Url;
use validator::{Validate, ValidationError};

/// Supported numerical data types
#[derive(Clone, Copy, Debug, Deserialize, Display, PartialEq)]
#[derive(Clone, Copy, Debug, Deserialize, Display, PartialEq, JsonSchema)]
#[serde(rename_all = "lowercase")]
pub enum DType {
/// [i32]
Expand Down Expand Up @@ -41,7 +42,7 @@ impl DType {
/// Array ordering
///
/// Defines an ordering for multi-dimensional arrays.
#[derive(Debug, Deserialize, PartialEq)]
#[derive(Debug, Deserialize, PartialEq, JsonSchema)]
pub enum Order {
/// Row-major (C) ordering
C,
Expand All @@ -51,7 +52,7 @@ pub enum Order {

/// A slice of a single dimension of an array
///
/// The API uses NumPy slice semantics:
/// The API uses NumPy slice (i.e. [start, end, stride]) semantics where:
///
/// When start or end is negative:
/// * positive_start = start + length
Expand All @@ -65,7 +66,7 @@ pub enum Order {
/// * positive_end <= i < positive_start
// NOTE: In serde, structs can be deserialised from sequences or maps. This allows us to support
// the [<start>, <end>, <stride>] API, with the convenience of named fields.
#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize, Validate)]
#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize, Validate, JsonSchema)]
#[serde(deny_unknown_fields)]
#[validate(schema(function = "validate_slice"))]
pub struct Slice {
Expand All @@ -86,7 +87,7 @@ impl Slice {
}

/// Request data for operations
#[derive(Debug, Deserialize, PartialEq, Validate)]
#[derive(Debug, Deserialize, PartialEq, Validate, JsonSchema)]
#[serde(deny_unknown_fields)]
#[validate(schema(function = "validate_request_data"))]
pub struct RequestData {
Expand Down Expand Up @@ -179,8 +180,9 @@ fn validate_request_data(request_data: &RequestData) -> Result<(), ValidationErr
}

/// Response containing the result of a computation and associated metadata.
#[derive(JsonSchema)]
pub struct Response {
/// Response data. May be a scalar or multi-dimensional array.
/// Raw response data as bytes. May represent a scalar or multi-dimensional array.
pub body: Bytes,
/// Data type of the response
pub dtype: DType,
Expand Down