Skip to content

Commit

Permalink
rest: define openapi schema for health check endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
bmwill committed Jul 23, 2024
1 parent 5d47ed3 commit 730f2c6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
27 changes: 26 additions & 1 deletion crates/sui-rest-api/openapi/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,32 @@
}
},
"/health": {
"get": {}
"get": {
"tags": [
"General"
],
"operationId": "HealthCheck",
"parameters": [
{
"in": "query",
"name": "threshold_seconds",
"schema": {
"type": "integer",
"format": "uint32",
"minimum": 0.0
},
"style": "form"
}
],
"responses": {
"200": {
"description": "",
"content": {
"text/plain; charset=utf-8": {}
}
}
}
}
},
"/accounts/{account}/objects": {
"get": {
Expand Down
16 changes: 14 additions & 2 deletions crates/sui-rest-api/src/health.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use crate::{
openapi::{ApiEndpoint, RouteHandler},
openapi::{ApiEndpoint, OperationBuilder, ResponseBuilder, RouteHandler},
reader::StateReader,
RestService, Result,
};
Expand All @@ -26,12 +26,24 @@ impl ApiEndpoint<RestService> for HealthCheck {
"/health"
}

fn operation(
&self,
generator: &mut schemars::gen::SchemaGenerator,
) -> openapiv3::v3_1::Operation {
OperationBuilder::new()
.tag("General")
.operation_id("HealthCheck")
.query_parameters::<Threshold>(generator)
.response(200, ResponseBuilder::new().text_content().build())
.build()
}

fn handler(&self) -> crate::openapi::RouteHandler<RestService> {
RouteHandler::new(self.method(), health)
}
}

#[derive(serde::Deserialize, serde::Serialize)]
#[derive(Debug, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
pub struct Threshold {
pub threshold_seconds: Option<u32>,
}
Expand Down
4 changes: 4 additions & 0 deletions crates/sui-rest-api/src/openapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,10 @@ impl ResponseBuilder {
pub fn bcs_content(&mut self) -> &mut Self {
self.content(crate::APPLICATION_BCS, MediaType::default())
}

pub fn text_content(&mut self) -> &mut Self {
self.content(mime::TEXT_PLAIN_UTF_8.as_ref(), MediaType::default())
}
}

#[derive(Default)]
Expand Down

0 comments on commit 730f2c6

Please sign in to comment.