python 调用teambtion对外api整理需求

需求:整理每个迭代内,测试人员做了多少任务,涉及的故事点多少,方便查看整理
技术:
后端:djargo+teambtion的对外api(对外api地址:https://open.teambition.com/help/folders/5d89d63c8acc9d00143ac725)
前端:layui+js+echarts(画图js)

设计思路:
1.拿到公司项目的token,使用免登录场景,就需要去下载专门的sdk去生成token,由于是用python语言,需要去下载专门的jwt包,pip install pyjwt,具体代码如下
	def get_token():
    """
    生成Token
    :return:
    """
    app_id = '应用的唯一 ID'
    # app_secret = '6xZ2sP9VA7IOOspKkzVObAC2ZmLJsbHb'
    jwt_headers = {
        "typ": "JWT",
        "alg": "HS256"
    }
    salt = “应用d的secret”

    exp = int(time.time() + 43200)
    payload = {
        "_appId": app_id,
        "iat": int(time.time()),
        "exp": exp
    }
    token = jwt.encode(payload=payload, key=salt, algorithm='HS256', headers=jwt_headers)
    print(token)
    return token
2.获取项目信息:
	def getProject(token):
    '''
    :param token:
    :return:
    '''
    url='https://open.teambition.com/api/project/query'
    headers = {
        "Authorization": "Bearer " + token,
        "X-Tenant-Id": "企业 ID",
        "X-Tenant-Type": "organization"
    }
    body = {
        "name": “项目名称
    }

    res = requests.post(url=url, headers=headers,json=body)
    projectId=(res.json().get("result"))[0].get("projectId")
    print(projectId)
    return projectId

2.获取项目下的所有成员,返回固定的测试人员的列表
	def get_project_member_list(token,projectId):
    '''
    获取成员列表
    :param token:
    :return:
    '''
    url='https://open.teambition.com/api/project/member/list'
    headers = {
        "Authorization": "Bearer " + token,
        "X-Tenant-Id": “企业”id,
        "X-Tenant-Type": "organization"
    }
    body = {
       "projectId": projectId,
       "pageSize": 100
    }

    res = requests.get(url=url, headers=headers, params=body)
    userList=[]
    # newuser_list=[]
    for r in (res.json()["result"]):
        dic={}
        if r.get("name")==‘测试人员name’:
            dic["name"]=r.get("name")
            dic["userId"]=r.get("userId")
            userList.append(dic)
        elif r.get("name")=='测试人员name':
            dic["name"] = r.get("name")
            dic["userId"] = r.get("userId")
            userList.append(dic)
        elif r.get("name")=='测试人员name':
            dic["name"] = r.get("name")
            dic["userId"] = r.get("userId")
            userList.append(dic)

    print(userList)
    return userList
3.获取迭代版本列表:
	def get_sprint_query(token,projectId):
    '''
    获取迭代列表,返回list
    projectId 项目id
    :param token
    :return:
    '''
    url='https://open.teambition.com/api/sprint/query'
    headers = {
        "Authorization": "Bearer " + token,
        "X-Tenant-Id": “企业id”,
        "X-Tenant-Type": "organization"
    }
    body = {
       "projectId": projectId,
       "pageSize": 100
    }

    res = requests.post(url=url, headers=headers, json=body)
    res_list=(res.json()["result"])
    sprint_list=[]
    for r in res_list:
        dic={}
        dic["name"]=r.get("name")
        dic["sprintId"]=r.get("sprintId")
        sprint_list.append(dic)
    print(sprint_list)
    return sprint_list
4.获取具体的需求信息,由于获取自定义字段缺少id,故用了简单粗暴的方式,直接使用爬虫调用获取需求id详情接口:
	def get_task_desc(taskId):
    '''
    获取任务详情
    :param taskId:
    :return:
    '''
    url='https://www.teambition.com/api/tasks/%s'%taskId
    
    cookie=‘cookie’(f12抓取)
    headers={}
    headers["cookie"]=cookie
    res=requests.get(url=url,headers=headers)
    res_json= res.json()
    return res_json
5.存库信息:
	class Teambition(models.Model):
    '''
        tb需求信息
    '''
    taskId=models.CharField(verbose_name='任务id',max_length=45)
    projectId = models.CharField(verbose_name='项目Id',max_length=45)
    tasklistId = models.CharField(verbose_name='任务列表Id',max_length=45)
    involveMembers=models.CharField(verbose_name='参与者',max_length=200)
    isDone = models.BooleanField(verbose_name='是否完成')
    content= models.CharField(verbose_name='需求名称',max_length=256,default={})
    storyPoint=models.CharField(verbose_name='故事点',max_length=10)
    taskflowstatus=models.JSONField(verbose_name='工作流信息')
    customfieldId=models.JSONField(verbose_name='协作点')
    subtasks = models.JSONField(verbose_name='子任务', max_length=256,default={})
    sprint=models.CharField(verbose_name='所属版本',max_length=40)