webhook 可以直接针对 GCP PubSub 吗?
Can webhook target GCP PubSub directly?
我们正在使用网络钩子来监听 Jira 问题事件。任何时候 activity 票据发生时,JIRA API 都会通知 Google 云函数内的 HTTP 端点 运行。在此 Cloud Function 中,我们只是将请求原封不动地转发到 Pub/Sub via:
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 的目标?无论哪种方式都无法从文档中找出答案。
不,这不可能。 Pub/Sub 主题没有可用作网络挂接的相应 HTTP 端点。如果要从 end-user 应用程序触发 Pub/Sub 主题,则需要在它们之间使用 front-end 服务器:
有关详细信息,请参阅 https://cloud.google.com/pubsub/docs/overview#endpoints。
实际上可以使用 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:消息属性的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.
在使用 Google REST API 时还有一个 REST API reference and an overview of service endpoints and common instructions 应该会有帮助。
请注意,发布请求必须确认 projects.topic.publish method specification,因此仍需要从 JIRA webhook 通知进行转换。
我们正在使用网络钩子来监听 Jira 问题事件。任何时候 activity 票据发生时,JIRA API 都会通知 Google 云函数内的 HTTP 端点 运行。在此 Cloud Function 中,我们只是将请求原封不动地转发到 Pub/Sub via:
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 的目标?无论哪种方式都无法从文档中找出答案。
不,这不可能。 Pub/Sub 主题没有可用作网络挂接的相应 HTTP 端点。如果要从 end-user 应用程序触发 Pub/Sub 主题,则需要在它们之间使用 front-end 服务器:
有关详细信息,请参阅 https://cloud.google.com/pubsub/docs/overview#endpoints。
实际上可以使用 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:消息属性的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.
在使用 Google REST API 时还有一个 REST API reference and an overview of service endpoints and common instructions 应该会有帮助。
请注意,发布请求必须确认 projects.topic.publish method specification,因此仍需要从 JIRA webhook 通知进行转换。