mirror of https://github.com/nucypher/nucypher.git
Add common base condition class for JSON Api and Rpc for common verify method.
parent
6553f6bd77
commit
f718a00ba1
|
@ -1,4 +1,4 @@
|
|||
from typing import Any, Optional, Tuple
|
||||
from typing import Any, Optional
|
||||
|
||||
from marshmallow import ValidationError, fields, post_load, validate, validates
|
||||
from marshmallow.fields import Url
|
||||
|
@ -6,11 +6,11 @@ from typing_extensions import override
|
|||
|
||||
from nucypher.policy.conditions.context import is_context_variable
|
||||
from nucypher.policy.conditions.json.base import (
|
||||
BaseJsonRequestCondition,
|
||||
HTTPMethod,
|
||||
JSONPathField,
|
||||
JsonRequestCall,
|
||||
)
|
||||
from nucypher.policy.conditions.json.utils import process_result_for_condition_eval
|
||||
from nucypher.policy.conditions.lingo import (
|
||||
ConditionType,
|
||||
ExecutionCallAccessControlCondition,
|
||||
|
@ -61,9 +61,9 @@ class JsonApiCall(JsonRequestCall):
|
|||
return super()._execute(endpoint=self.endpoint, **context)
|
||||
|
||||
|
||||
class JsonApiCondition(ExecutionCallAccessControlCondition):
|
||||
class JsonApiCondition(BaseJsonRequestCondition):
|
||||
"""
|
||||
A JSON API condition is a condition that can be evaluated by reading from a JSON
|
||||
A JSON API condition is a condition that can be evaluated by performing a GET on a JSON
|
||||
HTTPS endpoint. The response must return an HTTP 200 with valid JSON in the response body.
|
||||
The response will be deserialized as JSON and parsed using jsonpath.
|
||||
"""
|
||||
|
@ -119,18 +119,3 @@ class JsonApiCondition(ExecutionCallAccessControlCondition):
|
|||
@property
|
||||
def authorization_token(self):
|
||||
return self.execution_call.authorization_token
|
||||
|
||||
def verify(self, **context) -> Tuple[bool, Any]:
|
||||
"""
|
||||
Verifies the offchain condition is met by performing a read operation on the endpoint
|
||||
and evaluating the return value test with the result. Parses the endpoint's JSON response using
|
||||
JSONPath.
|
||||
"""
|
||||
result = self.execution_call.execute(**context)
|
||||
result_for_eval = process_result_for_condition_eval(result)
|
||||
|
||||
resolved_return_value_test = self.return_value_test.with_resolved_context(
|
||||
**context
|
||||
)
|
||||
eval_result = resolved_return_value_test.eval(result_for_eval) # test
|
||||
return eval_result, result
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
from abc import ABC
|
||||
from enum import Enum
|
||||
from http import HTTPStatus
|
||||
from typing import Any, Optional
|
||||
from typing import Any, Optional, Tuple
|
||||
|
||||
import requests
|
||||
from jsonpath_ng.exceptions import JsonPathLexerError, JsonPathParserError
|
||||
|
@ -17,6 +17,8 @@ from nucypher.policy.conditions.exceptions import (
|
|||
ConditionEvaluationFailed,
|
||||
JsonRequestException,
|
||||
)
|
||||
from nucypher.policy.conditions.json.utils import process_result_for_condition_eval
|
||||
from nucypher.policy.conditions.lingo import ExecutionCallAccessControlCondition
|
||||
from nucypher.utilities.logging import Logger
|
||||
|
||||
|
||||
|
@ -140,3 +142,18 @@ class JSONPathField(Field):
|
|||
except (JsonPathLexerError, JsonPathParserError):
|
||||
raise self.make_error("invalid", value=value)
|
||||
return value
|
||||
|
||||
|
||||
class BaseJsonRequestCondition(ExecutionCallAccessControlCondition, ABC):
|
||||
def verify(self, **context) -> Tuple[bool, Any]:
|
||||
"""
|
||||
Verifies the JSON condition.
|
||||
"""
|
||||
result = self.execution_call.execute(**context)
|
||||
result_for_eval = process_result_for_condition_eval(result)
|
||||
|
||||
resolved_return_value_test = self.return_value_test.with_resolved_context(
|
||||
**context
|
||||
)
|
||||
eval_result = resolved_return_value_test.eval(result_for_eval) # test
|
||||
return eval_result, result
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
from abc import ABC
|
||||
from typing import Any, Optional, Tuple
|
||||
from typing import Any, Optional
|
||||
|
||||
from marshmallow import ValidationError, fields, post_load, validate, validates
|
||||
from marshmallow.fields import Url
|
||||
|
@ -10,11 +10,11 @@ from nucypher.policy.conditions.exceptions import (
|
|||
JsonRequestException,
|
||||
)
|
||||
from nucypher.policy.conditions.json.base import (
|
||||
BaseJsonRequestCondition,
|
||||
HTTPMethod,
|
||||
JSONPathField,
|
||||
JsonRequestCall,
|
||||
)
|
||||
from nucypher.policy.conditions.json.utils import process_result_for_condition_eval
|
||||
from nucypher.policy.conditions.lingo import (
|
||||
ConditionType,
|
||||
ExecutionCallAccessControlCondition,
|
||||
|
@ -109,7 +109,13 @@ class JsonEndpointRPCCall(BaseJsonRPCCall):
|
|||
return super()._execute(endpoint=self.endpoint, **context)
|
||||
|
||||
|
||||
class JsonRpcCondition(ExecutionCallAccessControlCondition):
|
||||
class JsonRpcCondition(BaseJsonRequestCondition):
|
||||
"""
|
||||
A JSON RPC condition is a condition that can be evaluated by performing a POST on a JSON
|
||||
HTTPS endpoint. The response must return an HTTP 200 with valid JSON RPC 2.0 response.
|
||||
The response will be deserialized as JSON and parsed using jsonpath.
|
||||
"""
|
||||
|
||||
EXECUTION_CALL_TYPE = JsonEndpointRPCCall
|
||||
CONDITION_TYPE = ConditionType.JSONRPC.value
|
||||
|
||||
|
@ -169,13 +175,3 @@ class JsonRpcCondition(ExecutionCallAccessControlCondition):
|
|||
@property
|
||||
def timeout(self):
|
||||
return self.execution_call.timeout
|
||||
|
||||
def verify(self, **context) -> Tuple[bool, Any]:
|
||||
result = self.execution_call.execute(**context)
|
||||
result_for_eval = process_result_for_condition_eval(result)
|
||||
|
||||
resolved_return_value_test = self.return_value_test.with_resolved_context(
|
||||
**context
|
||||
)
|
||||
eval_result = resolved_return_value_test.eval(result_for_eval) # test
|
||||
return eval_result, result
|
||||
|
|
Loading…
Reference in New Issue