美文网首页
codebeamer接口初始化

codebeamer接口初始化

作者: 繁华丶凋零 | 来源:发表于2022-08-12 09:49 被阅读0次

实现机制:拓展了一下网上的 codebeamer 库

目录结构:

codebeamer
├── client.py
├── __init__.py
├── mixins
│   ├── __init__.py
│   └── project.py

1.codebeamer/init.py

from .client import Codebeamer

2.codebeamer/client.py

import requests, json
from .mixins.project import ProjectMixin,Trackers,Item,User,Group,Role
import os

class Codebeamer(ProjectMixin,Trackers,Item,User,Group,Role):

    def __init__(self, url, key):
        self.base_url = url
        self.headers = {
        "accept": 'application/json',
        "Content-Type" : 'application/json',
        "Authorization": key
        }

        self.headers2 = {
#        "Accept": "*/*",
#        "Accept-Encoding": "gzip, deflate",
#        "Accept-Language": "zh-CN,zh;q=0.9",
#        "Connection": "keep-alive",
        "Authorization": key
        }


    def get(self, uri, params={}):
        url = self.base_url + uri
        res = requests.get(url, headers = self.headers, params=params)
        if res.status_code == 200:
            return json.loads(res.content)
        else:
            print(f"Warning : GET error ({url}) as {res.content}")
            result = f"Warning : GET error ({url}) as {res.content}"
            return(json.dumps(result))

    def put(self, uri, data):
        url = self.base_url + uri
        res = requests.put(url, headers = self.headers, data=json.dumps(data))
        if res.status_code == 200:
            return json.loads(res.content)
        else:
            print(f"Warning : PUT error ({url}) as {res.content}")
            result = f"Warning : PUT error ({url}) as {res.content}"
            return(json.dumps(result))

    def put2(self, uri, data):
        url = self.base_url + uri
        res = requests.put(url, headers = self.headers2, files=data)
        if res.status_code == 200:
            return json.loads(res.content)
        else:
            print(f"Warning : PUT error ({url}) as {res.content}")
            result = f"Warning : PUT error ({url}) as {res.content}"
            return(json.dumps(result))


    def post(self, uri, data):
        url = self.base_url + uri
        print(data)
        res = requests.post(url, headers = self.headers, data=json.dumps(data))
        if res.status_code == 200:
            return json.loads(res.content)
        else:
            print(f"Warning : POST error ({url}) as {res.content}")
            result = f"Warning : POST error ({url}) as {res.content}"
            return(json.dumps(result))

    def post2(self, uri, data):
        url = self.base_url + uri
        res = requests.post(url, headers = self.headers2, files=data)
        if res.status_code == 200:
            return json.loads(res.content)
        else:
            print(f"Warning : POST error ({url}) as {res.content}")
            result = f"Warning : POST error ({url}) as {res.content}"
            return(json.dumps(result))

    def delete(self, uri, data):
        url = self.base_url + uri
        res = requests.delete(url, headers = self.headers2, files=data)
        if res.status_code == 200:
            return json.loads(res.content)
        else:
            print(f"Warning : delete error ({url}) as {res.content}")
            result = f"Warning : delete error ({url}) as {res.content}"
            return(json.dumps(result))




3.codebeamer/mixins/init.py

4.codebeamer/mixins/project.py

class ProjectMixin:

    def getProjects(self,params = {}):
        return self.get('/projects',params)

    def getProject(self,**kwargs):

        if not len(kwargs) == 1:
            raise Exception('getProject method takes one argument in : name, id')
        elif kwargs.get('name', None):
            item = {}
            for i in self.getProjects():
                if i['name'] == kwargs['name']:
                    item['id'] = i['id']
                    break
            return self.get(f"/projects/{item['id']}")
        elif kwargs.get('id', None):
            return self.get(f"/projects/{kwargs['id']}")
        else:
            raise Exception('getProject method takes one argument in : name, id')

    def getProjectTrackers(self,projectid: int,params = {}) ->dict:
        return self.get(f"/projects/{projectid}/trackers",params)

    def getProjectmembers(self,projectid: int,params = {}) ->dict:
        return self.get(f"/projects/{projectid}/members",params)

    def getProjectwikipages(self,projectid: int,params = {}) ->dict:
        return self.get(f"/projects/{projectid}/wikipages",params)

