-1

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:

  1. Make a POST request while redirecting in flask
  2. Display data streamed from a Flask view as it updates
  3. Update and render a value from Flask periodically
  4. Execute a function after Flask returns response
SamAct
  • 529
  • 4
  • 23
  • This sounds like it would be best handled by your front end with some JavaScript to update the value, instead of reloading the entire page. – noslenkwah Sep 16 '21 at 22:03
  • JS will not help if i call an api in /index route – SamAct Sep 16 '21 at 22:10
  • 1
    Maybe I don't understand the question because I don't see how calling an API in `index` would preclude updating values with JavaScript. – noslenkwah Sep 16 '21 at 22:15
  • values in `index` only gets updated only if its loaded, i dont see how i can get the values without loading `index`. Also not an expert in JS – SamAct Sep 16 '21 at 22:22

1 Answers1

0

I am going with While Loop to check if cache values have changed from the previous stored once for now, until a better method is available.

SamAct
  • 529
  • 4
  • 23