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

Gdf*_*elt 3 cron python-3.x google-api-python-client google-cloud-platform google-cloud-scheduler

我正在扩展这个问题:How to create a job with Google Cloud Scheduler 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 方法)?

Dus*_*ram 6

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)
Run Code Online (Sandbox Code Playgroud)