Skip to content

Commit 6cc7691

Browse files
committed
Add an HTTP route to get the current Timestamp
Under a new router, `/unstable`, as `/unstable/timestamp`. Also add a smoketest that this route is reachable and returns a JSON-encoded `Timestamp`.
1 parent c99dc82 commit 6cc7691

4 files changed

Lines changed: 45 additions & 2 deletions

File tree

crates/client-api/src/routes/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ mod internal;
1212
pub mod metrics;
1313
pub mod prometheus;
1414
pub mod subscribe;
15+
pub mod unstable;
1516

1617
/// This API call is just designed to allow clients to determine whether or not they can
1718
/// establish a connection to SpacetimeDB. This API call doesn't actually do anything.
@@ -40,4 +41,5 @@ where
4041
axum::Router::new()
4142
.nest("/v1", router.layer(cors))
4243
.nest("/internal", internal::router())
44+
.nest("/unstable", unstable::router())
4345
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use axum::response::IntoResponse;
2+
use spacetimedb_lib::{sats, Timestamp};
3+
4+
use crate::NodeDelegate;
5+
6+
/// Returns the database's view of the current time,
7+
/// as a SATS-JSON encoded [`Timestamp`].
8+
async fn get_timestamp() -> impl IntoResponse {
9+
axum::Json(sats::serde::SerdeWrapper(Timestamp::now())).into_response()
10+
}
11+
12+
/// The internal router is for routes which are early in design,
13+
/// and may incompatibly change or be removed without a major version bump.
14+
pub fn router<S>() -> axum::Router<S>
15+
where
16+
S: NodeDelegate + Clone + 'static,
17+
{
18+
use axum::routing::get;
19+
20+
axum::Router::new().route("/timestamp", get(get_timestamp))
21+
}

smoketests/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,11 @@ def api_call(self, method, path, body = None, headers = {}):
264264
log_cmd([method, path])
265265
conn.request(method, path, body, headers)
266266
resp = conn.getresponse()
267-
logging.debug(f"{resp.status} {resp.read()}")
267+
body = resp.read()
268+
logging.debug(f"{resp.status} {body}")
268269
if resp.status != 200:
269270
raise resp
270-
resp
271+
return body
271272

272273

273274
@classmethod
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from .. import Smoketest, random_string
2+
import unittest
3+
import json
4+
import io
5+
6+
TIMESTAMP_TAG = "__timestamp_micros_since_unix_epoch__"
7+
8+
class TimestampRoute(Smoketest):
9+
AUTOPUBLISH = False
10+
11+
def test_timestamp_route(self):
12+
resp = self.api_call(
13+
"GET",
14+
"/unstable/timestamp",
15+
)
16+
timestamp = json.load(io.BytesIO(resp))
17+
self.assertIsInstance(timestamp, dict)
18+
self.assertIn(TIMESTAMP_TAG, timestamp)
19+
self.assertIsInstance(timestamp[TIMESTAMP_TAG], int)

0 commit comments

Comments
 (0)