Coverage for /home/runner/work/zserio/zserio/compiler/extensions/python/runtime/src/zserio/service.py: 100%

48 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2024-07-18 11:41 +0000

1""" 

2The module provides classes for Zserio services. 

3""" 

4 

5import typing 

6 

7from zserio.exception import PythonRuntimeException 

8from zserio.bitwriter import BitStreamWriter 

9 

10 

11class ServiceData: 

12 """ 

13 Data abstraction to be sent or to be received in all Zserio services. 

14 """ 

15 

16 @property 

17 def zserio_object(self) -> typing.Any: 

18 """ 

19 Gets the Zserio object which represents the request if available. 

20 

21 :returns: The Zserio object or None. 

22 """ 

23 

24 raise NotImplementedError() 

25 

26 @property 

27 def byte_array(self) -> bytes: 

28 """ 

29 Gets the data which represent the request. 

30 

31 :returns: The request data. 

32 """ 

33 

34 raise NotImplementedError() 

35 

36 

37class ObjectServiceData(ServiceData): 

38 """ 

39 ServiceData implementation based on object generated by Zserio. 

40 """ 

41 

42 def __init__(self, zserio_object: typing.Any): 

43 """ 

44 Constructor. 

45 

46 :param zserio_object: Zserio object from which to create service data. 

47 """ 

48 

49 self._is_byte_array_initialized = False 

50 self._byte_array = bytes() 

51 self._zserio_object = zserio_object 

52 

53 @property 

54 def zserio_object(self) -> typing.Any: 

55 """ 

56 Gets the Zserio object which represents the request. 

57 

58 :returns: The Zserio object. 

59 """ 

60 

61 return self._zserio_object 

62 

63 @property 

64 def byte_array(self) -> bytes: 

65 """ 

66 Gets the data which represent the request. 

67 

68 :returns: The request data which are created by serialization of Zserio object. 

69 """ 

70 

71 if not self._is_byte_array_initialized: 

72 writer = BitStreamWriter() 

73 self._zserio_object.write(writer) 

74 self._byte_array = writer.byte_array 

75 self._is_byte_array_initialized = True 

76 

77 return self._byte_array 

78 

79 

80class RawServiceData(ServiceData): 

81 """ 

82 Service data implementation based on raw data. 

83 """ 

84 

85 def __init__(self, raw_data: bytes): 

86 """ 

87 Constructor. 

88 

89 :param raw_data: Raw data to use. 

90 """ 

91 

92 self._raw_data = raw_data 

93 

94 @property 

95 def zserio_object(self) -> None: 

96 return None 

97 

98 @property 

99 def byte_array(self) -> bytes: 

100 return self._raw_data 

101 

102 

103class ServiceInterface: 

104 """ 

105 Generic interface for all Zserio services on server side. 

106 """ 

107 

108 def call_method(self, method_name: str, request_data: bytes, context: typing.Any = None) -> ServiceData: 

109 """ 

110 Calls method with the given name synchronously. 

111 

112 :param method_name: Name of the service method to call. 

113 :param request_data: Request data to be passed to the method. 

114 :param context: Context specific for particular service. 

115 :returns: Response service data. 

116 :raises ServiceException: If the call fails. 

117 """ 

118 raise NotImplementedError() 

119 

120 @property 

121 def service_full_name(self) -> str: 

122 """ 

123 Gets service full name. 

124 

125 :returns: Service full name. 

126 """ 

127 raise NotImplementedError() 

128 

129 @property 

130 def method_names(self) -> typing.List: 

131 """ 

132 Gets list of service method names. 

133 

134 :returns: List of service method names. 

135 """ 

136 raise NotImplementedError() 

137 

138 

139class ServiceClientInterface: 

140 """ 

141 Generic interface for all Zserio services on client side. 

142 """ 

143 

144 def call_method(self, method_name: str, request: ServiceData, context: typing.Any = None) -> bytes: 

145 """ 

146 Calls method with the given name synchronously. 

147 

148 :param method_name: Name of the service method to call. 

149 :param request: Request service data to be passed to the method. 

150 :param context: Context specific for particular service. 

151 :returns: Response data. 

152 :raises ServiceException: If the call fails. 

153 """ 

154 raise NotImplementedError() 

155 

156 

157class ServiceException(PythonRuntimeException): 

158 """ 

159 Exception thrown in case of an error in Zserio Service 

160 """