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

Move request-schema.json to a python file #284

Merged
merged 1 commit into from
Aug 28, 2024
Merged
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
9 changes: 4 additions & 5 deletions jsonrpcserver/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"""

import json
from importlib.resources import read_text
from typing import Any, Callable, Dict, List, Union, cast

from jsonschema.validators import validator_for # type: ignore
Expand All @@ -23,6 +22,7 @@
validate_args,
)
from .methods import Methods, global_methods
from .request_schema import REQUEST_SCHEMA
from .response import Response, to_dict
from .sentinels import NOCONTEXT
from .utils import identity
Expand All @@ -32,10 +32,9 @@

# Prepare the jsonschema validator. This is global so it loads only once, not every time
# dispatch is called.
schema = json.loads(read_text(__package__, "request-schema.json"))
klass = validator_for(schema)
klass.check_schema(schema)
default_jsonrpc_validator = klass(schema).validate
klass = validator_for(REQUEST_SCHEMA)
klass.check_schema(REQUEST_SCHEMA)
default_jsonrpc_validator = klass(REQUEST_SCHEMA).validate


def dispatch_to_response(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,32 @@
{
REQUEST_SCHEMA = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "A JSON RPC 2.0 request",
"oneOf": [
{
"description": "An individual request",
"$ref": "#/definitions/request"
},
{"description": "An individual request", "$ref": "#/definitions/request"},
{
"description": "An array of requests",
"type": "array",
"items": { "$ref": "#/definitions/request" },
"minItems": 1
}
"items": {"$ref": "#/definitions/request"},
"minItems": 1,
},
],
"definitions": {
"request": {
"type": "object",
"required": [ "jsonrpc", "method" ],
"required": ["jsonrpc", "method"],
"properties": {
"jsonrpc": { "enum": [ "2.0" ] },
"method": {
"type": "string"
},
"jsonrpc": {"enum": ["2.0"]},
"method": {"type": "string"},
"id": {
"type": [ "string", "number", "null" ],
"type": ["string", "number", "null"],
"note": [
"While allowed, null should be avoided: http://www.jsonrpc.org/specification#id1",
"While allowed, a number with a fractional part should be avoided: http://www.jsonrpc.org/specification#id2"
]
"While allowed, a number with a fractional part should be avoided: http://www.jsonrpc.org/specification#id2",
],
},
"params": {
"type": [ "array", "object" ]
}
"params": {"type": ["array", "object"]},
},
"additionalProperties": false
"additionalProperties": False,
}
}
},
}
Loading