體驗webhooks

無風聽海發表於2021-11-12

一、webhooks是什麼

webhooks是一種實現在web api跟web service之間的釋出訂閱的輕量級的模式;當服務中心某個事件發生的時候,就會向訂閱者傳送一個POST請求形式的通知,這個POST請求中會包含事件的相關資訊。

webhooks是一種與外部系統進行互動的簡單的輕量級的方式,目前已經有Dropbox、Github、PayPal等很多服務提供了webhooks功能。

二、體驗Github中的webhooks

在Github中,我們可以在組織機構、程式碼倉庫、Github App上設定webhooks;當我們訂閱的事件放生的時候,Github會傳送一個http post請求到配置的URL,從而可以觸發CI builds、更新備份映象、部署程式碼等。

配置訂閱web伺服器

web伺服器主要用於監聽Github的push事件,並返回傳入的json資料

from flask import Flask, request
import hashlib
import hmac

app = Flask(__name__)

@app.route("/push", methods=["POST"])
def  push():
    secret = 'github_webhooks'
    from_signature = request.headers.get('X-Hub-Signature-256')
    cal_signature = 'sha256=' + hmac.new(str.encode(secret), request.data, digestmod=hashlib.sha256).hexdigest()
    if cal_signature == from_signature:
        return 'True'
    else:
        return 'False'


啟動web伺服器
mango@mango-ubuntu:~/文件/blogs/webhook$ export FLASK_APP=hooks
mango@mango-ubuntu:~/文件/blogs/webhook$ flask run
 * Serving Flask app "hooks"
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

開放本地web server to internet

下載ngrok並解壓之後執行,完成公網URL和本地URL的對映

ngrok http 5000

ngrok by @inconshreveable                                                                              (Ctrl+C to quit)
                                                                                                                       
Session Status                online                                                                                   
Session Expires               1 hour, 42 minutes                                                                       
Version                       2.3.40                                                                                   
Region                        United States (us)                                                                       
Web Interface                 http://127.0.0.1:4040                                                                    
Forwarding                    http://fde0-110-251-30-176.ngrok.io -> http://localhost:5000                             
Forwarding                    https://fde0-110-251-30-176.ngrok.io -> http://localhost:5000                            
                                                                                                                       
Connections                   ttl     opn     rt1     rt5     p50     p90                                              
                              2       0       0.00    0.00    0.01    0.01 
配置webhooks

將得到的外網URL填寫到Payload URL中

http://fde0-110-251-30-176.ngrok.io/push

ContentType選擇 application/json

為了提高安全性,填寫Secret,其他保持預設設定儲存即可

測試webhooks

點選最左側的Webhooks選項,在右邊就可以看到新增的webhooks裡邊,點進去之後,選擇上方的Recent Deliveries標籤頁;

在裡邊可以看到每次的觸發記錄,點進去可以看到請求和回應的header及傳送的資料,當然你也可以redelivery

request header

Request URL: http://fde0-110-251-30-176.ngrok.io/push
Request method: POST
Accept: */*
content-type: application/json
User-Agent: GitHub-Hookshot/945516b
X-GitHub-Delivery: 034ea5a0-429c-11ec-9315-aa9e0e849aaa
X-GitHub-Event: ping
X-GitHub-Hook-ID: 327865514
X-GitHub-Hook-Installation-Target-ID: 357383239
X-GitHub-Hook-Installation-Target-Type: repository
X-Hub-Signature: sha1=fb0aba301c9af7463cc7e3ff7be0a1f9dccd1938
X-Hub-Signature-256: sha256=10c1e266aabfa7ce519ceb0c543988f6d19d12b16e5f336a54367682319f1111

response header

Content-Length: 6728
Content-Type: application/json
Date: Thu, 11 Nov 2021 03:06:05 GMT
Server: Werkzeug/2.0.1 Python/3.9.5

相關文章