Reputation: 185
I am processing the below data (sample data) to API however while sending this data, i want to update the status to 'done' from 'ready' since next time when it runs, these recipients should be excluded and new recipients will available with 'ready' status.
data = [{'lot_number': 'a53f-8fb40cabab7e',
'recipients':
[{'status': 'ready', 'account': '1001'},
{'status': 'ready', 'account': '1002'}]},
{'lot_number': 'ad3d-a0849d5c7c7a',
'recipients':
[{'status': 'ready', 'account': '1015'},
{'status': 'ready', 'account': '1019'},
{'status': 'ready', 'account': '1023'}]}]
for final_data in data:
batch = final_data.get("lot_number")
url = "https://ext-api-support-dev.llws.com/api/notify/"+ batch
response = requests.put(url, data=json.dumps(final_data), headers=headers)
The above code is working perfectly and sending the response successfully. Right now there are three fields (lot_number, recipients.status, recipients.account) that we are processing however i need to process only recipients.status, recipients.account only and recipients.status should be updated to 'done'.
For example let's assume that we have 100 lots in data with 50 recipients in each lot, i want to update the status to 'done' in each loop. Even though if response gets failed in the middle of process (20 lots processed and got failed) i want to update the status for all processed batches (first 20 lots).
Since we can update the data using put, can we do any changes in data=json.dumps(final_data) in response and process?
Thanks for your help in advance.
Upvotes: 1
Views: 1091
Reputation: 7419
You can follow this:
done_lots = list()
url = "https://ext-api-support-dev.llws.com/api/notify/{0}"
for final_data in data:
batch = final_data.get("lot_number")
# Remove the lot_number
request_data = {k: v for (k, v) in final_data.items() if k != "lot_number"}
response = requests.put(url.format(batch), data=json.dumps(request_data), headers=headers)
# Loop over recipients and update status
# THIS DOES NOT UPDATE TO THE DATABASE. IT ONLY UPDATES THE final_data variable
for recipients in final_data.get("recipients"):
recipients["status"] = "done"
done_lots.append(final_data)
Upvotes: 1