Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
dapr run --protocol grpc --grpc-port=50001 python example.py
"""
import grpc
import os
from dapr.proto import api_v1, api_service_v1, common_v1
from google.protobuf.any_pb2 import Any
# Get port from environment variable.
port = os.getenv('DAPR_GRPC_PORT', '50001')
daprUri = 'localhost:' + port
channel = grpc.insecure_channel(daprUri)
client = api_service_v1.DaprStub(channel)
client.PublishEvent(api_v1.PublishEventRequest(topic='sith', data='lala'.encode('utf-8')))
print('Published!')
key = 'mykey'
storeName = 'statestore'
req = common_v1.StateItem(key=key, value='my state'.encode('utf-8'))
state = api_v1.SaveStateRequest(store_name=storeName, states=[req])
client.SaveState(state)
print('Saved!')
resp = client.GetState(api_v1.GetStateRequest(store_name=storeName, key=key))
print('Got!')
print(resp)
resp = client.DeleteState(api_v1.DeleteStateRequest(store_name=storeName, key=key))
def __init__(self, address: Optional[str] = None):
"""Connects to Dapr Runtime and initialize gRPC client stub.
Args:
address (str, optional): Dapr Runtime gRPC endpoint address.
"""
if not address:
address = f"{settings.DAPR_RUNTIME_HOST}:{settings.DAPR_GRPC_PORT}"
self._channel = grpc.insecure_channel(address)
if settings.DAPR_API_TOKEN:
api_token_interceptor = DaprClientInterceptor([
('dapr-api-token', settings.DAPR_API_TOKEN), ])
self._channel = grpc.intercept_channel(self._channel, api_token_interceptor)
self._stub = api_service_v1.DaprStub(self._channel)
import os
import grpc
from dapr.proto import api_v1, api_service_v1, common_v1
import proto.response_pb2 as response_messages
from google.protobuf.any_pb2 import Any
# Start a gRPC client
port = os.getenv('DAPR_GRPC_PORT')
channel = grpc.insecure_channel(f"localhost:{port}")
client = api_service_v1.DaprStub(channel)
print(f"Started gRPC client on DAPR_GRPC_PORT: {port}")
# Invoke the Receiver
req = api_v1.InvokeServiceRequest(
id="invoke-receiver",
message=common_v1.InvokeRequest(
method='my_method',
data=Any(value='SOME_DATA'.encode('utf-8')),
content_type="text/plain; charset=UTF-8")
)
response = client.InvokeService(req)
# Unpack the response
res = response_messages.CustomResponse()
if response.data.Is(response_messages.CustomResponse.DESCRIPTOR):
import os
import grpc
from dapr.proto import api_v1, api_service_v1, common_v1
from google.protobuf.any_pb2 import Any
# Start a gRPC client
port = os.getenv('DAPR_GRPC_PORT')
channel = grpc.insecure_channel(f"localhost:{port}")
client = api_service_v1.DaprStub(channel)
print(f"Started gRPC client on DAPR_GRPC_PORT: {port}")
# Create a typed message with content type and body
test_message = Any(value='INVOKE_RECEIVED'.encode('utf-8'))
# Invoke the method 'my-method' on receiver
req = api_v1.InvokeServiceRequest(
id="invoke-receiver",
message=common_v1.InvokeRequest(
method='my-method',
data=test_message,
content_type="text/plain; charset=UTF-8")
)
response = client.InvokeService(req)
# Print the response