3

I'm new to Flask and working on creating a web application that will listen and monitor for status updates with callback urls from our API. If a particular event is performed, the application will download the files in a pdf format.

The application will use the user's integration key to access the management console. The app will route the user to a selection screen where the user will get to select between downloading options.

@app.route('/tool', methods=['GET', 'POST'])
def tool():

# Empty dict
agreement_list = {}

# Iterate through session cookies to extract out the name & agreement ID
for agreement in session['agreementID']['userAgreementList']:
    if agreement['esign'] == True and agreement['status'] != 'SIGNED':
        agreement_list[agreement['name']] = agreement['agreementId']

# If the user clicks the download button
if request.method == 'POST':

    # extract the data from the selection field
    session['config_selected'] = request.form.get('select_name')

    # Store into session
    session['agreements'] = agreement_list

    return redirect(url_for('scan'))

return render_template('tool.html', agreements=agreement_list)

Once the button is clicked, the app will route the user to a page where it will monitor for updates and download the file when there's an update.

@app.route('/scan', methods=['Get'])
def scan():

def createPDF(file_selected, agreementID, config_selected):
    """
    This sub function will create a pdf requested by the user. It will use an API call
    depending on which file is selected. It does both single and multiple pdf creations.
    :param page_id: A list of selected downloading option the user selected
    :param agreementID: Agreement ID that the user selected
    :param select: name of the selected file use to help name pdf
    :return: N/A
    """
    # Iterate through the list of selected options that the user selected
    for file in file_selected:

        # API request
        r = requests.get('Some request url from API' + agreementID + file,
                         headers=session['header'])

        file_name = config_selected + ' - ' + id[1:] + '.pdf'
        file_name = file_name.replace(' ', '_')

        # Write file to pdf
        with open(file_name, 'wb') as f:
            f.write(r.content)

    f.close()

    # call createPDF here


return render_template('scan.html')

Attempts

After researching around, I found that I should use sockets for communication between the server and the client. I tried to implement Flask-SocketIO and got it running.

@socketio.on('message')
def handleMessage(msg):
    print('Message: ' + msg)
    send(msg, broadcast=True)

Currently the application is running on my local machine and it's currently being able to do polling on that particular page.

$(document).ready(function () {
    var socket = io.connect('http://127.0.0.1:5000');

    socket.on('connect', function () {
        socket.send('User has connected!');
    });

    // socket.on('message', function () {
    //     socket.send('Scanning')
    // });

});

Question:

How would I get the live callback updates when the end user performs the task that I am looking out for? Do I have to connect the socket to the API server for the callbacks? Or is there another way I can possibly get the callback updates using the integration key since I have access to the management UI using HTTP request.

Nathan
  • 483
  • 4
  • 7
  • 19

1 Answers1

2

I finally figured out what was wrong and why I wasn't getting any event pings from the API server. It turns out that my localhost needed to be publicly accessible in order to receive event changes from the server. My solution was to us ngrok to create a secure tunnel from my localhost out to the public endpoint. Once that was achieved, I added the callback url, which in turn allowed the server to ping me with event changes.

Nathan
  • 483
  • 4
  • 7
  • 19