-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
31 lines (24 loc) · 1.25 KB
/
common.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
import grpc
from google.api_core.client_options import ClientOptions
from google.auth.credentials import AnonymousCredentials
from google.pubsub_v1.services.publisher.client import PublisherClient
from google.pubsub_v1.services.publisher.transports.grpc import PublisherGrpcTransport
from google.pubsub_v1.services.subscriber.client import SubscriberClient
from google.pubsub_v1.services.subscriber.transports import SubscriberGrpcTransport
PUBSUB_EMULATOR_ENDPOINT_DEFAULT = "localhost:8085"
def get_publisher_client(endpoint: str) -> PublisherClient:
grpc_channel = grpc.insecure_channel(endpoint)
transport = PublisherGrpcTransport(
channel=grpc_channel, credentials=AnonymousCredentials()
)
client_options = ClientOptions(api_endpoint=endpoint)
publisher = PublisherClient(transport=transport, client_options=client_options)
return publisher
def get_subscriber_client(endpoint: str) -> SubscriberClient:
grpc_channel = grpc.insecure_channel(endpoint)
transport = SubscriberGrpcTransport(
channel=grpc_channel, credentials=AnonymousCredentials()
)
client_options = ClientOptions(api_endpoint=endpoint)
subscriber = SubscriberClient(transport=transport, client_options=client_options)
return subscriber