You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PagerDuty API团队查询代码优化与递归实现技术问询

关于PagerDuty API团队信息获取的规模化适配与优化疑问

我是编程新手,编写了调用PagerDuty API获取团队oncall信息的代码,将接口响应转为JSON后提取所需值存入字典。当前代码可正常运行,但需适配规模化场景并优化逻辑,具体疑问如下:

  • 当接口返回大规模响应时,现有循环的资源与时间效率如何?例如设置limit=10000会产生什么影响?
  • 是否存在更优的实现方案(参考pdpyras官方文档);
  • 如何通过递归调用函数,每次递增offset为10,直到找到名称匹配i['name'] == testteamnm的团队(避免不必要地拉取全部记录)。

现有代码

from pdpyras import APISession
session = APISession(PD_API_KEY, default_from="team@test.com")

total = 1 #true or false
limit = 10
offset = limit + 1
teams = session.get('/teams?limit={0}&total={1}'.format(limit,total))
testteamnm = "Test Team"
to_dict = dict()
if teams.ok:
    print(teams.json())
    tj = teams.json()['teams']

    for i in tj:
        if i['name'] == testteamnm:
            to_dict.update({i['id']:i['name']})
            break
    else:
        print("In the limit {} fetched from PD API, test team was not found. call this functtion recursively until team is found. Hence set offset to {} and fetch further".format(limit,offset))   

if __name__== "__main__":
    o = TEAMPDClass()
    o.pagerduty_oncall()

接口示例输出

{'teams': [{'id': 'PCBBSDD', 'name': '[obsolete] Old Survey API', 'description': None, 'type': 'team', 'summary': '[obsolete] GX Old Survey API', 'self': 'https://api.pagerduty.com/teams/PCZBSDD', 'html_url': 'https://team.pagerduty.com/teams/PCLBSDD', 'default_role': 'manager', 'parent': None}, {'id': 'PISLGYQ', 'name': 'Mobile Client', 'description': None, 'type': 'team', 'summary': 'Mobile Client', 'self': 'https://api.pagerduty.com/teams/PISGSYQ', 'html_url': 'https://team.pagerduty.com/teams/PIGCSYQ', 'default_role': 'manager', 'parent': None}, {'id': 'PAVY84Y', 'name': 'Shield Client', 'description': None, 'type': 'team', 'summary': 'Android Shield Client', 'self': 'https://api.pagerduty.com/teams/PAXY84Y', 'html_url': 'https://team.pagerduty.com/teams/PAGT84Y', 'default_role': 'manager', 'parent': None}], 'limit': 3, 'offset': 0, 'total': 1001, 'more': True}

内容的提问来源于stack exchange,提问作者AhmFM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 05:45:33