def get(self): token = Wework().get_token(self.secret) # data = { # "method": "post", # "url": "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_corp_tag_list", # "params": {"access_token": f"{token}"}, # } data = self.load("../data/tag_get.yml") data["params"]["access_token"] = self.token return self.send(data)
class Tag(BaseApi): # 这是标签的秘钥 secret = "MDe1km8BK3AZ05Dnfw4uILuKCZDStZ2NPaokf-UE6c8" # 通过get_token获取到标签的access_token token = Wework().get_token(secret) # 这是增加标签的api,传一个tag_name def add_tag(self, name): # data={ # "method":"post", # "url":"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add_corp_tag?", # "params":f"access_token={self.token}", # "json":{ # "group_id": "etMCs1DwAAg_jBNAuvGR3B21cwl4o0jg", # "tag":[{"name":name}] # } # } # 现在要把add_tag的数据都放到yaml文件上去 # 但这种写法比较low,也不好控制和管理 # data=self.load_yaml("../data/tag_add.yml") # print(data["params"]) # data["params"]=f"access_token={self.token}" # data["json"]["tag"][0]['name']=name # 把请求的数据都放到yml文件,通过template模板把yml文件特定的值改成变量 data = self.template("../data/tag_add.yml", { "token": self.token, "name": name }) # 返回响应的dict return self.send_api(data) # 这是获取tag相关信息的api def get_tag(self): data = { "method": "post", "url": "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_corp_tag_list", "params": f"access_token={self.token}", "json": { "tag": [] } } return self.send_api(data) # 修改tag名字,需要传tag_id和修改后的名字 def edit_tag(self, tag_id, edit_name): data = { "method": "post", "url": "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/edit_corp_tag", "params": f"access_token={self.token}", "json": { "id": tag_id, "name": edit_name } } return self.send_api(data) # 这是删除标签的api,需要传tag_id def delete_tag(self, tag_id): # data={ # "method":"post", # "url":"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/del_corp_tag", # "params":f"access_token={self.token}", # "json":{ # "tag_id":[tag_id] # } # } data = self.template("../data/tag_all.yml", { "token": self.token, "tag_id": tag_id }, "delete") return self.send_api(data)
def get_token(self): secret = cf.get_key("wwork", "contact_secret") access_token = Wework().get_token(secret) return access_token
class WCalendar(BaseApi): """ 企业微信日程中的日历模块的API类 secret:日历的秘钥 token:日历的token yml_api_path: yml api数据的相对路径 """ # 通过配置文件获取日历的secret,token只是为了测试方法,其实不应该存在的 secret = cf.get_key("wwork", "schedule_secret") token = Wework().get_token(secret) # yml api数据的相对路径 yml_api_path = "data/schedule/calendar/calendar_api.yml" # 不用yml文件保存cal_id的数据了,放在数据库好啦 # cal_id_path="data/schedule/calendar/cal_id.yml" def get_cal_id_list(self): """ 通过数据库,获取日历id的值,日历id的值无法通过接口获取,所以就保存在数据库中 :return: 返回全部的日历id的列表 """ # 执行sql语句获取日历id的元祖 cal_id_tuple = sql.select("select cal_id from cal_id") # 把元祖转化成列表 cal_id_list = [i[0] for i in cal_id_tuple] return cal_id_list def add_calendar(self, token, organizer, readonly, set_as_default, summary, color, description): """ 添加日历 :param token: access_token的值 :param organizer: 请求参数的值 :param readonly: 请求参数的值 :param set_as_default: 请求参数的值 :param summary: 请求参数的值 :param color: 请求参数的值 :param description: 请求参数的值 :return: 返回响应体 """ # Template模板需要二次改变的值 p_data = {"ip": self.ip, "token": token, "organizer": organizer, "readonly": readonly, "set_as_default": set_as_default, "summary": summary, "color": color, "description": description} res = self.send_api_data(self.yml_api_path, p_data, "add") try: cal_id = res["cal_id"] # 当cal_id获取到了,就把cal_id放到数据库中 sql.insert(f"insert into cal_id(userid,cal_id) values('{organizer}','{cal_id}')") except KeyError: log.error(f"响应不正确,无法插入数据") # 用yml文件保存数据的方法不要了 # try: # cal_id=res["cal_id"] # cal_id_list=self.load_yaml(self.cal_id_path) # cal_id_list.append(cal_id) # cal_id_list=list(filter(None,cal_id_list)) # self.save_yaml(self.cal_id_path,cal_id_list) # except: # log.info(f"无法获取到cal_id") return res def get_calendar(self, token, index=None): """ 获取日历信息 :param token: 日历的token值 :param index: 在数据库获取第几个id值 :return: 返回响应体 """ # 解决index存在的时候,传的cal_id_list是一个列表 cal_id_list = [] # 从数据库获取cal_id_list,才能查询日历 if index is None: cal_id_list = self.get_cal_id_list() else: cal_id_list.append(self.get_cal_id_list()[index]) p_data = {"ip": self.ip, "token": token, "cal_id_list": cal_id_list} res = self.send_api_data(self.yml_api_path, p_data, "get") return res def delete_calendar(self, token, index): """ 删除日历 :param token: 日历的token值 :param index: 在数据库获取第几个id值 :return: 返回响应体 """ # 从数据库获取cal_id_list cal_id = self.get_cal_id_list()[index] p_data = {"ip": self.ip, "token": token, "cal_id": cal_id} res = self.send_api_data(self.yml_api_path, p_data, "delete") # 当删除api成功时,同步从数据库中删除cal_id if res["errcode"] == 0: sql.delete(f"delete from cal_id where cal_id='{cal_id}'") else: log.info("日历删除请求有误,数据库没有删除cal_id") return res def edit_calendar(self, token, index, readonly, summary, color, description): """ 编辑日历 :param token: 日历的token值 :param index: 请求参数的值 :param readonly: 请求参数的值 :param summary: 请求参数的值 :param color: 请求参数的值 :param description: 请求参数的值 :return: 返回响应体 """ # 从数据库获取cal_id_list cal_id = self.get_cal_id_list()[index] p_data = {"ip": self.ip, "token": token, "cal_id": cal_id, "readonly": readonly, "summary": summary, "color": color, "description": description} res = self.send_api_data(self.yml_api_path, p_data, "edit") return res
class TestMember(): """ 联系人的测试类 1.参数化存放在特定的yml文件中,用三级目录管理用例、参数数据和ids的数据 2.token值并没有使用conftest的fixture,而是在测试类中获取,效率不高 3.并未对token的值做参数化 4.critical的用例等级为完整测试,blocker等级为冒烟测试 5.每个用例都配合fixture,完成了不同的前置和后置,实现了不同用例互不干扰的状态 """ @classmethod def get_token(cls, token): return token # 通过配置文件获取联系人的secret contact_secret = cf.get_key("wwork", "contact_secret") # 获取access_token token = Wework().get_token(contact_secret) # 初始化member的api对象 member = Member() # # 比如在类中,怎么才能导入这个conftest的token # access_token=token ''' 这样获取数据的方法,要读取两次文件,速度太慢了 add_data = member.load_yaml("data/member/member_para_data.yml")['add']['data'] add_ids = member.load_yaml("data/member/member_para_data.yml")['add']['ids'] ''' # 参数化的数据 para_data = member.load_yaml("data/member/member_para_data.yml") # 删除用例的参数化数据和ids标题数据 delete_data = para_data['delete']['data'] delete_ids = para_data['delete']['ids'] # 删除用例的参数化数据和ids标题数据 multi_delete_data = para_data['multi_delete']['data'] multi_delete_ids = para_data['multi_delete']['ids'] # 删除用例的参数化数据和ids标题数据 add_data = para_data['add']['data'] add_ids = para_data['add']['ids'] # 删除用例的参数化数据和ids标题数据 edit_data = para_data['edit']['data'] edit_ids = para_data['edit']['ids'] # 删除用例的参数化数据和ids标题数据 get_data = para_data['get']['data'] get_ids = para_data['get']['ids'] # 删除用例的参数化数据和ids标题数据 active_data = para_data['active']['data'] active_ids = para_data['active']['ids'] # 删除用例的参数化数据和ids标题数据 qr_data = para_data['qr']['data'] qr_ids = para_data['qr']['ids'] # 删除用例的参数化数据和ids标题数据 depart_simple_data = para_data['depart_simple']['data'] depart_simple_ids = para_data['depart_simple']['ids'] # 删除用例的参数化数据和ids标题数据 depart_explicit_data = para_data['depart_explicit']['data'] depart_explicit_ids = para_data['depart_explicit']['ids'] @allure.severity(allure.severity_level.CRITICAL) @allure.story("增加联系人") @pytest.mark.parametrize(("userid,name,mobile,errcode,errmsg"), add_data, ids=add_ids) def test01_add_member(self, userid, name, mobile, errcode, errmsg, add): log.info(f"-------开始测试增加成员-------") res = self.member.add_member(self.token, userid, name, mobile) log.info(f"打印响应结果:{res}") log.info("-------测试结束-------") assert res["errcode"] == errcode assert errmsg in res["errmsg"] @allure.severity(allure.severity_level.CRITICAL) @allure.story("获得联系人信息") @pytest.mark.parametrize(("userid,errcode,errmsg"), get_data, ids=get_ids) def test02_get_member(self, userid, errcode, errmsg, get): log.info("-------开始测试获取成员-------") res = self.member.get_member_info(self.token, userid) log.info(f"打印响应结果:{res}") log.info("-------测试结束-------") assert res["errcode"] == errcode assert errmsg in res["errmsg"] @allure.severity(allure.severity_level.CRITICAL) @allure.story("删除联系人") @pytest.mark.parametrize(("userid,errcode,errmsg"), delete_data, ids=delete_ids) def test04_delete_member(self, userid, errcode, errmsg, delete): log.info("-------开始测试获取成员-------") res = self.member.delete_member(self.token, userid) log.info(f"打印响应结果:{res}") log.info("-------测试结束-------") assert res["errcode"] == errcode assert errmsg in res["errmsg"] @allure.severity(allure.severity_level.CRITICAL) @allure.story("批量删除联系人") @pytest.mark.parametrize(("userid_list,errcode,errmsg"), multi_delete_data, ids=multi_delete_ids) def test05_multi_delete_member(self, userid_list, errcode, errmsg, multi_delete): log.info("-------开始批量删除获取成员-------") res = self.member.multi_delete_member(self.token, userid_list) log.info(f"打印响应结果:{res}") log.info("-------测试结束-------") assert res["errcode"] == errcode assert errmsg in res["errmsg"] @allure.severity(allure.severity_level.CRITICAL) @allure.story("编辑联系人") @pytest.mark.parametrize(("userid,name,mobile,errcode,errmsg"), edit_data, ids=edit_ids) def test03_edit_member(self, userid, name, mobile, errcode, errmsg, edit): log.info("-------开始修改获取成员-------") res = self.member.edit_member(self.token, userid, name, mobile) log.info(f"打印响应结果:{res}") log.info("-------测试结束-------") assert res["errcode"] == errcode assert errmsg in res["errmsg"] @allure.severity(allure.severity_level.NORMAL) @allure.story("查看企业微信活跃度") @pytest.mark.parametrize(("date,errcode,errmsg"), active_data, ids=active_ids) def test_active_stat(self, date, errcode, errmsg): log.info("-------开始查看企业微信活跃度-------") res = self.member.get_active_stat(self.token, date) log.info(f"打印响应结果:{res}") log.info("-------测试结束-------") assert res["errcode"] == errcode assert errmsg in res["errmsg"] @allure.severity(allure.severity_level.NORMAL) @allure.story("增加联系人") @pytest.mark.parametrize(("size,errcode,errmsg"), qr_data, ids=qr_ids) def test_get_invite_qr(self, size, errcode, errmsg): log.info("-------开始获取企业微信二维码-------") res = self.member.get_invite_qr(self.token, size) log.info(f"打印响应结果:{res}") log.info("-------测试结束-------") assert res["errcode"] == errcode assert errmsg in res["errmsg"] @allure.severity(allure.severity_level.CRITICAL) @allure.story("查看部门联系人的简单信息") @pytest.mark.parametrize(("department_id,fetch_child,errcode,errmsg"), depart_simple_data, ids=depart_simple_ids) def test_get_depart_member(self, department_id, fetch_child, errcode, errmsg): log.info("-------开始获取部门成员简单的信息-------") res = self.member.get_depart_member(self.token, department_id, fetch_child) log.info(f"打印响应结果:{res}") log.info("-------测试结束-------") assert res["errcode"] == errcode assert errmsg in res["errmsg"] @allure.severity(allure.severity_level.CRITICAL) @allure.story("查看部门联系人的复杂信息") @pytest.mark.parametrize(("department_id,fetch_child,errcode,errmsg"), depart_explicit_data, ids=depart_explicit_ids) def test_get_depart_member_explict(self, department_id, fetch_child, errcode, errmsg): log.info("-------开始部门成员详细信息-------") res = self.member.get_depart_member_explicit(self.token, department_id, fetch_child) log.info(f"打印响应结果:{res}") log.info("-------测试结束-------") assert res["errcode"] == errcode assert errmsg in res["errmsg"] @allure.severity(allure.severity_level.BLOCKER) @allure.story("增加联系人") @pytest.mark.smoke def test_all_smoke_member(self, test_all_pre_data): add_res = self.member.add_member(self.token, "tong1234", "tong1234", "13172771165") edit_res = self.member.edit_member(self.token, "tong1234", "tong1234", "13172771165") del_res = self.member.delete_member(self.token, "tong1234") multi_del_res = self.member.multi_delete_member( self.token, ["tongtong1", "tongtong2", "tongtong3"]) qr_res = self.member.get_invite_qr(self.token, 1) active_res = self.member.get_active_stat(self.token, "2020-10-10") depart_res = self.member.get_depart_member(self.token, "1", "1") depart_res_e = self.member.get_depart_member_explicit( self.token, "1", "1") assert add_res["errcode"] == 0 assert edit_res["errcode"] == 0 assert del_res["errcode"] == 0 assert multi_del_res["errcode"] == 0 assert qr_res["errcode"] == 0 assert active_res["errcode"] == 40058 assert depart_res["errcode"] == 0 assert depart_res_e["errcode"] == 0 log.info("finish")
import os from api.base_api import BaseApi from api.wework import Wework from common.config import cf from common.get_log import log secret = cf.get_key("wwork", "contact_secret") token = Wework().get_token(secret) class Department(BaseApi): base_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) ip = cf.get_key("env", "formal_ip") def get_depart(self, id=None): p_data = {"ip": self.ip, "token": token, "id": id} res = self.send_api_data("data/department/department_api.yml", p_data, "get") return res if __name__ == "__main__": a = Department() print(a.get_depart())
def token(): secret = cf.get_key("wwork", "meeting_room_secret") token = Wework().get_token(secret) return token
def schedule_token(): schedule_secret = cf.get_key("wwork", "schedule_secret") token = Wework().get_token(schedule_secret) return token
def setup(self): corpsecret = "YC9RRMQcQqGNxapjoeiDIn84mCY7H-aJblz_X9X073U" wework = Wework() print(wework.get_token(corpsecret))
def test_get_token(self): a = Wework() log.error("test") print(a.get_token(self.secret))
class TestMember(): # 通过配置文件获取联系人的secret contact_secret = cf.get_key("wwork", "contact_secret") # 获取access_token token = Wework().get_token(contact_secret) # 初始化member的api对象 member = Member() ''' 这样获取数据的方法,要读取两次文件,速度太慢了 add_data = member.load_yaml("data/member/member_para_data.yml")['add']['data'] add_ids = member.load_yaml("data/member/member_para_data.yml")['add']['ids'] ''' # 参数化的数据 para_data = member.load_yaml("data/member/member_para_data.yml") # 删除用例的参数化数据和ids标题数据 delete_data = para_data['delete']['data'] delete_ids = para_data['delete']['ids'] # 删除用例的参数化数据和ids标题数据 multi_delete_data = para_data['multi_delete']['data'] multi_delete_ids = para_data['multi_delete']['ids'] # 删除用例的参数化数据和ids标题数据 add_data = para_data['add']['data'] add_ids = para_data['add']['ids'] # 删除用例的参数化数据和ids标题数据 edit_data = para_data['edit']['data'] edit_ids = para_data['edit']['ids'] # 删除用例的参数化数据和ids标题数据 get_data = para_data['get']['data'] get_ids = para_data['get']['ids'] # 删除用例的参数化数据和ids标题数据 active_data = para_data['active']['data'] active_ids = para_data['active']['ids'] # 删除用例的参数化数据和ids标题数据 qr_data = para_data['qr']['data'] qr_ids = para_data['qr']['ids'] # 删除用例的参数化数据和ids标题数据 depart_simple_data = para_data['depart_simple']['data'] depart_simple_ids = para_data['depart_simple']['ids'] # 删除用例的参数化数据和ids标题数据 depart_explicit_data = para_data['depart_explicit']['data'] depart_explicit_ids = para_data['depart_explicit']['ids'] # # def setup_class(self,): # print("abc") @allure.severity(allure.severity_level.CRITICAL) @allure.story("增加联系人") @pytest.mark.parametrize(("userid,name,mobile,errcode,errmsg"), add_data, ids=add_ids) def test01_add_member(self, userid, name, mobile, errcode, errmsg, add): log.info(f"-------开始测试增加成员-------") res = self.member.add_member(self.token, userid, name, mobile) log.info(f"打印响应结果:{res}") log.info("-------测试结束-------") assert res["errcode"] == errcode assert errmsg in res["errmsg"] @allure.severity(allure.severity_level.CRITICAL) @allure.story("获得联系人信息") @pytest.mark.parametrize(("userid,errcode,errmsg"), get_data, ids=get_ids) def test02_get_member(self, userid, errcode, errmsg, get): log.info("-------开始测试获取成员-------") res = self.member.get_member_info(self.token, userid) log.info(f"打印响应结果:{res}") log.info("-------测试结束-------") assert res["errcode"] == errcode assert errmsg in res["errmsg"] @allure.severity(allure.severity_level.CRITICAL) @allure.story("删除联系人") @pytest.mark.parametrize(("userid,errcode,errmsg"), delete_data, ids=delete_ids) def test04_delete_member(self, userid, errcode, errmsg, delete): log.info("-------开始测试获取成员-------") res = self.member.delete_member(self.token, userid) log.info(f"打印响应结果:{res}") log.info("-------测试结束-------") assert res["errcode"] == errcode assert errmsg in res["errmsg"] @allure.severity(allure.severity_level.CRITICAL) @allure.story("批量删除联系人") @pytest.mark.parametrize(("userid_list,errcode,errmsg"), multi_delete_data, ids=multi_delete_ids) def test05_multi_delete_member(self, userid_list, errcode, errmsg, multi_delete): log.info("-------开始批量删除获取成员-------") res = self.member.multi_delete_member(self.token, userid_list) log.info(f"打印响应结果:{res}") log.info("-------测试结束-------") assert res["errcode"] == errcode assert errmsg in res["errmsg"] @allure.severity(allure.severity_level.CRITICAL) @allure.story("编辑联系人") @pytest.mark.parametrize(("userid,name,mobile,errcode,errmsg"), edit_data, ids=edit_ids) def test03_edit_member(self, userid, name, mobile, errcode, errmsg, edit): log.info("-------开始修改获取成员-------") res = self.member.edit_member(self.token, userid, name, mobile) log.info(f"打印响应结果:{res}") log.info("-------测试结束-------") assert res["errcode"] == errcode assert errmsg in res["errmsg"] @allure.severity(allure.severity_level.NORMAL) @allure.story("查看企业微信活跃度") @pytest.mark.parametrize(("date,errcode,errmsg"), active_data, ids=active_ids) def test_active_stat(self, date, errcode, errmsg): log.info("-------开始查看企业微信活跃度-------") res = self.member.get_active_stat(self.token, date) log.info(f"打印响应结果:{res}") log.info("-------测试结束-------") assert res["errcode"] == errcode assert errmsg in res["errmsg"] @allure.severity(allure.severity_level.NORMAL) @allure.story("增加联系人") @pytest.mark.parametrize(("size,errcode,errmsg"), qr_data, ids=qr_ids) def test_get_invite_qr(self, size, errcode, errmsg): log.info("-------开始获取企业微信二维码-------") res = self.member.get_invite_qr(self.token, size) log.info(f"打印响应结果:{res}") log.info("-------测试结束-------") assert res["errcode"] == errcode assert errmsg in res["errmsg"] @allure.severity(allure.severity_level.CRITICAL) @allure.story("查看部门联系人的简单信息") @pytest.mark.parametrize(("department_id,fetch_child,errcode,errmsg"), depart_simple_data, ids=depart_simple_ids) def test_get_depart_member(self, department_id, fetch_child, errcode, errmsg): log.info("-------开始获取部门成员简单的信息-------") res = self.member.get_depart_member(self.token, department_id, fetch_child) log.info(f"打印响应结果:{res}") log.info("-------测试结束-------") assert res["errcode"] == errcode assert errmsg in res["errmsg"] @allure.severity(allure.severity_level.CRITICAL) @allure.story("查看部门联系人的复杂信息") @pytest.mark.parametrize(("department_id,fetch_child,errcode,errmsg"), depart_explicit_data, ids=depart_explicit_ids) def test_get_depart_member_explict(self, department_id, fetch_child, errcode, errmsg): log.info("-------开始部门成员详细信息-------") res = self.member.get_depart_member_explicit(self.token, department_id, fetch_child) log.info(f"打印响应结果:{res}") log.info("-------测试结束-------") assert res["errcode"] == errcode assert errmsg in res["errmsg"] @allure.severity(allure.severity_level.BLOCKER) @allure.story("增加联系人") @pytest.mark.smoke def test_all_smoke_member(self, test_all_pre_data): add_res = self.member.add_member(self.token, "tong1234", "tong1234", "13172771165") edit_res = self.member.edit_member(self.token, "tong1234", "tong1234", "13172771165") del_res = self.member.delete_member(self.token, "tong1234") multi_del_res = self.member.multi_delete_member( self.token, ["tongtong1", "tongtong2", "tongtong3"]) qr_res = self.member.get_invite_qr(self.token, 1) active_res = self.member.get_active_stat(self.token, "2020-10-10") depart_res = self.member.get_depart_member(self.token, "1", "1") depart_res_e = self.member.get_depart_member_explicit( self.token, "1", "1") assert add_res["errcode"] == 0 assert edit_res["errcode"] == 0 assert del_res["errcode"] == 0 assert multi_del_res["errcode"] == 0 assert qr_res["errcode"] == 0 assert active_res["errcode"] == 0 assert depart_res["errcode"] == 0 assert depart_res_e["errcode"] == 0 log.info("finish") def tear_down(self): pass
class WSchedule(BaseApi): """ 企业微信日程中的日程模块的API类 secret:日历的秘钥 token:日历的token yml_api_path: yml api数据的相对路径 """ # 通过配置文件获取日历的secret,token只是为了测试方法,其实不应该存在的 secret = cf.get_key("wwork", "schedule_secret") token = Wework().get_token(secret) # yml api数据的相对路径 data_path = "data/schedule/schedule/schedule_api.yml" def get_schedule_id_list(self): """ 通过数据库,获取日程id的值,日程id的值无法通过接口获取,所以就保存在数据库中 :return: 返回全部的日历程id的列表 """ # 执行sql语句获取日历id的元祖 schedule_id_list = sql.select("select schedule_id from schedule_id") # 把元祖转化成列表 schedule_id_list = [i[0] for i in schedule_id_list] return schedule_id_list def add_schedule(self, token, organizer, start_time, end_time, userid, summary, description, location): """ 增加日程 :param token: access_token的值 :param organizer: 请求参数的值 :param start_time: 请求参数的值 :param end_time: 请求参数的值 :param userid: 请求参数的值 :param summary: 请求参数的值 :param description: 请求参数的值 :param location: 请求参数的值 :return: 返回响应体 """ # 时间传入时间戳,get_time可以将时间字符串转化成时间戳,自己封装的 start_time = self.get_time(start_time) end_time = self.get_time(end_time) # Template模板需要二次改变的值 p_data = { "ip": self.ip, "token": token, "organizer": organizer, "start_time": start_time, "end_time": end_time, "userid": userid, "summary": summary, "description": description, "location": location } res = self.send_api_data(self.data_path, p_data, "add") try: schedule_id = res["schedule_id"] # 当cal_id获取到了,就把schedule_id放到数据库中 sql.insert( f"insert into schedule_id(userid,schedule_id) values('{organizer}','{schedule_id}')" ) except KeyError as e: log.error("响应不正确,无法插入数据") return res def delete_schedule(self, token, index): """ 删除日程 :param token: 日历的token值 :param index: 在数据库获取第几个id值 :return: 返回响应体 """ # 从数据库获取schedule_id schedule_id = self.get_schedule_id_list()[index] p_data = {"ip": self.ip, "token": token, "schedule_id": schedule_id} res = self.send_api_data(self.data_path, p_data, "delete") # 当删除api成功时,同步从数据库中删除schedule_id if res["errcode"] == 0: sql.delete( f"delete from schedule_id where schedule_id='{schedule_id}'") else: log.info("删除请求失败,无法删除schedule_id") return res def get_schedule(self, token, index=None): """ 获取日程信息 :param token: 日程的token值 :param index: 在数据库获取第几个id值 :return: 返回响应体 """ # 解决index存在的时候,传的schedule_id_list不是一个列表 schedule_id_list = [] # 从数据库获取schedule_id_list,才能查询日历 if index is None: schedule_id_list = self.get_schedule_id_list() else: schedule_id_list.append(self.get_schedule_id_list()[index]) p_data = { "ip": self.ip, "token": token, "schedule_id_list": schedule_id_list } res = self.send_api_data(self.data_path, p_data, "get") return res def edit_schedule(self, token, organizer, index, start_time, end_time, userid, summary, description, location): """ :param token: 日程的token值 :param organizer: 请求参数的值 :param index: 请求参数的值 :param start_time: 请求参数的值 :param end_time: 请求参数的值 :param userid: 请求参数的值 :param summary: 请求参数的值 :param description: 请求参数的值 :param location: 请求参数的值 :return: """ # 从数据库获取schedule_id schedule_id = self.get_schedule_id_list()[index] start_time = self.get_time(start_time) end_time = self.get_time(end_time) p_data = { "ip": self.ip, "token": token, "organizer": organizer, "schedule_id": schedule_id, "start_time": start_time, "end_time": end_time, "userid": userid, "summary": summary, "description": description, "location": location } res = self.send_api_data(self.data_path, p_data, "edit") return res
class Tag(Base_api): secret = "MDe1km8BK3AZ05Dnfw4uILuKCZDStZ2NPaokf-UE6c8" token = Wework().get_token(secret) def add(self, tag_name): # data={ # "method":"post", # "url":"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add_corp_tag", # "params":{"access_token":f"{self.token}"}, # "json":{ # "group_name": "test", # "tag":[{"name": f"{tag_name}"}] # } # data=self.load("../data/tag_add.yml") # data["params"]["access_token"]=self.token # data["json"]["tag"][0]["name"]=tag_name # print(json.dumps(data,indent=2)) # return self.send(data) data = self.template("../data/tag_add.yml", { "token": self.token, "tag_name": tag_name }) return self.send(data) def get(self): token = Wework().get_token(self.secret) # data = { # "method": "post", # "url": "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_corp_tag_list", # "params": {"access_token": f"{token}"}, # } data = self.load("../data/tag_get.yml") data["params"]["access_token"] = self.token return self.send(data) def delete(self, tag_id): data = { "method": "post", "url": "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/del_corp_tag", "params": { "access_token": f"{self.token}" }, "json": { "tag_id": [f"{tag_id}"] } } return self.send(data) def update(self, tag_id, tag_name): data = { "method": "post", "url": "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/edit_corp_tag", "params": { "access_token": f"{self.token}" }, "json": { "id": f"{tag_id}", "name": f"{tag_name}" } } return self.send(data)
class MeetingRoom(BaseApi): """ 会议室的api类 test_token: token值 api_path: yml_api的相对路径 """ # 只是拿来测试用,可以不存在在这里 test_token = Wework().get_token(cf.get_key("wwork", "meeting_room_secret")) # 简化send_api_data方法路径引用,不用写那么多路径了 api_path = "data/meeting_room/meeting_room_api.yml" # 增加会议室 def add_meeting_room(self, token, name, capacity, city, building, floor, equipment): # Template模板二次修改的值,p_data p_data = { "ip": self.ip, "token": token, "name": name, "capacity": capacity, "city": city, "building": building, "floor": floor, "equipment": equipment } # 获取响应,进行了多次封装 res = self.send_api_data(self.api_path, p_data, "add") return res # 编辑会议室 def edit_meeting_room(self, token, meetingroom_id, name, capacity, city, building, floor, equipment): p_data = { "ip": self.ip, "token": token, "meetingroom_id": meetingroom_id, "name": name, "capacity": capacity, "city": city, "building": building, "floor": floor, "equipment": equipment } res = self.send_api_data(self.api_path, p_data, "edit") return res # 删除会议室 def delete_meeting_room(self, token, meetingroom_id): p_data = { "ip": self.ip, "token": token, "meetingroom_id": meetingroom_id } res = self.send_api_data(self.api_path, p_data, "delete") return res # 获取会议室 def get_meeting_room(self, token, city, building, floor, equipment): p_data = { "ip": self.ip, "token": token, "city": city, "building": building, "floor": floor, "equipment": equipment } res = self.send_api_data(self.api_path, p_data, "get") return res