1

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

mclovine
  • 71
  • 5
  • Can you explore more description in the question, discovering the `gmail-pub/sub` design as per your intention? – Nick_Kh Nov 10 '20 at 10:48
  • I assume that Flask [request](https://flask.palletsprojects.com/en/1.1.x/quickstart/#the-request-object) object holds specific attributes to access user data, just check out this [thread](https://stackoverflow.com/a/16664376/9928809) to get more details. – Nick_Kh Nov 10 '20 at 12:30
  • the Flask request object returns something like this: {"emailAddress": "user@example.com", "historyId": "9876543210"} As per PubSub doc: You can then use history.list() to get the change details for the user since their last known historyId, as per the sync guide. https://developers.google.com/gmail/api/guides/push – mclovine Nov 10 '20 at 12:45
  • Have you checked the link above about the Flask request object attributes? – Nick_Kh Nov 17 '20 at 12:20
  • @Nick_Kh thanks for the reply - I had a look at the link you shared, I can get the contents from the PubSub post request without issue. Problem is my own post request within the if statement - I made some changes to my code and edited my original question, if you can please take a look – mclovine Nov 18 '20 at 23:21
  • Have you tried to define `json_body` and `response` outside the `if` construction just before `if request.method == 'POST':`? – Nick_Kh Nov 23 '20 at 11:16
  • @mclovine any updates on this issue? What seemed to be causing the mentioned problem in your latest version of your original question? – NikolaS Aug 11 '23 at 10:42

0 Answers0