通过 PUT 请求更新数据 [英] Data update though PUT request

查看:54
本文介绍了通过 PUT 请求更新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将以下数据(示例数据)处理到 API,但是在发送此数据时,我想将状态从就绪"更新为完成",因为下次运行时,应排除这些收件人和新收件人将以就绪"状态提供.

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)

上面的代码运行良好,成功发送响应.现在我们正在处理三个字段(lot_number、recipients.status、recipients.account),但是我只需要处理recipients.status、recipients.account,并且recipients.status 应该更新为完成".

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'.

例如,假设我们有 100 批数据,每批有 50 个收件人,我想在每个循环中将状态更新为完成".即使响应在过程中失败(处理了 20 批并失败),我也想更新所有已处理批次(前 20 批)的状态.

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).

既然我们可以使用put更新数据,那么我们可以在响应和处理中对data=json.dumps(final_data)做任何更改吗?

Since we can update the data using put, can we do any changes in data=json.dumps(final_data) in response and process?

提前感谢您的帮助.

推荐答案

你可以关注这个:

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)

这篇关于通过 PUT 请求更新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