Gmail Pubsub - Infinite Push Notifications

故事扮演 提交于 2020-12-13 03:25:51

问题


I'm trying to setup a flask endpoint so that whenever it receives a Gmail push notifications from PubSub, it sends a post request to another API/endpoint.

However upon running the server, the process runs infinitely many times i.e. (receive notification, sends POST request to another API, returns 200 to PubSub)

import Flask
import requests

app = Flask(__name__)

# my endpoint receiving notifications from PubSub
@app.route('/pubsub_notification_receiver')    
def receiver():

    if request.method == 'POST':

        json_body = grab_latest_email()

        # send post request to another API
        response = requests.post(url, json_body)
        print(response.status) # returns OK/200
            
        return 'success'

This leads to infinite notifications from PubSub, and subsequently infinite post requests to the API ie. infinite_loop(receive new PubSub post request, app sends own POST request to API).

Each request is almost instant one after another, so I'm not sure if sending the post request somehow returns a post request that just triggers the receiver again.

If I remove the post request:

    if flask.request.method == 'POST':

        json_body = grab_latest_email()

        # send post request to another API
        # response = requests.post(url, json_body)
        # print(response.status) # returns OK/200
            
        return 'success'

There's no loop and the PubSub notification works as intended.

Thank you

来源:https://stackoverflow.com/questions/64764967/gmail-pubsub-infinite-push-notifications

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!