#creat_from_other_Tracker
    def createProjectTracker(self,projectid: int,data):
        return self.post(f"/projects/{projectid}/trackers",data)


class Trackers:
    def getTrackerItems(self, trackerId: int,params = {}) ->dict:
        return self.get(f"/trackers/{trackerId}/items",params)

    def getTrackerbaselines(self,trackerId: int,params = {}) ->dict:
        return self.get(f"/trackers/{trackerId}/baselines",params)

    def getTrackerfields(self,trackerId: int,params = {}) ->dict:
        return self.get(f"/trackers/{trackerId}/fields",params)

    def getTrackeroutline(self,trackerId: int,params = {}) ->dict:
        return self.get(f"/trackers/{trackerId}/outline",params)

    def getTrackerschema(self,trackerId: int,params = {}) ->dict:
        return self.get(f"/trackers/{trackerId}/schema",params)

    def getTrackertransitions(self,trackerId: int,params = {}) ->dict:
        return self.get(f"/trackers/{trackerId}/transitions",params)

    def getTrackertypes(self,params = {}) ->dict:
        return self.get(f"/trackers/types",params)

    def getTracker(self,trackerId: int) ->dict:
        return self.get(f"/trackers/{trackerId}")


class Item:
    def getItem(self, itemId: int) ->dict:
        return self.get(f"/items/{itemId}")

    def getItemschildren(self, itemId: int,params = {}) ->dict:
        return self.get(f"/items/{itemId}/children",params)

    def getItemsFields(self, itemId: int,params = {}) ->dict:
        return self.get(f"/items/{itemId}/fields",params)

    def getItemshistory(self, itemId: int,params = {}) ->dict:
        return self.get(f"/items/{itemId}/history",params)

    def getItemsrelations(self, itemId: int,params = {}) ->dict:
        return self.get(f"/items/{itemId}/relations",params)

    def getItemsreviews(self, itemId: int,params = {}) ->dict:
        return self.get(f"/items/{itemId}/reviews",params)

    def getItemstransitions(self, itemId: int,params = {}) ->dict:
        return self.get(f"/items/{itemId}/transitions",params)

    def createTrackerItem(self,trackerId,data):
        return self.post(f"/trackers/{trackerId}/items",data)

    def updateTrackerItem(self,itemId,data):
        return self.put(f"/items/{itemId}",data)

    def updateItemField(self,itemId,data):
        return self.put(f"/items/{itemId}/fields",data)

    def getItemscomments(self, itemId: int):
        return self.get(f"/items/{itemId}/comments")

    def createItemscomment(self, itemId: int,data):
        return self.post2(f"/items/{itemId}/comments",data)

    def updateItemscomment(self, itemId: int,commentid: int,data):
        return self.put2(f"/items/{itemId}/comments/{commentid}",data)

    def getItemscomment(self, itemId: int,commentid: int):
        return self.get(f"/items/{itemId}/comments/{commentid}")

    def queryItem(self,params):
        return self.get(f"/items/query",params)

    def deleteItem(self, itemId: int,params = {}) ->dict:
        return self.delete(f"/items/{itemId}",params)

    def addItemschildren(self, src_itemId: int, des_itemId: int) ->dict:
        #des_itemId 为父,src_itemId为子
        data = { "id":src_itemId,"version":0 }
        return self.post(f"/items/{des_itemId}/children",data)

class User:
    def getUsers(self,params = {}):
        return self.get(f"/users",params)

    def getUser(self,userid):
        return self.get(f"/users/{userid}")


class Role:
    def getRoles(self,params = {}):
        return self.get(f"/roles",params)

    def getRole(self,roleid):
        return self.get(f"/roles/{roleid}")

class Group:
    def getGroups(self,params = {}):
        return self.get(f"/users/groups",params)

    def getGroup(self,groupid):
        return self.get(f"/users/groups/{groupid}")

    def getGroupmembers(self,groupid):
        return self.get(f"/users/groups/{groupid}/members")



相关文章

网友评论

      本文标题:codebeamer接口初始化

      本文链接:https://www.haomeiwen.com/subject/rwzzwrtx.html