python - Python - Mailchimp 批量 PUT 请求
问题描述
我正在尝试使用 Mailchimp API 的批处理功能。我目前的设置是这样的
operations = []
for idx, row in users_df.iterows():
payload = {
'email': row['email'],
'last_updated': row['new_time'],
'custom_value': row['new_value']
}
operation_item = {
"method": "POST", # I changed this to PUT
"path": '/lists/members/12345',
"body": json.dumps(payload),
}
operations.append(operation_item)
client = mc.MailChimp(MAILCHIMP_TOKEN, MAILCHIMP_USER)
batch = client.batches.create(data={"operations": operations})
每当我使用 POST 方法时,我都会收到此错误:old_user@gmail.com is already a list member. Use PUT to insert or update list members.
但是每当我将方法更改为 PUT 时,我都会收到另一个错误:The requested method and resource are not compatible. See the Allow header for this resource's available methods.
解决方案
您不能对列表资源执行 PUT 操作"/lists/members/12345"
,您需要将路径更改为"/lists/members/12345/{email}"
此代码适用于我们:
def add_users_to_mailchimp_list(list_id, users):
operations = []
client = get_mailchimp_client()
for user in users:
member = {
'email_address': user.email,
'status_if_new': 'subscribed',
'merge_fields': {
'FNAME': user.first_name or '',
'LNAME': user.last_name or '',
},
}
operation_item = {
"method": "PUT",
"path": client.lists.members._build_path(list_id, 'members', user.email),
"body": json.dumps(member),
}
operations.append(operation_item)
return client.batches.create(data={"operations": operations})
我猜使用受保护的方法client.lists.members._build_path
有点hacky,如果你喜欢你可以手动构建url,它将是f'lists/{list_id}/members/{user.email}'
推荐阅读
- javascript - 材质组件web组件mwc-按钮上传文件
- python - 创建 Python 管道以转换数据
- dpdk - 如何修复 dpdk 构建错误:“需要 isa 选项 -m32 -mssse3”
- css - 在 Visual Studio 2019 中编辑 CSS 文件时,有没有办法查看类是否是媒体查询的一部分?
- java - Java新手(手写笔记:Implements vs Extends)
- google-cloud-storage - 通过 Google 存储提供压缩文件
- .net - 如何在 gitlab 的 .net 解决方案构建管道中运行 git 命令
- r - 在因子水平上迭代 R 函数
- angular - 输入字段未在 Angular 中重置其内容
- python - 无法在 Tensorflow 中输入形状的值