Apply AI RFCs from #3571.

- Use random uuid4 as id for request
- Improve error handling in case code/message isn't available.
- Add to headers dictionary instead of recreating
- offchain -> off-chain
pull/3571/head
derekpierre 2025-01-02 09:19:56 -05:00
parent f718a00ba1
commit 74df16e015
No known key found for this signature in database
4 changed files with 20 additions and 10 deletions

View File

@ -1 +1 @@
Support for conditions based on APIs provided by offchain JSON RPC 2.0 endpoints.
Support for conditions based on APIs provided by off-chain JSON RPC 2.0 endpoints.

View File

@ -62,7 +62,7 @@ class JsonRequestCall(ExecutionCall, ABC):
resolved_authorization_token = resolve_any_context_variables(
self.authorization_token, **context
)
headers = {"Authorization": f"Bearer {resolved_authorization_token}"}
headers["Authorization"] = f"Bearer {resolved_authorization_token}"
try:
if self.http_method == HTTPMethod.GET:
@ -139,8 +139,8 @@ class JSONPathField(Field):
try:
if not string_contains_context_variable(value):
parse(value)
except (JsonPathLexerError, JsonPathParserError):
raise self.make_error("invalid", value=value)
except (JsonPathLexerError, JsonPathParserError) as e:
raise self.make_error("invalid", value=value) from e
return value

View File

@ -1,5 +1,6 @@
from abc import ABC
from typing import Any, Optional
from uuid import uuid4
from marshmallow import ValidationError, fields, post_load, validate, validates
from marshmallow.fields import Url
@ -50,7 +51,7 @@ class BaseJsonRPCCall(JsonRequestCall, ABC):
"jsonrpc": "2.0",
"method": self.method,
"params": self.params,
"id": 1, # any id will do
"id": str(uuid4()), # any random id will do
}
super().__init__(
http_method=HTTPMethod.POST,
@ -67,7 +68,7 @@ class BaseJsonRPCCall(JsonRequestCall, ABC):
error = data.get("error")
if error:
raise JsonRequestException(
f"JSON RPC Request failed with error in response: code={error['code']}, msg={error['message']}"
f"JSON RPC Request failed with error in response: code={error.get('code')}, msg={error.get('message')}"
)
# obtain result first then perform query

View File

@ -1,4 +1,5 @@
import json
import uuid
import pytest
import requests
@ -14,6 +15,14 @@ from nucypher.policy.conditions.lingo import (
ReturnValueTest,
)
UUID4_STR = "b192fdd2-1529-4fe9-a671-e5386453aa9c"
@pytest.fixture(scope="function", autouse=True)
def mock_uuid4(mocker):
u = uuid.UUID(UUID4_STR, version=4)
mocker.patch("nucypher.policy.conditions.json.rpc.uuid4", return_value=u)
def test_json_rpc_condition_initialization():
condition = JsonRpcCondition(
@ -84,7 +93,7 @@ def test_json_rpc_condition_verify(mocker):
assert mocked_method.call_count == 1
assert mocked_method.call_args.kwargs["json"] == {
"jsonrpc": "2.0",
"id": 1,
"id": UUID4_STR,
"method": condition.method,
"params": condition.params,
}
@ -111,7 +120,7 @@ def test_json_rpc_condition_verify_params_as_dict(mocker):
assert mocked_method.call_count == 1
assert mocked_method.call_args.kwargs["json"] == {
"jsonrpc": "2.0",
"id": 1,
"id": UUID4_STR,
"method": condition.method,
"params": condition.params,
}
@ -196,7 +205,7 @@ def test_json_rpc_condition_evaluation_with_auth_token(mocker):
assert mocked_method.call_count == 1
assert mocked_method.call_args.kwargs["json"] == {
"jsonrpc": "2.0",
"id": 1,
"id": UUID4_STR,
"method": condition.method,
"params": condition.params,
}
@ -242,7 +251,7 @@ def test_json_rpc_condition_evaluation_with_various_context_variables(mocker):
assert call_args.kwargs["headers"]["Authorization"] == f"Bearer {auth_token}"
assert call_args.kwargs["json"] == {
"jsonrpc": "2.0",
"id": 1,
"id": UUID4_STR,
"method": context[":methodContextVar"],
"params": [42, 23],
}