/
connection.py
38 lines (28 loc) · 1.03 KB
/
connection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from typing import Generic, TypeVar, AsyncContextManager
from abc import abstractmethod
Request = TypeVar("Request")
Response = TypeVar("Response")
class Connection(Generic[Request, Response], AsyncContextManager["Connection"]):
"""
A connection to an underlying stream. Only one call to 'read' may be outstanding at a time.
"""
@abstractmethod
async def write(self, request: Request) -> None:
"""
Write a message to the stream.
Raises:
GoogleAPICallError: When the connection terminates in failure.
"""
raise NotImplementedError()
@abstractmethod
async def read(self) -> Response:
"""
Read a message off of the stream.
Raises:
GoogleAPICallError: When the connection terminates in failure.
"""
raise NotImplementedError()
class ConnectionFactory(Generic[Request, Response]):
"""A factory for producing Connections."""
async def new(self) -> Connection[Request, Response]:
raise NotImplementedError()