Reputation: 113
My application generates a UUID when I open a form and on submit it makes a POST call to my api passing this value which creates an event in my app with additional properties. I then want to call my events API passing the UUID value so my api will return a response where I will be able to see more properties.
I am receiving a 202 status code but on the response from my GET call I don't see the record. My guess was I wasn't waiting for the response but I was under the impression python requests will wait then execute the rest of the code.
I can make a call using postman passing the same values as in the GET call and I receive a response.
How can I ensure my app has waited enough time? I have tried sleep(5)
which should be more than enough time but it till shows 0 records.
import requests, uuid, json, pymysql, datetime, time
from datetime import datetime
from random import randint
from time import strftime, sleep
from flask import Flask, render_template, flash, request, redirect, url_for, jsonify
from wtforms import Form, TextField, TextAreaField, validators, \
StringField, SubmitField
DEBUG = True
app = Flask(__name__)
app.config.from_object(__name__)
app.config['SECRET_KEY'] = 'xxxxxxxxxxxxxxxxx'
class ReusableForm(Form):
###
@app.route('/alert', methods=['GET', 'POST'])
def alert():
form = ReusableForm(request.form)
incident = uuid.uuid4()
time = strftime('%Y-%m-%d %H:%M')
if request.method == 'POST':
impact = request.form['impact']
if form.validate():
write_to_disk(impact)
flash('{} {}'.format(impact))
url = "https://api/v1/"
payload="{\"incident\":\"" + str(incident) + "\", \"Impact\":\"" + str(impact) + "\"}"
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
if response.status_code == 202:
sleep(3)
inc_id = str(incident)
print ("This is the second call")
print (inc_id)
url = "https://api/v1/events/" + inc_id + ",&embed=properties"
print (url)
payload={}
headers = {
'Authorization': 'Basic xxxxxxxxxxxxxxxxxxxxxxxxxx='
}
response = requests.request("GET", url, headers=headers, data=payload)
response_dump = json.dumps(response.text, sort_keys = True, indent = 4, separators = (',', ': '))
print (response_dump)
return redirect(url_for('updated'))
Upvotes: 2
Views: 2087
Reputation: 146
Don't have enough reputation to write a comment. Can you add a code for your GET endpoint in api? Cause I see that there is also some get parameters (by the way, ,&embed=properties
should be changed to ?embed=properties
.
And one advice:
You can pass your payload
as a dict if you use json keyword argument. It will add appropriate header. So your code will look like that:
url = "https://api/v1/"
# usual dict now
payload={
"incident" : str(incident),
"Impact": str(impact)
}
response = requests.request("POST", url, json=payload)
Also: no need to pass any data=payload
to your GET request
Upvotes: 1