Getting started with the Dapr client Python SDK

How to get up and running with the Dapr Python SDK

The Dapr client package allows you to interact with other Dapr applications from a Python application.

前提

Import the client package

The dapr package contains the DaprClient which will be used to create and use a client.

  1. from dapr.clients import DaprClient

构建块

The Python SDK allows you to interface with all of the Dapr building blocks.

调用服务

  1. from dapr.clients import DaprClient
  2. with DaprClient() as d:
  3. resp = d.invoke_service(id='service-to-invoke', method='method-to-invoke', data='{"message":"Hello World"}')

保存 & 获取 应用程序状态

  1. from dapr.clients import DaprClient
  2. with DaprClient() as d:
  3. # Save state
  4. d.save_state(store_name="statestore", key="key1", value="value1")
  5. # Get state
  6. data = d.get_state(store_name="statestore", key="key1").data
  7. # Delete state
  8. d.delete_state(store_name="statestore", key="key1")

发布消息

  1. from dapr.clients import DaprClient
  2. with DaprClient() as d:
  3. resp = d.publish_event(pubsub_name='pubsub', topic='TOPIC_A', data='{"message":"Hello World"}')

与输出绑定交互

  1. from dapr.clients import DaprClient
  2. with DaprClient() as d:
  3. resp = d.invoke_binding(name='kafkaBinding', operation='create', data='{"message":"Hello World"}')

检索密钥

  1. from dapr.clients import DaprClient
  2. with DaprClient() as d:
  3. resp = d.get_secret(store_name='localsecretstore', key='secretKey')

相关链接