forked from microsoft/durabletask-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared.py
More file actions
49 lines (37 loc) · 1.38 KB
/
shared.py
File metadata and controls
49 lines (37 loc) · 1.38 KB
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
39
40
41
42
43
44
45
46
47
48
49
# Copyright (c) The Dapr Authors.
# Licensed under the MIT License.
from typing import Optional, Sequence, Union
import grpc
from grpc import aio as grpc_aio
from durabletask.internal.shared import (
get_default_host_address,
SECURE_PROTOCOLS,
INSECURE_PROTOCOLS,
)
ClientInterceptor = Union[
grpc_aio.UnaryUnaryClientInterceptor,
grpc_aio.UnaryStreamClientInterceptor,
grpc_aio.StreamUnaryClientInterceptor,
grpc_aio.StreamStreamClientInterceptor
]
def get_grpc_aio_channel(
host_address: Optional[str],
secure_channel: bool = False,
interceptors: Optional[Sequence[ClientInterceptor]] = None) -> grpc_aio.Channel:
if host_address is None:
host_address = get_default_host_address()
for protocol in SECURE_PROTOCOLS:
if host_address.lower().startswith(protocol):
secure_channel = True
host_address = host_address[len(protocol):]
break
for protocol in INSECURE_PROTOCOLS:
if host_address.lower().startswith(protocol):
secure_channel = False
host_address = host_address[len(protocol):]
break
if secure_channel:
channel = grpc_aio.secure_channel(host_address, grpc.ssl_channel_credentials(), interceptors=interceptors)
else:
channel = grpc_aio.insecure_channel(host_address, interceptors=interceptors)
return channel