gpt4 book ai didi

python - webhook 可以直接针对 GCP PubSub 吗?

转载 作者:行者123 更新时间:2023-12-04 15:20:13 25 4
gpt4 key购买 nike

我们正在使用网络钩子(Hook)来监听 Jira 问题事件。每当票证上发生事件时,JIRA API 都会通知在 Google Cloud Function 中运行的 HTTP 端点。在此 Cloud Function 中,我们只是通过以下方式将请求原样转发到 Pub/Sub:

def forward_to_pubsub(request):

publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path(project_id, topic_id)

# Request body contains user, project, issue basically everything we care about
jira_body = request.json
publisher.publish(topic_path, data=json.dumps(jira_body).encode('utf-8'))

这似乎是一个不必要的跃点。有没有办法将 Pub/Sub 配置为 HTTP webhook 的目标?无法从文档中找出任何一种方法。

最佳答案

实际上可以使用 REST API 通过 HTTP 直接将消息发布到 Pub/Sub 主题。来自 https://cloud.google.com/pubsub/docs/publisher#rest 的快速入门指南-

To publish a message, send a POST request like the following:

POST  https://pubsub.googleapis.com/v1/projects/PROJECT_ID/topics/TOPIC_ID:publish
Authorization: Bearer $(gcloud auth application-default print-access-token)

Replace the following:

  • PROJECT_ID:主题项目的项目ID
  • TOPIC_ID:主题ID

Specify the following fields in the request body:

{
"messages": [
{
"attributes": {
"KEY": "VALUE",
...
},
"data": MESSAGE_DATA,
}
]
}

Replace the following:

  • KEY:消息属性的键
  • VALUE:消息属性的键值
  • MESSAGE_DATA:带有消息数据的 base64 编码字符串

If the request is successful, the response is a JSON object with the message ID. The following example is a response with a message ID:

{
"messageIds": [
"19916711285"
]
}

After you publish a message, the Pub/Sub service returns the message ID to the publisher.

还有一个REST API reference和一个 overview of service endpoints and common instructions在使用 Google REST API 时应该会有帮助。

请注意,发布请求必须确认 projects.topic.publish method specification因此仍然需要从 JIRA webhook 通知进行转换。

关于python - webhook 可以直接针对 GCP PubSub 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63474610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com