Google Cloud PubSub - 列出自定义属性的更好方法?

Google Cloud PubSub - better way to list custom attributes?

我正在尝试简化将数据发布到 PubSub 的 Python 代码。这有效:

import os
from google.cloud import pubsub_v1
import json

credentials_path = '/path/to/my/service.account.privateKey.json'
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credentials_path

publisher = pubsub_v1.PublisherClient()
# topic_path = publisher.topic_path(project_id, topic_id)         # this is the same as writing the string 'projects/{projectId}/topics/{topicId}'
topic_path = 'projects/MY_PROJECT/topics/MY_TOPIC'

data = 'Sensor data ready!'
data = data.encode('utf-8')                                     # data needs to be a bytestring
future = publisher.publish(topic_path, data, sensorName='garden', temperature='75.0', humidity='88.8')            # when you publish a message, the client returns a future
print(f'published message id {future.result()}')

但我希望有一种更优雅的方式来传递我的自定义属性。而不是像这样一一列出:

future = publisher.publish(topic_path, data, sensorName='garden', temperature='75.0', humidity='88.8')

...有没有办法按照以下方式做一些事情:

attributes = {
    'sensorName': 'garden',
    'temperature': '75.0',
    'humidity': '60'
}
future = publisher.publish(topic_path, data, attributes)

谢谢, 瑞安

正如@furas 上面提到的,所有需要做的就是解压传递给 publish(...)

attributes 对象
attributes = {
    'sensorName': 'garden',
    'temperature': '75.0',
    'humidity': '60'
}
future = publisher.publish(topic_path, data, **attributes)

谢谢, 瑞安