Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def GetSecret(self, request, context) -> api_v1.GetSecretResponse:
headers = ()
trailers = ()
key = request.key
headers = headers + (('keyh', key), )
trailers = trailers + (('keyt', key), )
resp = {key: "val"}
context.send_initial_metadata(headers)
context.set_trailing_metadata(trailers)
return api_v1.GetSecretResponse(data=resp)
for k, v in request.metadata.items():
headers = headers + (('h' + k, v), )
trailers = trailers + (('t' + k, v), )
resp_data = b'INVALID'
metadata = {}
if request.operation == 'create':
resp_data = request.data
metadata = request.metadata
context.send_initial_metadata(headers)
context.set_trailing_metadata(trailers)
return api_v1.InvokeBindingResponse(data=resp_data, metadata=metadata)
)
# resp.headers includes the gRPC initial metadata.
# resp.trailers includes that gRPC trailing metadata.
Args:
store_name (str): store name to get secret from
key (str): str for key
secret_metadata (Dict[str, str], Optional): metadata of request
metadata (MetadataTuple, optional): custom metadata
Returns:
:class:`GetSecretResponse` object with the secret and metadata returned from callee
"""
req = api_v1.GetSecretRequest(
store_name=store_name,
key=key,
metadata=secret_metadata)
response, call = self._stub.GetSecret.with_call(req, metadata=metadata)
return GetSecretResponse(
secret=response.data,
headers=call.initial_metadata(),
trailers=call.trailing_metadata())
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))
print('Deleted!')
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
print(response.content_type)
print(response.data.value)
channel.close()
data (bytes or :obj:`google.protobuf.message.Message`): bytes or Message for data
which will send to id
metadata (tuple, optional): custom metadata
http_verb (str, optional): http method verb to call HTTP callee application
http_querystring (tuple, optional): the tuple to represent query string
Returns:
:class:`InvokeServiceResponse` object returned from callee
"""
req_data = InvokeServiceRequestData(data, content_type)
http_ext = None
if http_verb:
http_ext = self._get_http_extension(http_verb, http_querystring)
req = api_v1.InvokeServiceRequest(
id=id,
message=common_v1.InvokeRequest(
method=method,
data=req_data.data,
content_type=req_data.content_type,
http_extension=http_ext)
)
response, call = self._stub.InvokeService.with_call(req, metadata=metadata)
return InvokeServiceResponse(
response.data, response.content_type,
call.initial_metadata(), call.trailing_metadata())
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):
response.data.Unpack(res)
print("test", flush=True)
# Print Result
print(res, flush=True)
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))
print('Deleted!')
channel.close()