我正在编写一些通过 boto3 与 AWS API 交互的代码,我所做的调用具有很大的响应负载,并且需要一段时间来处理,在开发时测试代码变得非常令人沮丧和不切实际。对我来说缓存某个 api 调用的响应最实用的方法是什么,如下所示?
#!/usr/bin/python3.6
import boto3
client_cw = boto3.client('cloudwatch')
paginator = client_cw.get_paginator('describe_alarms')
for response in paginator.paginate():
print(response)
任何帮助将不胜感激。
最佳答案
这是使用 unittest
的 mock
功能的绝佳情况。这允许您重新定义整个对象或对象的特定行为。使用此方法的解决方案分为两部分:
您正在为
get_paginator()
函数定义自定义行为,因此您将模拟boto3.client.get_paginator()
方法以返回一个简单的自定义分页器类。您还需要定义一个简单的自定义 Paginator 类,该类仅定义一个方法,即您在代码中调用的方法,即
paginate()
,以及何时调用该方法,它应该返回一个硬编码的分页列表(JSON 格式),反射(reflect)您期望 AWS 返回的任何内容。
修改后的代码如下:
#!/usr/bin/python3.6
import boto3
from unittest import mock
client_cw = boto3.client('cloudwatch')
# ---
# This is where the mocking starts
with mock.patch("boto3.client") as client:
# Create a mock paginator to return mock pages
class MockPaginator(object):
def paginate(self):
# Customize this to whatever the "real"
# paginated list from AWS looks like
return [
{
"AlarmsList": [
{
"Name": "my_alarm_1"
},
{
"Name": "my_alarm_2"
}
]
}
]
# Tell the get_paginator method to return a fake Paginator
client.get_paginator.return_value = MockPaginator()
# This is where the mocking ends
# (but we're still in the mocking context)
# ---
# And now on with the show...
paginator = client.get_paginator('describe_alarms')
for response in paginator.paginate():
print(response)
现在,当您调用 paginator.paginate()
时,它将返回您在自定义 Paginator 类中提供的自定义硬编码 JSON 列表,而不是尝试调用 AWS。
关于python - 如何在 python 中缓存 boto3 API 调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57436085/