示例#1
0
文件: jwt.py 项目: myackinc/Myack
 def payload_schema(self):
     return validx.Dict(
         {
             "sub": validx.Const(self.sub),
             "iat": validx.Float(min=0),
             "exp": validx.Float(min=0),
         },
         optional=None if self.ttl else ("exp",),
         extra=(validx.Str(), validx.Any()),
     )
示例#2
0
@dataclass
class Contract:
    name: str
    description: str
    validate_in: v.Dict
    validate_out: Union[v.Dict, v.List]


_contracts: Tuple[Contract, ...] = (
    Contract(
        name="push_tasks",
        description="Data about the tasks that ran on a given push.",
        validate_in=v.Dict(
            {
                "branch": v.Str(),
                "rev": v.Str(),
            }
        ),
        validate_out=v.List(
            v.Dict(
                {
                    "id": v.Str(),
                    "label": v.Str(),
                    "state": v.Str(
                        options=[
                            "completed",
                            "running",
                            "pending",
                            "unscheduled",
                            "exception",
import validx as v

city_schema: v.Validator = v.Dict(
    {
        "location": v.Tuple(v.Float(min=-90, max=90), v.Float(min=-180,
                                                              max=180)),
        "name": v.Str(),
        "alt_names": v.List(v.Str(), unique=True),
        "population": v.Dict({
            "city": v.Int(min=0),
            "metro": v.Int(min=0)
        }),
    },
    extra=(v.Str(), v.Any()),
)
示例#4
0
from mozci import data
from mozci.data.base import DataHandler, DataSource
from mozci.data.contract import Contract
from mozci.errors import (
    ContractNotFilled,
    ContractNotFound,
    InvalidSource,
    SourcesNotFound,
)

FAKE_CONTRACTS = (
    Contract(
        name="foo",
        description="test",
        validate_in=v.Dict({"label": v.Str()}),
        validate_out=v.Dict({"count": v.Int()}),
    ),
    Contract(
        name="bar",
        description="test",
        validate_in=v.Dict({"desc": v.Str()}),
        validate_out=v.Dict({"amount": v.Int()}),
    ),
    Contract(
        name="baz",
        description="test",
        validate_in=v.Dict({"id": v.Str()}),
        validate_out=v.Dict({"sum": v.Int()}),
    ),
    Contract(
示例#5
0
class Request:

    schema: ClassVar[validx.Validator] = validx.Dict(
        {
            "id":
            validx.Int(),
            "method":
            validx.Str(encoding="ascii"),
            "params":
            validx.Dict(extra=(validx.Str(), validx.Any())),
            "meta":
            validx.Dict(
                {
                    "version":
                    validx.Tuple(
                        validx.Int(min=0), validx.Int(min=0), nullable=True),
                },
                optional=("version", ),
                extra=(validx.Str(), validx.Any()),
            ),
        },
        defaults={
            "params": {},
            "meta": {}
        },
    )

    id: int
    method: str
    meta: Dict[str, Any]
    params: Dict[str, Any]

    middlewares: List[Middleware]
    injections: Dict[str, Any]

    handler: Optional[Callable[..., Awaitable[Any]]]
    handler_info: Dict[str, Any]

    def __init__(
        self,
        id: int,
        method: str,
        meta: Dict[str, Any] = None,
        params: Dict[str, Any] = None,
    ) -> None:
        self.id = id
        self.method = method
        self.meta = meta if meta is not None else {}
        self.params = params if params is not None else {}

        self.middlewares = []
        self.injections = {}

        self.handler = None
        self.handler_info = {}

    @classmethod
    def load(cls, payload: Dict[str, Any]) -> "Request":
        try:
            return cls(**cls.schema(payload))
        except validx.exc.ValidationError as e:
            raise RPCInvalidRequest(reason=cls.format_schema_error(e))

    @classmethod
    def format_schema_error(
            cls, error: validx.exc.ValidationError) -> Dict[str, str]:
        return dict(validx.exc.format_error(error))

    @cached_property
    def handler_signature(self) -> inspect.Signature:
        assert self.handler is not None
        return inspect.signature(self.handler)

    def response(
        self,
        result: Any = None,
        meta: Dict[str, Any] = None,
        error: BaseError = None,
        warnings: List[BaseWarning] = None,
    ) -> Response:
        return Response(id=self.id,
                        meta=meta,
                        result=result,
                        error=error,
                        warnings=warnings)
示例#6
0
文件: jwt.py 项目: myackinc/Myack
 def header_schema(self):
     return validx.Dict(
         {"typ": validx.Const("JWT"), "alg": validx.Const(self.alg)},
         extra=(validx.Str(), validx.Any()),
     )
示例#7
0

@dataclass
class Contract:
    name: str
    description: str
    validate_in: v.Dict
    validate_out: Union[v.Dict, v.List]


_contracts: Tuple[Contract, ...] = (
    Contract(
        name="push_tasks",
        description="Data about the tasks that ran on a given push.",
        validate_in=v.Dict({
            "branch": v.Str(),
            "rev": v.Str(),
        }),
        validate_out=v.List(
            v.Dict(
                {
                    "id":
                    v.Str(),
                    "label":
                    v.Str(),
                    "state":
                    v.Str(options=[
                        "completed",
                        "running",
                        "pending",
                        "unscheduled",