Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_positionals_with_context(self):
methods = {"square": lambda foo, context=None: context}
req = Request(
{"jsonrpc": "2.0", "method": "square", "params": [FOO], "id": 1},
context=BAR,
)
self.assertEqual(BAR, req.call(methods)["result"])
def test_configuring_http_status(self):
NotificationResponse.http_status = status.HTTP_OK
req = Request({"jsonrpc": "2.0", "method": "foo"}).call([foo])
self.assertEqual(status.HTTP_OK, req.http_status)
NotificationResponse.http_status = status.HTTP_NO_CONTENT
def test_positionals(self):
methods = {"square": lambda x: x * x}
req = Request({"jsonrpc": "2.0", "method": "square", "params": [3], "id": 1})
self.assertEqual(9, req.call(methods)["result"])
def test_methods_functions_with_decorator(self):
methods = Methods()
@methods.add
def foo():
return "bar"
req = Request({"jsonrpc": "2.0", "method": "foo", "id": 1})
self.assertEqual("bar", req.call(methods)["result"])
def test_dict_partials(self):
multiply = lambda x, y: x * y
req = Request({"jsonrpc": "2.0", "method": "baz", "params": [3], "id": 1})
self.assertEqual(6, req.call({"baz": partial(multiply, 2)})["result"])
def test_request_id_notification(self):
req = Request({"jsonrpc": "2.0", "method": "foo"})
self.assertEqual(None, req.request_id)
def test_methods_lambdas(self):
methods = Methods()
methods.add(lambda: "bar", "foo")
req = Request({"jsonrpc": "2.0", "method": "foo", "id": 1})
self.assertEqual("bar", req.call(methods)["result"])
def test_invalid_request(self):
req = Request({"jsonrpc": "2.0"})
self.assertIsInstance(req.response, ErrorResponse)
) -> Union[Request, Set[Request]]:
"""
Create a Request object from a dictionary (or list of them).
Args:
requests: Request object, or a collection of them.
methods: The list of methods that can be called.
context: If specified, will be the first positional argument in all requests.
convert_camel_case: Will convert the method name/any named params to snake case.
Returns:
A Request object, or a collection of them.
"""
if isinstance(requests, list):
return {
Request(context=context, convert_camel_case=convert_camel_case, **request)
for request in requests
}
return Request(context=context, convert_camel_case=convert_camel_case, **requests)
"""Asynchronous request."""
from .request import Request
from .request_utils import *
from .response import NotificationResponse, RequestResponse, Response
class AsyncRequest(Request):
"""Asynchronous request"""
async def call(self, methods):
# Validation or parsing may have failed in __init__, in which case
# there's no point calling. It would've already set the response.
if not self.response:
# Handles setting the result/exception of the call
with self.handle_exceptions():
# Get the method object from a list (raises MethodNotFound)
callable_ = self.get_method(methods)
# Ensure the arguments match the method's signature
validate_arguments_against_signature(callable_, self.args, self.kwargs)
# Call the method
result = await callable_(*(self.args or []), **(self.kwargs or {}))
# Set the response
if self.is_notification: