如何创建 python webhook 发件人应用程序?

问题描述

这是此帖子的后续问题.

我有一个通过 xxx.comdata API 端点公开的数据仓库表

I have a data warehouse table exposed via xxx.comdata API endpoint

我一直在使用以下代码查询该表并将其解析为如下数据框;

I have been querying this table using the following code and parsing it into a dataframe as follows;

import requests
import json
import http.client
import pandas as pd

url = "xxx.comdata?q=Active%20%3D1%20and%20LATITUDE%20%3D%20%20%220.000000%22%20and%20LONGITUDE%20%3D%20%220.000000%22&pageSize =300"
payload = {}
headers = {'Authorization': access_token}
response = requests.request("GET", url, headers=headers, data = payload)
j=json.loads(response.text.encode('utf8'))
df = pd.json_normalize(j['DataSet'])

仓库表会定期更新,我需要创建一个 webhook 以供以下 Azure httptrigger 监听;

The warehouse table gets periodically updated and I am required to create a webhook to be listened to by the following Azure httptrigger;

import logging
import os
import json
import pandas as pd
import azure.functions as func

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    d={
    'Date' :['2016-10-30','2016-10-30','2016-11-01','2016-10-30'],
    'Time':['09:58:11', '10:05:34', '10:07:57', '11:15:32'],
    'Transaction':[2,3,1,1]
    }
    df=pd.DataFrame(d, columns=['Date','Time','Transaction'])
    output = df.to_csv (index_label="idx", encoding = "utf-8")
 
return func.HttpResponse(output)

运行时,httptrigger 成功侦听了我创建并在我的磁盘上本地运行的以下 webhooker 发送器.

When run,the httptrigger successfully listens to the following webhooker sender which I have created and am running locally on my disk.

    import logging
    import os
    import json
    import pandas as pd

data={'Lat': '0.000000',
   'Long': '0.000000',
   'Status': '1', 'Channel URL':"xxx.comdata"}

webhook_url="http://localhost:7071/api/HttpTrigger1"

r=requests.post(webhook_url, headers={'Content-Type':'application/json'}, data =json.dumps(l))

我的问题是;

  1. 如何将 webhook 发送器作为应用程序部署到云中,以便每次xxx.comdata"使用 Lat==0Long=00Status=1 进行更新,是否向我的 webhook 侦听器发送了一条消息?
  1. How can I deploy the webhook sender to the cloud as an app so that every time "xxx.comdata" is updated with Lat==0,Long=00 and Status=1, a message is send to my webhook listener?

该应用可以是 Azure/Flask/postman 或任何其他基于 python 的 webhook 构建器.

The app can either be Azure/Flask/postman or any other python based webhook builder.


解决方案

  1. 您目前有一些轮询逻辑(在使用以下代码查询此表 下).如果您想将其移至云";然后 创建一个 TimerTrigger 函数,并将你所有的 poller 代码放入其中.

  1. You currently have some polling logic (under querying this table using the following code). If you want to move that to "Cloud" then create a TimerTrigger function, and put all your poller code in it.

如果您想保持该轮询器代码不变,但想在云"中调用某些代码;每当轮询器检测到更改(使用 Lat==0,Long=00 和 Status=1 更新),您就可以创建一个 HTTPTrigger 函数,并在检测到更改时从轮询器调用它.

If you want to leave that poller code untouched, but want to call some code in "cloud" whenever poller detects a change (updated with Lat==0,Long=00 and Status=1), then you can create an HTTPTrigger function and invoke it from poller whenever it detects the change.


令人困惑的部分是:您今天如何检测到这种变化?poller 代码托管在哪里,执行频率如何?


Confusing part is this: How do you detect this change today? Where is the poller code hosted and how often is it executed?

如果 DB 中的数据发生变化,那么只有您可以执行一些代码"的方法;每当数据发生变化时:

If data in DB is changing then only ways you can execute "some code" whenever the data changes is:

  1. 定期轮询数据库,比如每 1 分钟一次,如果有变化,则执行一些代码";或
  2. 此数据库的某些功能允许您配置一个 REST API(HTTP Webhook),只要有更改,该数据库就会调用该 API.实现一个 REST API(例如,作为一个 HttpTrigger 函数)并将一些代码"放在你想在里面执行.现在,只要有更改,数据库就会调用您的 webhook/REST-API 和一些代码".被执行.
  1. poll the DB periodically, say every 1 minute and if tehre is a change execute "some code" OR
  2. some feature of this DB allows you to configure a REST API (HTTP Webhook) that is called by the DB whenever there is a change. Implement a REST API (e.g. as an HttpTrigger function) and put that "some code" that you want executed inside it. Now whenever there is a change the DB calls your webhook/REST-API and "some code" is executed.

读取它的方法是调用 REST API (xxx.com/data?q=...),那么您可以检测到的唯一方法

and the way to read it is to call a REST API (xxx.com/data?q=...) then the only ways you can detect

相关文章