@@ -15,6 +15,7 @@ def create_channel(
1515 uri : str = "localhost:50051" ,
1616 metadata : Optional [List [Tuple [str , str ]]] = None ,
1717 options : Optional [List [Tuple [str , str ]]] = [],
18+ use_aio : Optional [bool ] = False ,
1819) -> grpc .Channel :
1920 def metadata_callback (context , callback ):
2021 callback (metadata , None )
@@ -39,9 +40,15 @@ def metadata_callback(context, callback):
3940 if metadata :
4041 auth_creds = grpc .metadata_call_credentials (metadata_callback )
4142 creds = grpc .composite_channel_credentials (creds , auth_creds )
42- channel = grpc .secure_channel (uri , creds , options = options )
43+ if use_aio :
44+ channel = grpc .aio .secure_channel (uri , creds , options = options )
45+ else :
46+ channel = grpc .secure_channel (uri , creds , options = options )
4347 else :
44- channel = grpc .insecure_channel (uri , options = options )
48+ if use_aio :
49+ channel = grpc .aio .insecure_channel (uri , options = options )
50+ else :
51+ channel = grpc .insecure_channel (uri , options = options )
4552 return channel
4653
4754
@@ -55,6 +62,7 @@ def __init__(
5562 ssl_client_cert : Optional [Union [str , os .PathLike ]] = None ,
5663 ssl_client_key : Optional [Union [str , os .PathLike ]] = None ,
5764 options : Optional [List [Tuple [str , str ]]] = [],
65+ use_aio : bool = False ,
5866 ) -> None :
5967 """
6068 Initialize the Auth class for establishing secure connections with a server.
@@ -76,6 +84,7 @@ def __init__(
7684 Used for mutual TLS authentication. Defaults to None.
7785 options (Optional[List[Tuple[str, str]]], optional): Additional gRPC channel options.
7886 Each tuple should contain (option_name, option_value). Defaults to [].
87+ use_aio (bool, optional): Whether to use asyncio for the channel. Defaults to False.
7988
8089 Raises:
8190 ValueError: If any metadata argument doesn't contain exactly 2 elements (key-value pair).
@@ -111,7 +120,14 @@ def __init__(
111120 )
112121 self .metadata .append (tuple (meta ))
113122 self .channel : grpc .Channel = create_channel (
114- self .ssl_root_cert , self .ssl_client_cert , self .ssl_client_key , self .use_ssl , self .uri , self .metadata , options = options
123+ self .ssl_root_cert ,
124+ self .ssl_client_cert ,
125+ self .ssl_client_key ,
126+ self .use_ssl ,
127+ self .uri ,
128+ self .metadata ,
129+ options = options ,
130+ use_aio = use_aio ,
115131 )
116132
117133 def get_auth_metadata (self ) -> List [Tuple [str , str ]]:
0 commit comments