禅道调用接口来创建用户
禅道通过接口来创建用户,主要是获取sessionid,这个给后面的接口当着鉴权一样的效果
1、通过 /api-getsessionid.json 接口来获取zentaosid
def _get_session_id(self): api_path = "/xxx/api-getsessionid.json" response = requests.get(self.host + api_path) result = self._get_zentao_result(response.json()) self.session_id = result['sessionID'] self.params[result['sessionName']] = self.session_id
def _get_zentao_result(result): if result['status'] == 'success' and md5(result['data'].encode()).hexdigest() == result['md5']: data = json.loads(result['data']) return data return result
2、自动登录来测试效果
def login(self): api_path = '/pro/user-login.json' data = { 'account': self.account, 'password': self.password, } result = requests.post(self.host + api_path, data=data, params=self.params).json() if result['status'] == 'success': print('zentao 登陆成功') else: print(result)
3、用户接口为 /user-create-0.json,
def create_user(self, account: str, password: str, realname: str, deptid: int, email: str = ""): """ 创建用户 :param account: 账号 :param password: 密码 :param realname: 真实姓名 :param deptid: 部门编号 :param email: 邮箱 :return: """ api_path = "/user-create-0.json" data = { "dept": deptid, "account": account, "password1": password, "password2": password, "realname": realname, "join": str(datetime.date.today()), "role": "dev", "group": 2, "email": email, "commiter": "", "gender": "m", "verifyPassword": self._md5(self._md5(self.password) + str(self._get_rand())) } response = requests.post(self.host + api_path, data=data, params=self.params) if response.status_code == 200: result = response.json() return result return response.text
怎么获取rand随机数呢?同样也是通过 /user-create-0.json 接口,不过是get方式
def _get_rand(self): api_path = "/user-create-0.json" response = requests.get(self.host + api_path, params=self.params) data = json.loads((response.json()).get('data')) rand = data.get('rand') return rand
def _md5(self, password): value = hashlib.md5(password.encode(encoding='utf-8')).hexdigest() return value
完整代码如下,也参考网上资料
#!/usr/bin/env python # -*- coding: utf-8 -*- import requests import hashlib from hashlib import md5 import json import datetime class Zentao(object): def __init__(self, host, account, password): self.host = host self.session_id = '' self.params = {} self.account = account self.password = password self._get_session_id() self.login() def _get_session_id(self): api_path = "/xxx/api-getsessionid.json" response = requests.get(self.host + api_path) result = self._get_zentao_result(response.json()) self.session_id = result['sessionID'] self.params[result['sessionName']] = self.session_id def login(self): api_path = '/xxx/user-login.json' data = { 'account': self.account, 'password': self.password, } result = requests.post(self.host + api_path, data=data, params=self.params).json() if result['status'] == 'success': print('zentao 登陆成功') else: print(result) @staticmethod def _get_zentao_result(result): if result['status'] == 'success' and md5(result['data'].encode()).hexdigest() == result['md5']: data = json.loads(result['data']) return data return result def zentao_get(self, api_path): response = requests.get(self.host + api_path, self.params) result = response.json() return self._get_zentao_result(result) def zentao_post(self, api_path, data=None, json_data=None): response = requests.post(self.host + api_path, data=data, json=json_data, params=self.params) return self._get_zentao_result(response.json()) def get_dept_list(self): """ 获取部门列表 :return: """ api_path = "/xxx/dept-browse-.json" data = self.zentao_get(api_path) return data['sons'] def create_user(self, account: str, password: str, realname: str, deptid: int, email: str = ""): """ 创建用户 :param account: 账号 :param password: 密码 :param realname: 真实姓名 :param deptid: 部门编号 :param email: 邮箱 :return: """ api_path = "/user-create-0.json" data = { "dept": deptid, "account": account, "password1": password, "password2": password, "realname": realname, "join": str(datetime.date.today()), "role": "dev", "group": 2, "email": email, "commiter": "", "gender": "m", "verifyPassword": self._md5(self._md5(self.password) + str(self._get_rand())) } response = requests.post(self.host + api_path, data=data, params=self.params) if response.status_code == 200: result = response.json() return result return response.text def _get_rand(self): """ """ api_path = "/user-create-0.json" response = requests.get(self.host + api_path, params=self.params) data = json.loads((response.json()).get('data')) rand = data.get('rand') return rand def _md5(self, password): value = hashlib.md5(password.encode(encoding='utf-8')).hexdigest() return value if __name__ == '__main__': zentao_host = "xxx" zentao_account = "root" zentao_password = "root" zentao = Zentao(zentao_host, zentao_account, zentao_password) zentao.create_user("demo","123456","测试",66)
参考地址:https://www.jianshu.com/p/4f3fa26c8dc3?utm_campaign=haruki&utm_content=note&utm_medium=seo_notes&utm_source=recommendation