Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
afshin committed Oct 29, 2024
1 parent 3abb802 commit 26d9115
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
13 changes: 6 additions & 7 deletions jupyter_server/services/events/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ def on_close(self):
self.event_logger.remove_listener(listener=self.event_listener)


def validate_model(data: dict[str, Any], schema: jupyter_events.schema.EventSchema) -> None:
def validate_model(
data: dict[str, Any], registry: jupyter_events.schema_registry.SchemaRegistry
) -> None:
"""Validates for required fields in the JSON request body and verifies that
a registered schema/version exists"""
required_keys = {"schema_id", "version", "data"}
Expand All @@ -81,9 +83,7 @@ def validate_model(data: dict[str, Any], schema: jupyter_events.schema.EventSche
raise Exception(message)
schema_id = cast(str, data.get("schema_id"))
version = cast(int, data.get("version"))
if schema is None:
message = f"Unregistered schema: `{schema_id}`"
raise Exception(message)
schema = registry.get(schema_id)
if schema.version != version:
message = f"Unregistered version: `{version}` for `{schema_id}`"
raise Exception(message)
Expand Down Expand Up @@ -121,10 +121,9 @@ async def post(self):
raise web.HTTPError(400, "No JSON data provided")

try:
schema = self.event_logger.schemas.get(cast(str, payload.get("schema_id")))
validate_model(payload, schema)
validate_model(payload, self.event_logger.schemas)
self.event_logger.emit(
schema_id=schema.id,
schema_id=cast(str, payload.get("schema_id")),
data=cast("Dict[str, Any]", payload.get("data")),
timestamp_override=get_timestamp(payload),
)
Expand Down
1 change: 1 addition & 0 deletions tests/services/events/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,5 @@ async def test_post_event(jp_fetch, event_logger_sink, payload):
async def test_post_event_400(jp_fetch, event_logger, payload):
with pytest.raises(tornado.httpclient.HTTPClientError) as e:
await jp_fetch("api", "events", method="POST", body=payload)

assert expected_http_error(e, 400)

0 comments on commit 26d9115

Please sign in to comment.