如何为动态URL列表创建蝗虫任务?

时间:2019-01-30 22:58:13

标签: python python-3.x python-3.6 locust

我正在尝试基于OpenAPI JSON获取所有 API端点的计时信息,因此我想在setup方法中执行以下操作:

for api_path in self.client.get('/api/doc/?format=openapi').json()['paths'].keys():
    self.get_[api_path] = [@task decorated method which retrieves the API path]

还是有另一种在运行时生成任务的方法?

1 个答案:

答案 0 :(得分:0)

API docs中找到了它:

def setup(self) -> None:
    for api_path in self.client.get('/api/doc/?format=openapi').json()['paths'].keys():
        self.schedule_task(self.api_task, args=[api_path])

def api_task(self, path: str) -> None:
    response = self.client.get("/api{}?limit=1000".format(path))
    assert response.status_code == HTTPStatus.OK, response.content.decode()