使用 Python API 将正文添加到云调度程序请求

add body to a cloud scheduler request using the Python API

我正在扩展这个问题:

我想知道如何插入要与函数一起传递的正文对象,我可以通过 gcloud 完成,并且根据 v1 文档我知道需要传递正文在 HttpTarget 中,每当我尝试以这种方式传递它时,它都会出错并说:

TypeError: No positional arguments allowed

老实说,我根本无法 from google.cloud.scheduler_v1.types import HttpTarget as Target 工作。

有人能给我一个例子吗,他们成功地使用 API 在 Cloud Scheduler 中创建了一个作业,并发送了一个正文(JSON 对象)(POST 方法当然)?

import json

from google.cloud import scheduler_v1

client = scheduler_v1.CloudSchedulerClient()

project = "..."  # TODO
location = "..."  # TODO
parent = client.location_path(project, location)

uri = "..."  # TODO
body = {"Hello": "World"}

job = {
    "http_target": {
        "http_method": "POST",
        "uri": uri,
        "headers": {"Content-Type": "application/json"},
        "body": json.dumps(body).encode("utf-8"),
    },
    "schedule": "* * * * *",
}

response = client.create_job(parent, job)

print(response)