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

查看:231
本文介绍了通过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,receivers.status,receivers.account),但是我只需要处理receives.status,receivers.account,并且receives.status应该更新为"done".

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?

谢谢您的帮助.

推荐答案

您可以按照以下步骤操作:

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)

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

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