使用Python API将主体添加到云调度程序请求中

问题描述 投票:1回答:1

我要提出的问题是: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方法)吗?

python-3.x google-cloud-platform cron google-api-python-client google-cloud-scheduler
1个回答
0
投票
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)
© www.soinside.com 2019 - 2024. All rights reserved.