The concepts of webhooks are quite simple, yet I am here for some guidance on how a realtime change in POST request is handled. Consider a webhook program in FLASK:
@app.route('/webhook', methods = ['POST'])
def webhook():
data = json.loads(request.data)
return data
Such a program will easy return a json POST request. And result will look like this:
{
"passphrase": "SkZSV1YwZz0=",
"timenow": "2021-09-15T21:18:00Z",
"item": "apple",
"quantity": "50",
"account": "9999"
}
Now I have successfully read in the webhook data. Next step is to do something with the data.
Say I would like to search my database 'df'
for item: apple
and return the price.
def get_item_price(df, item_name):
# Returns price by filtering df.
df = df[df['name'] == item_name]
df.reset_index(inplace = True)
price = df['price']
return price
So, price_of_item = get_item_price(df, item_name)
can now be used. But since webhook is a POST request I am using cache to access values from other routes.
cache.set("price_data", price_of_item )
I can get the price of the apple by calling cache from another route. Say:
@app.route("/index")
def index():
price_data_recieved= cache.get("price_data")
# do some stuff based on price_data_recieved, and call a api
return index_template.format(message = price_of_item)
index_template = """
<div> the price of apple is: {price_data_recieved}{some_other_stuff} </div>
"""
However this is not real time, I need to reload the browser every time. Can I make changes to this such that as a new POST request changes my index
route output as well. I assume its like a ping to /index
and initiate a task.
Resources explored unsuccessfully: