|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +# contributor license agreements. See the NOTICE file distributed with |
| 4 | +# this work for additional information regarding copyright ownership. |
| 5 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +# (the "License"); you may not use this file except in compliance with |
| 7 | +# the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +import abc |
| 18 | +import logging |
| 19 | +from dataclasses import dataclass |
| 20 | +from typing import Any, Callable, Optional, List, Tuple |
| 21 | + |
| 22 | +__all__ = [ |
| 23 | + "ParameterDescriptor", |
| 24 | + "MethodDescriptor", |
| 25 | + "TransportCodec", |
| 26 | + "SerializationEncoder", |
| 27 | + "SerializationDecoder", |
| 28 | +] |
| 29 | + |
| 30 | +logger = logging.getLogger(__name__) |
| 31 | + |
| 32 | + |
| 33 | +@dataclass |
| 34 | +class ParameterDescriptor: |
| 35 | + """Information about a method parameter""" |
| 36 | + |
| 37 | + name: str |
| 38 | + annotation: Any |
| 39 | + is_required: bool = True |
| 40 | + default_value: Any = None |
| 41 | + |
| 42 | + |
| 43 | +@dataclass |
| 44 | +class MethodDescriptor: |
| 45 | + """Method descriptor with function details""" |
| 46 | + |
| 47 | + function: Callable |
| 48 | + name: str |
| 49 | + parameters: List[ParameterDescriptor] |
| 50 | + return_parameter: ParameterDescriptor |
| 51 | + documentation: Optional[str] = None |
| 52 | + |
| 53 | + |
| 54 | +class TransportCodec(abc.ABC): |
| 55 | + """ |
| 56 | + The transport codec interface. |
| 57 | + """ |
| 58 | + |
| 59 | + @classmethod |
| 60 | + @abc.abstractmethod |
| 61 | + def get_transport_type(cls) -> str: |
| 62 | + """ |
| 63 | + Get transport type of current codec |
| 64 | + :return: The transport type. |
| 65 | + :rtype: str |
| 66 | + """ |
| 67 | + raise NotImplementedError() |
| 68 | + |
| 69 | + @abc.abstractmethod |
| 70 | + def get_encoder(self) -> "SerializationEncoder": |
| 71 | + """ |
| 72 | + Get encoder instance |
| 73 | + :return: The encoder. |
| 74 | + :rtype: SerializationEncoder |
| 75 | + """ |
| 76 | + raise NotImplementedError() |
| 77 | + |
| 78 | + @abc.abstractmethod |
| 79 | + def get_decoder(self) -> "SerializationDecoder": |
| 80 | + """ |
| 81 | + Get decoder instance |
| 82 | + :return: The decoder. |
| 83 | + :rtype: SerializationDecoder |
| 84 | + """ |
| 85 | + raise NotImplementedError() |
| 86 | + |
| 87 | + |
| 88 | +class SerializationEncoder(abc.ABC): |
| 89 | + """ |
| 90 | + The serialization encoder interface. |
| 91 | + """ |
| 92 | + |
| 93 | + @abc.abstractmethod |
| 94 | + def encode(self, arguments: Tuple[Any, ...]) -> bytes: |
| 95 | + """ |
| 96 | + Encode arguments to bytes. |
| 97 | + :param arguments: The arguments to encode. |
| 98 | + :type arguments: Tuple[Any, ...] |
| 99 | + :return: The encoded bytes. |
| 100 | + :rtype: bytes |
| 101 | + """ |
| 102 | + raise NotImplementedError() |
| 103 | + |
| 104 | + |
| 105 | +class SerializationDecoder(abc.ABC): |
| 106 | + """ |
| 107 | + The serialization decoder interface. |
| 108 | + """ |
| 109 | + |
| 110 | + @abc.abstractmethod |
| 111 | + def decode(self, data: bytes) -> Any: |
| 112 | + """ |
| 113 | + Decode bytes to object. |
| 114 | + :param data: The data to decode. |
| 115 | + :type data: bytes |
| 116 | + :return: The decoded object. |
| 117 | + :rtype: Any |
| 118 | + """ |
| 119 | + raise NotImplementedError() |
0 commit comments