1
+ from __future__ import annotations
2
+
1
3
import abc
4
+ from io import BytesIO
5
+ from typing import Any , ClassVar , Dict , Optional , Type , Union
2
6
3
7
from .struct import Struct
4
8
from .types import Array , Int16 , Int32 , Schema , String , TaggedFields
@@ -12,7 +16,9 @@ class RequestHeader_v0(Struct):
12
16
("client_id" , String ("utf-8" )),
13
17
)
14
18
15
- def __init__ (self , request , correlation_id = 0 , client_id = "aiokafka" ):
19
+ def __init__ (
20
+ self , request : Request , correlation_id : int = 0 , client_id : str = "aiokafka"
21
+ ) -> None :
16
22
super ().__init__ (
17
23
request .API_KEY , request .API_VERSION , correlation_id , client_id
18
24
)
@@ -28,7 +34,13 @@ class RequestHeader_v1(Struct):
28
34
("tags" , TaggedFields ),
29
35
)
30
36
31
- def __init__ (self , request , correlation_id = 0 , client_id = "aiokafka" , tags = None ):
37
+ def __init__ (
38
+ self ,
39
+ request : Request ,
40
+ correlation_id : int = 0 ,
41
+ client_id : str = "aiokafka" ,
42
+ tags : Optional [Dict [int , bytes ]] = None ,
43
+ ):
32
44
super ().__init__ (
33
45
request .API_KEY , request .API_VERSION , correlation_id , client_id , tags or {}
34
46
)
@@ -48,32 +60,38 @@ class ResponseHeader_v1(Struct):
48
60
49
61
50
62
class Request (Struct , metaclass = abc .ABCMeta ):
51
- FLEXIBLE_VERSION = False
63
+ FLEXIBLE_VERSION : ClassVar [ bool ] = False
52
64
53
- @abc .abstractproperty
54
- def API_KEY (self ):
65
+ @property
66
+ @abc .abstractmethod
67
+ def API_KEY (self ) -> int :
55
68
"""Integer identifier for api request"""
56
69
57
- @abc .abstractproperty
58
- def API_VERSION (self ):
70
+ @property
71
+ @abc .abstractmethod
72
+ def API_VERSION (self ) -> int :
59
73
"""Integer of api request version"""
60
74
61
- @abc .abstractproperty
62
- def SCHEMA (self ):
63
- """An instance of Schema() representing the request structure"""
64
-
65
- @abc .abstractproperty
66
- def RESPONSE_TYPE (self ):
75
+ @property
76
+ @abc .abstractmethod
77
+ def RESPONSE_TYPE (self ) -> Type [Response ]:
67
78
"""The Response class associated with the api request"""
68
79
69
- def expect_response (self ):
80
+ @property
81
+ @abc .abstractmethod
82
+ def SCHEMA (self ) -> Schema :
83
+ """An instance of Schema() representing the request structure"""
84
+
85
+ def expect_response (self ) -> bool :
70
86
"""Override this method if an api request does not always generate a response"""
71
87
return True
72
88
73
- def to_object (self ):
89
+ def to_object (self ) -> Dict [ str , Any ] :
74
90
return _to_object (self .SCHEMA , self )
75
91
76
- def build_request_header (self , correlation_id , client_id ):
92
+ def build_request_header (
93
+ self , correlation_id : int , client_id : str
94
+ ) -> Union [RequestHeader_v0 , RequestHeader_v1 ]:
77
95
if self .FLEXIBLE_VERSION :
78
96
return RequestHeader_v1 (
79
97
self , correlation_id = correlation_id , client_id = client_id
@@ -82,31 +100,36 @@ def build_request_header(self, correlation_id, client_id):
82
100
self , correlation_id = correlation_id , client_id = client_id
83
101
)
84
102
85
- def parse_response_header (self , read_buffer ):
103
+ def parse_response_header (
104
+ self , read_buffer : Union [BytesIO , bytes ]
105
+ ) -> Union [ResponseHeader_v0 , ResponseHeader_v1 ]:
86
106
if self .FLEXIBLE_VERSION :
87
107
return ResponseHeader_v1 .decode (read_buffer )
88
108
return ResponseHeader_v0 .decode (read_buffer )
89
109
90
110
91
111
class Response (Struct , metaclass = abc .ABCMeta ):
92
- @abc .abstractproperty
93
- def API_KEY (self ):
112
+ @property
113
+ @abc .abstractmethod
114
+ def API_KEY (self ) -> int :
94
115
"""Integer identifier for api request/response"""
95
116
96
- @abc .abstractproperty
97
- def API_VERSION (self ):
117
+ @property
118
+ @abc .abstractmethod
119
+ def API_VERSION (self ) -> int :
98
120
"""Integer of api request/response version"""
99
121
100
- @abc .abstractproperty
101
- def SCHEMA (self ):
122
+ @property
123
+ @abc .abstractmethod
124
+ def SCHEMA (self ) -> Schema :
102
125
"""An instance of Schema() representing the response structure"""
103
126
104
- def to_object (self ):
127
+ def to_object (self ) -> Dict [ str , Any ] :
105
128
return _to_object (self .SCHEMA , self )
106
129
107
130
108
- def _to_object (schema , data ) :
109
- obj = {}
131
+ def _to_object (schema : Schema , data : Union [ Struct , Dict [ int , Any ]]) -> Dict [ str , Any ] :
132
+ obj : Dict [ str , Any ] = {}
110
133
for idx , (name , _type ) in enumerate (zip (schema .names , schema .fields )):
111
134
if isinstance (data , Struct ):
112
135
val = data .get_item (name )
@@ -116,7 +139,7 @@ def _to_object(schema, data):
116
139
if isinstance (_type , Schema ):
117
140
obj [name ] = _to_object (_type , val )
118
141
elif isinstance (_type , Array ):
119
- if isinstance (_type .array_of , ( Array , Schema ) ):
142
+ if isinstance (_type .array_of , Schema ):
120
143
obj [name ] = [_to_object (_type .array_of , x ) for x in val ]
121
144
else :
122
145
obj [name ] = val
0 commit comments