def __init__(self, addresses, namespace_id): self.addrs = [] for addr in addresses.split(","): addr = addr.split(":") host = addr[0] port = addr[1] if len(addr) > 1 else 8848 self.addrs.append(Address(host, port)) self.winney = Winney( host=self.addrs[0].host, port=self.addrs[0].port, addrs=self.addrs, ) self.namespace_id = namespace_id self.init_functions()
def __init__(self, name, host, port, headers=None, scheme="http"): self.name = name self.host = host self.port = port self.scheme = scheme self.headers = headers self.group_list: List[Group] = [] self.winney = Winney(host=self.host, port=self.port, headers=self.headers, protocol=scheme)
class UserCenter(object): def __init__(self): addr = Address(host="localhost", port=5000) self.winney = Winney(host="localhost", port=5000, addrs=[addr]) self.init_functions() def init_functions(self): self.winney.register(method="post", name="login", uri="/api/login", mock=False, mock_data=None) self.winney.register(method="get", name="get_user", uri="/api/user", mock=True, mock_data=UserMock()) @retry def login(self, account, password): r = self.winney.login(json={"account": account, "password": password}) return r.json() @retry def get_user(self, user_id): r = self.winney.get_user(data={"user_id": user_id}) return r.json()
def __init__(self, host, port): self.token = None self.school = None self.winney = Winney(host=host, port=port) self.init_functions()
class Jojo(object): def __init__(self, host, port): self.token = None self.school = None self.winney = Winney(host=host, port=port) self.init_functions() def init_functions(self): # 用户 self.winney.register(method="POST", name="sign_up", uri="/yangtze/accounts/register/") self.winney.register(method="POST", name="sign_in", uri="/yangtze/login/") # 基础数据 self.winney.register(method="POST", name="school_add", uri="/yangtze/schools/") self.winney.register(method="GET", name="school_list", uri="/yangtze/schools/") self.winney.register(method="POST", name="subject_add", uri="/yangtze/subjects/") self.winney.register(method="GET", name="subject_list", uri="/yangtze/subjects/") self.winney.register(method="GET", name="subject_get", uri="/yangtze/subjects/{subject_id}/") self.winney.register(method="POST", name="phase_add", uri="/yangtze/phases/") self.winney.register(method="GET", name="phase_list", uri="/yangtze/phases/") self.winney.register(method="GET", name="phase_get", uri="/yangtze/phases/{phase_id}/") self.winney.register(method="POST", name="knowledge_add", uri="/yangtze/knowledge/") self.winney.register(method="GET", name="knowledge_list", uri="/yangtze/knowledge/") self.winney.register(method="POST", name="chapter_add", uri="/yangtze/chapters/") self.winney.register(method="GET", name="chapter_list", uri="/yangtze/chapters/") self.winney.register(method="POST", name="tb_edition_add", uri="/yangtze/textbook_editions/") self.winney.register(method="GET", name="tb_edition_list", uri="/yangtze/textbook_editions/") self.winney.register(method="POST", name="textbook_add", uri="/yangtze/textbooks/") self.winney.register(method="GET", name="textbook_list", uri="/yangtze/textbooks/") self.winney.register(method="GET", name="diff_level_list", uri="/yangtze/diff_level/list/") # 资源 self.winney.register(method="POST", name="resource_add", uri="/yangtze/resource/") self.winney.register(method="GET", name="resource_list", uri="/yangtze/resource/list/") self.winney.register(method="POST", name="resource_publish", uri="/yangtze/resource/publish/") self.winney.register(method="GET", name="resource_download", uri="/yangtze/resource/download/{resource_id}/") self.winney.register(method="POST", name="resource_chapter", uri="/yangtze/resource/chapter/") self.winney.register(method="POST", name="resource_knowledge", uri="/yangtze/resource/knowledge/") self.winney.register(method="GET", name="resource_category_list", uri="/yangtze/resource/category/list/") # 资源类别 self.winney.register(method="POST", name="resource_kind_add", uri="/yangtze/resource_kind/") self.winney.register(method="GET", name="resource_kind_list", uri="/yangtze/resource_kind/list/") # 题目 self.winney.register(method="POST", name="question_upload", uri="/yangtze/question/upload/") self.winney.register(method="POST", name="question_add", uri="/yangtze/question/") self.winney.register(method="GET", name="question_get", uri="/yangtze/question/{question_id}/") # self.winney.register(method="GET", name="question_list", uri="/yangtze/question/list/") self.winney.register(method="POST", name="question_list", uri="/yangtze/question/list/") self.winney.register(method="POST", name="question_update_label", uri="/yangtze/question/update/label/") self.winney.register(method="POST", name="question_remove_label", uri="/yangtze/question/remove/label/") self.winney.register(method="DELETE", name="question_delete", uri="/yangtze/question/{question_id}") self.winney.register(method="POST", name="question_delete_bulk", uri="/yangtze/question/delete/") self.winney.register(method="GET", name="sourcefile_list", uri="/yangtze/sourcefile/list/") self.winney.register(method="PUT", name="sourcefile_update", uri="/yangtze/sourcefile/{sourcefile_id}/") self.winney.register(method="POST", name="sourcefile_publish", uri="/yangtze/sourcefile/{sourcefile_id}/") self.winney.register(method="DELETE", name="sourcefile_delete", uri="/yangtze/sourcefile/{sourcefile_id}/") # 试卷 self.winney.register(method="POST", name="exam_kind_add", uri="/yangtze/exam_kind/") self.winney.register(method="PUT", name="exam_kind_update", uri="/yangtze/exam_kind/{kind_id}/") self.winney.register(method="GET", name="exam_kind_get", uri="/yangtze/exam_kind/{kind_id}/") self.winney.register(method="DELETE", name="exam_kind_delete", uri="/yangtze/exam_kind/{kind_id}/") self.winney.register(method="GET", name="exam_kind_list", uri="/yangtze/exam_kind/list/") self.winney.register(method="POST", name="exam_add", uri="/yangtze/exam/") self.winney.register(method="GET", name="exam_list", uri="/yangtze/exam/list/") self.winney.register(method="PUT", name="exam_update", uri="/yangtze/exam/{exam_id}/") self.winney.register(method="GET", name="exam_get", uri="/yangtze/exam/{exam_id}/") self.winney.register(method="DELETE", name="exam_delete", uri="/yangtze/exam/{exam_id}/") # 收藏 self.winney.register(method="POST", name="focus", uri="/yangtze/focus/") self.winney.register(method="GET", name="focus_list", uri="/yangtze/focus/list/") self.winney.register(method="POST", name="focus_remove", uri="/yangtze/focus/remove/") # 下载历史 self.winney.register(method="GET", name="download_history", uri="/yangtze/download/history/") @staticmethod def get_data(r): if not r.ok(): raise HTTPResponseError("出现异常,HTTP status code={}".format( r.status_code)) data = r.json() if data["code"] != 0: raise ServerResponseError("服务器返回错误,code={}, message={}".format( data["code"], data["message"])) return data["data"] @staticmethod def check_type(param, tp): if not isinstance(param, tp): raise TypeError("param should be type of {}, but {} found".format( tp, type(param))) def sign_up(self, param: SignUpParam): r = self.winney.sign_up(json=param.json()) user = UserEntity.loads(self.get_data(r)) return user def sign_in(self, param: LogInParam): if not isinstance(param, LogInParam): raise TypeError( "param should be type of LogInParam, but {} found".format( type(param))) r = self.winney.sign_in(json=param.json()) user = UserEntity.loads(self.get_data(r)) self.token = "Token " + user.token print("token = ", self.token) return user def school_add(self, param: SchoolAddParam): self.check_type(param, SchoolAddParam) r = self.winney.school_add(json=param.json(), headers={"Authorization": self.token}) self.school = SchoolEntity.loads(self.get_data(r)) return self.school def school_list(self): r = self.winney.school_list(headers={"Authorization": self.token}) schools = self.get_data(r)["result"] schools = [SchoolEntity.loads(school) for school in schools] if schools else None self.school = schools[0] if schools else None return schools def phase_add(self, param: PhaseAddParam): self.check_type(param, PhaseAddParam) r = self.winney.phase_add(json=param.json(), headers={"Authorization": self.token}) phase = PhaseEntity.loads(self.get_data(r)) return phase def phase_list(self): r = self.winney.phase_list( headers={ "Authorization": self.token, "X-Custom-Header-3School": self.school.uid }) phases = self.get_data(r)["result"] phases = [PhaseEntity.loads(phase) for phase in phases] if phases else None return phases def subject_add(self, param: SubjectAddParam): self.check_type(param, SubjectAddParam) r = self.winney.subject_add(json=param.json(), headers={"Authorization": self.token}) return SubjectEntity.loads(self.get_data(r)) def subject_list(self): r = self.winney.subject_list( headers={ "Authorization": self.token, "X-Custom-Header-3School": self.school.uid }) subjects = self.get_data(r)["result"] subjects = [SubjectEntity.loads(subject) for subject in subjects] if subjects else None return subjects def tb_edition_add(self, param: TbEditionAddParam) -> TextBookEditionEntity: self.check_type(param, TbEditionAddParam) r = self.winney.tb_edition_add(json=param.json(), headers={"Authorization": self.token}) return TextBookEditionEntity.loads(self.get_data(r)) def tb_edition_list(self) -> List[TextBookEditionEntity]: r = self.winney.tb_edition_list( headers={ "Authorization": self.token, "X-Custom-Header-3School": self.school.uid }) editions = self.get_data(r)["result"] editions = [ TextBookEditionEntity.loads(edition) for edition in editions ] if editions else None return editions def textbook_add(self, param: TextBookAddParam) -> TextBookEntity: self.check_type(param, TextBookAddParam) r = self.winney.textbook_add(json=param.json(), headers={"Authorization": self.token}) return TextBookEntity.loads(self.get_data(r)) def textbook_list(self) -> [TextBookEntity]: r = self.winney.textbook_list( headers={ "Authorization": self.token, "X-Custom-Header-3School": self.school.uid }) textbooks = self.get_data(r)["result"] textbooks = [TextBookEntity.loads(tb) for tb in textbooks] if textbooks else None return textbooks def knowledge_add(self, param: KnowledgeAddParam) -> KnowledgeEntity: self.check_type(param, KnowledgeAddParam) r = self.winney.knowledge_add(json=param.json(), headers={"Authorization": self.token}) return KnowledgeEntity.loads(self.get_data(r)) def knowledge_list(self) -> [KnowledgeEntity]: r = self.winney.knowledge_list( headers={ "Authorization": self.token, "X-Custom-Header-3School": self.school.uid }) rs = self.get_data(r)["result"] rs = [KnowledgeEntity.loads(r) for r in rs] if rs else None return rs def chapter_add(self, param: ChapterAddParam) -> ChapterEntity: self.check_type(param, ChapterAddParam) r = self.winney.chapter_add(json=param.json(), headers={"Authorization": self.token}) return ChapterEntity.loads(self.get_data(r)) def chapter_list(self) -> List[ChapterEntity]: r = self.winney.chapter_list( headers={ "Authorization": self.token, "X-Custom-Header-3School": self.school.uid }) chapters = self.get_data(r)["result"] chapters = [ChapterEntity.loads(chapter) for chapter in chapters] if chapters else None return chapters def diff_level_list(self): r = self.winney.diff_level_list(headers={"Authorization": self.token}) rs = self.get_data(r) print(rs) def question_upload(self, param: QuestionUploadParam) -> SourceFileEntity: self.check_type(param, QuestionUploadParam) r = self.winney.question_upload( data={"subject_id": param.subject_id}, files={"file": param.file_obj}, headers={"Authorization": self.token}, ) return SourceFileEntity.loads(self.get_data(r)) def question_list(self, param: QuestionListParam) -> List[QuestionEntity]: self.check_type(param, QuestionListParam) r = self.winney.question_list(json=param.json(), headers={"Authorization": self.token}) questions = self.get_data(r)["result"] # 有分页参数 # print(json.dumps(questions[0], indent=4)) # print(questions[0]["uid"]) questions = [QuestionEntity.loads(q) for q in questions] if questions else None return questions def question_get(self, param: QuestionEntity) -> QuestionEntity: self.check_type(param, QuestionEntity) r = self.winney.question_get(question_id=param.uid, headers={"Authorization": self.token}) return QuestionEntity.loads(self.get_data(r)) def question_update_label( self, param: QuestionLabelParam) -> List[QuestionEntity]: self.check_type(param, QuestionLabelParam) r = self.winney.question_update_label( json=param.json(), headers={"Authorization": self.token}) questions = self.get_data(r) questions = [QuestionEntity.loads(q) for q in questions] if questions else None return questions def question_remove_label(self, param: QuestionLabelParam): self.check_type(param, QuestionLabelParam) r = self.winney.question_remove_label( json=param.json(), headers={"Authorization": self.token}) questions = self.get_data(r) questions = [QuestionEntity.loads(q) for q in questions] if questions else None return questions def question_delete(self, param: QuestionEntity): self.check_type(param, QuestionEntity) r = self.winney.question_delete(question_id=param.uid, headers={"Authorization": self.token}) return self.get_data(r) def question_delete_bulk(self, param: QuestionDeleteBulkParam): r = self.winney.question_delete_bulk( json=param.json(), headers={"Authorization": self.token}) return self.get_data(r) def sourcefile_list(self) -> List[SourceFileEntity]: r = self.winney.sourcefile_list(headers={"Authorization": self.token}) rs = self.get_data(r)["result"] rs = [SourceFileEntity.loads(r) for r in rs] if rs else None return rs def sourcefile_publish(self, param: SourceFilePublishParam): self.check_type(param, SourceFilePublishParam) r = self.winney.sourcefile_publish( sourcefile_id=param.sourcefile_id, headers={"Authorization": self.token}) return SourceFileEntity.loads(self.get_data(r)) def sourcefile_update(self, param: SourceFileUpdateParam): self.check_type(param, SourceFileUpdateParam) r = self.winney.sourcefile_update( sourcefile_id=param.sourcefile_id, json=param.json(), headers={"Authorization": self.token}, ) return SourceFileEntity.loads(self.get_data(r)) def sourcefile_delete(self, param: SourceFileDeleteParam): self.check_type(param, SourceFileDeleteParam) r = self.winney.sourcefile_delete( sourcefile_id=param.sourcefile_id, headers={"Authorization": self.token}) return self.get_data(r) def resource_add(self, param: ResourceAddParam): self.check_type(param, ResourceAddParam) r = self.winney.resource_add( data={"subject_id": param.subject_id}, files={"file": param.file_obj}, headers={"Authorization": self.token}, ) return ResourceEntity.loads(self.get_data(r)) def resource_publish(self, param: ResourcePublishParam): r = self.winney.resource_publish(json=param.json(), headers={"Authorization": self.token}) return ResourceEntity.loads(self.get_data(r)) def resource_list(self, param: ResourceListParam): r = self.winney.resource_list(data=param.json(), headers={"Authorization": self.token}) rs = self.get_data(r)["result"] rs = [ResourceEntity.loads(r) for r in rs] if rs else None return rs def resource_download(self, resource_id): r = self.winney.resource_download( resource_id=resource_id, headers={"Authorization": self.token}) print("Success to download resource") print(r.headers) return r.content def resource_chapter(self, param: ResourceChapterParam): self.check_type(param, ResourceChapterParam) r = self.winney.resource_chapter(json=param.json(), headers={"Authorization": self.token}) return ResourceEntity.loads(self.get_data(r)) def resource_knowledge(self, param: ResourceKnowledgeParam): self.check_type(param, ResourceKnowledgeParam) r = self.winney.resource_knowledge( json=param.json(), headers={"Authorization": self.token}) return ResourceEntity.loads(self.get_data(r)) def resource_category_list(self): r = self.winney.resource_category_list( headers={"Authorization": self.token}) rs = self.get_data(r) rs = [ResourceCategoryEntity.loads(r) for r in rs] if rs else None return rs def exam_kind_add(self, param: ExamKindAddParam): self.check_type(param, ExamKindAddParam) r = self.winney.exam_kind_add(json=param.json(), headers={"Authorization": self.token}) return ExamKindEntity.loads(self.get_data(r)) def exam_kind_update(self, param: ExamKindUpdateParam): self.check_type(param, ExamKindUpdateParam) r = self.winney.exam_kind_update( kind_id=param.kind_id, json=param.json(), headers={"Authorization": self.token}, ) return ExamKindEntity.loads(self.get_data(r)) def exam_kind_get(self, param: ExamKindEntity): self.check_type(param, ExamKindEntity) r = self.winney.exam_kind_get(kind_id=param.uid, headers={"Authorization": self.token}) return ExamKindEntity.loads(self.get_data(r)) def exam_kind_list(self): r = self.winney.exam_kind_list(headers={"Authorization": self.token}) kinds = self.get_data(r) kinds = [ExamKindEntity.loads(k) for k in kinds] if kinds else None return kinds def exam_add(self, param: ExamAddParam): self.check_type(param, ExamAddParam) r = self.winney.exam_add(json=param.json(), headers={"Authorization": self.token}) return ExamEntity.loads(self.get_data(r)) def exam_list(self, param: ExamListParam): self.check_type(param, ExamListParam) r = self.winney.exam_list(data=param.json(), headers={"Authorization": self.token}) rs = self.get_data(r)["result"] rs = [ExamEntity.loads(r) for r in rs] if rs else None return rs def exam_get(self, param: ExamEntity): r = self.winney.exam_get(exam_id=param.uid, headers={"Authorization": self.token}) return ExamEntity.loads(self.get_data(r)) def focus(self, param: FocusParam) -> FocusEntity: self.check_type(param, FocusParam) r = self.winney.focus(json=param.json(), headers={"Authorization": self.token}) return FocusEntity.loads(self.get_data(r)) def focus_list(self, param: FocusListParam) -> List[FocusEntity]: r = self.winney.focus_list(data=param.json(), headers={"Authorization": self.token}) rs = self.get_data(r)["result"] rs = [FocusEntity.loads(r) for r in rs] if rs else None return rs def focus_remove(self, param: FocusRemoveParam): r = self.winney.focus_remove(json=param.json()) print("Success to remove focus") def download_history(self, param: DownloadHistoryParam): r = self.winney.download_history(data=param.json(), headers={"Authorization": self.token}) rs = self.get_data(r)["result"] print(json.dumps(rs, indent=4)) rs = [DownloadHistoryEntity.loads(r) for r in rs] if rs else None return rs
def __init__(self): addr = Address(host="localhost", port=5000) self.winney = Winney(host="localhost", port=5000, addrs=[addr]) self.init_functions()
def winney_api(host): wy = Winney(host=host, port=PORT) wy.add_url(method="post", uri="/api/file", function_name="save_file") return wy
class Nacos(object): def __init__(self, addresses, namespace_id): self.addrs = [] for addr in addresses.split(","): addr = addr.split(":") host = addr[0] port = addr[1] if len(addr) > 1 else 8848 self.addrs.append(Address(host, port)) self.winney = Winney( host=self.addrs[0].host, port=self.addrs[0].port, addrs=self.addrs, ) self.namespace_id = namespace_id self.init_functions() def init_functions(self): # 用户管理 self.winney.register(method="post", name="user_create", uri="/nacos/v1/auth/users") self.winney.register(method="put", name="user_update", uri="/nacos/v1/auth/users") self.winney.register(method="delete", name="user_delete", uri="/nacos/v1/auth/users") self.winney.register(method="post", name="login", uri="/nacos/v1/auth/users/login") # 角色管理 self.winney.register(method="post", name="role_create", uri="/nacos/v1/auth/roles") self.winney.register(method="get", name="role_list", uri="/nacos/v1/auth/roles") self.winney.register(method="delete", name="role_delete", uri="/nacos/v1/auth/roles") # 权限管理 self.winney.register(method="post", name="permission_add", uri="/nacos/v1/auth/permissions") self.winney.register(method="get", name="permission_list", uri="/nacos/v1/auth/permissions") self.winney.register(method="delete", name="permission_delete", uri="/nacos/v1/auth/permissions") # 配置 self.winney.register(method="get", name="config_get", uri="/nacos/v1/cs/configs") self.winney.register(method="post", name="config_listen", uri="/nacos/v1/cs/configs/listener") self.winney.register(method="post", name="config_publish", uri="/nacos/v1/cs/configs") self.winney.register(method="delete", name="config_remove", uri="/nacos/v1/cs/configs") # 服务 self.winney.register(method="post", name="service_register", uri="/nacos/v1/ns/service") self.winney.register(method="delete", name="service_remove", uri="/nacos/v1/ns/service") self.winney.register(method="put", name="service_update", uri="/nacos/v1/ns/service") self.winney.register(method="get", name="service_get", uri="/nacos/v1/ns/service") self.winney.register(method="get", name="service_list", uri="/nacos/v1/ns/service/list") # 开关 self.winney.register(method="get", name="switch_get", uri="/nacos/v1/ns/operator/switches") self.winney.register(method="put", name="switch_update", uri="/nacos/v1/ns/operator/switches") # 实例 self.winney.register(method="post", name="instance_register", uri="/nacos/v1/ns/instance") self.winney.register(method="delete", name="instance_remove", uri="/nacos/v1/ns/instance") self.winney.register(method="put", name="instance_update", uri="/nacos/v1/ns/instance") self.winney.register(method="get", name="instance_list", uri="/nacos/v1/ns/instance/list") self.winney.register(method="get", name="instance_get", uri="/nacos/v1/ns/instance") self.winney.register(method="put", name="instance_beat", uri="/nacos/v1/ns/instance/beat") # 其他 self.winney.register(method="get", name="metrics_get", uri="/nacos/v1/ns/operator/metrics") self.winney.register(method="get", name="server_list", uri="/nacos/v1/ns/operator/servers") self.winney.register(method="get", name="leader_get", uri="/nacos/v1/ns/raft/leader") self.winney.register(method="put", name="health_update", uri="/nacos/v1/ns/health/instance") # 处理参数 def get_params(self, param: Param) -> Dict: if not isinstance(param, Param): raise ParamError( "param should be type of Param, but {} found".format( type(param))) params = param.json() params["namespaceId"] = self.namespace_id return params # 登陆 @retry def login(self, username, password) -> LoginResponse: r = self.winney.login(data={ "username": username, "password": password }) if not r.ok(): raise RequestError("failed to login, http status = {}".format( r.status_code)) return LoginResponse(**r.json()) # 创建用户 @retry def user_create(self, username, password) -> bool: r = self.winney.user_create(data={ "username": username, "password": password }) if not r.ok(): raise RequestError( "failed to create user, http status = {}".format( r.status_code)) return True # 更新用户信息 @retry def user_update(self, username, old_password, new_password) -> bool: r = self.winney.user_update( data={ "username": username, "oldPassword": old_password, "newPassword": new_password }) if not r.ok(): raise RequestError( "failed to update user, http status = {}".format( r.status_code)) return True # 删除用户 @retry def user_delete(self, username, password) -> bool: r = self.winney.user_delete(data={ "username": username, "password": password }) if not r.ok(): raise RequestError( "failed to delete user, http status = {}".format( r.status_code)) return True # 创建角色 @retry def role_create(self, username, role) -> bool: r = self.winney.role_create(data={"username": username, "role": role}) if not r.ok(): raise RequestError( "failed to create role, http status = {}".format( r.status_code)) return True # 获取用户的所有角色 @retry def role_list(self, username, page=1, page_size=100) -> RoleListResponse: r = self.winney.role_list(data={ "username": username, "pageNo": page, "pageSize": page_size }) if not r.ok(): raise RequestError("failed to list role, http status = {}".format( r.status_code)) return RoleListResponse(**r.json()) # 删除用户的角色 @retry def role_delete(self, username, role) -> bool: r = self.winney.role_delete(data={"username": username, "role": role}) if not r.ok(): raise RequestError( "failed to delete role, http status = {}".format( r.status_code)) return True # 给角色添加权限 @retry def permission_add(self, role, resource, action) -> bool: r = self.winney.permission_add(data={ "role": role, "resource": resource, "action": action }) if not r.ok(): raise RequestError( "failed to add permission, http status = {}".format( r.status_code)) return True # 从角色删除权限 @retry def permission_delete(self, role, resource, action) -> bool: r = self.winney.permission_delete(data={ "role": role, "resource": resource, "action": action }) if not r.ok(): raise RequestError( "failed to delete permission, http status = {}".format( r.status_code)) return True # 获取某个角色的权限 @retry def permission_list(self, role, page=1, page_size=100) -> PermissionListResponse: r = self.winney.permission_list(data={ "role": role, "pageNo": page, "pageSize": page_size }) if not r.ok(): raise RequestError( "failed to list permission, http status = {}".format( r.status_code)) return PermissionListResponse(**r.json()) # 注册服务 @retry def service_register(self, param: ServiceRegisterParam) -> bool: if not isinstance(param, ServiceRegisterParam): raise ParamError( "param should be type of ServiceRegisterParam, but {} found". format(type(param))) params = self.get_params(param) r = self.winney.service_register(data=params) if not r.ok(): raise RequestError( "failed to register service, http status = {}".format( r.status_code)) return True # 删除服务 @retry def service_remove(self, param: ServiceRemoveParam) -> bool: if not isinstance(param, ServiceRemoveParam): raise ParamError( "param should be type of ServiceRemoveParam, but {} found". format(type(param))) params = self.get_params(param) r = self.winney.service_remove(data=params) if not r.ok(): raise RequestError( "failed to remove service, http status = {}".format( r.status_code)) return True # 更新服务 @retry def service_update(self, param: ServiceUpdateParam) -> bool: if not isinstance(param, ServiceUpdateParam): raise ParamError( "param should be type of ServiceUpdateParam, but {} found". format(type(param))) params = self.get_params(param) r = self.winney.service_update(data=params) if not r.ok(): raise RequestError( "failed to update service, http status = {}".format( r.status_code)) return True # 获取服务详情 @retry def service_get(self, param: ServiceGetParam) -> ServiceEntity: if not isinstance(param, ServiceGetParam): raise ParamError( "param should be type of ServiceGetParam, but {} found".format( type(param))) params = self.get_params(param) r = self.winney.service_get(data=params) if not r.ok(): raise RequestError( "failed to get service, http status = {}".format( r.status_code)) return ServiceEntity.loads(**r.json()) # 获取服务列表 @retry def service_list(self, param: ServiceListParam) -> ServiceListEntity: if not isinstance(param, ServiceListParam): raise ParamError( "param should be type of ServiceListParam, but {} found". format(type(param))) params = self.get_params(param) r = self.winney.service_list(data=params) if not r.ok(): raise RequestError( "failed to list service, http status = {}".format( r.status_code)) return ServiceListEntity.loads(**r.json()) # 获取系统开关 @retry def switch_get(self) -> Dict: r = self.winney.switch_get() if not r.ok(): raise RequestError("failed to get switch, http status = {}".format( r.status_code)) return r.json() # 更新系统开关 @retry def switch_update(self, param: SwitchUpdateParam) -> bool: if not isinstance(param, SwitchUpdateParam): raise ParamError( "param should be type of SwitchUpdateParam, but {} found". format(type(param))) params = self.get_params(param) r = self.winney.service_list(data=params) if not r.ok(): raise RequestError( "failed to update switch, http status = {}".format( r.status_code)) return True # 注册实例 @retry def instance_register(self, param: InstanceRegisterParam) -> bool: if not isinstance(param, InstanceRegisterParam): raise ParamError( "param should be type of InstanceRegisterParam, but {} found". format(type(param))) params = self.get_params(param) r = self.winney.instance_register(data=params) if not r.ok(): raise RequestError( "failed to register instance, http status = {}".format( r.status_code)) return True # 删除实例 @retry def instance_remove(self, param: InstanceRemoveParam) -> bool: if not isinstance(param, InstanceRemoveParam): raise ParamError( "param should be type of InstanceRemoveParam, but {} found". format(type(param))) params = self.get_params(param) r = self.winney.instance_remove(data=params) if not r.ok(): raise RequestError( "failed to remove instance, http status = {}".format( r.status_code)) return True # 更新实例 @retry def instance_update(self, param: InstanceUpdateParam) -> bool: if not isinstance(param, InstanceUpdateParam): raise ParamError( "param should be type of InstanceUpdateParam, but {} found". format(type(param))) params = self.get_params(param) r = self.winney.instance_update(data=params) if not r.ok(): raise RequestError( "failed to update instance, http status = {}".format( r.status_code)) return True # 查询服务下的实例列表 @retry def instance_list(self, param: InstanceListParam) -> InstanceListEntity: if not isinstance(param, InstanceListParam): raise ParamError( "param should be type of InstanceListParam, but {} found". format(type(param))) params = self.get_params(param) r = self.winney.instance_list(data=params) if not r.ok(): raise RequestError( "failed to list instance, http status = {}".format( r.status_code)) return InstanceListEntity.loads(**r.json()) # 获取实例详情 @retry def instance_get(self, param: InstanceGetParam) -> InstanceEntity: if not isinstance(param, InstanceGetParam): raise ParamError( "param should be type of InstanceGetParam, but {} found". format(type(param))) params = self.get_params(param) r = self.winney.instance_get(data=params) if not r.ok(): raise RequestError( "failed to get instance, http status = {}".format( r.status_code)) return InstanceEntity.loads(**r.json()) # 实例发送心跳 @retry def instance_beat(self, beat_info: BeatInfo) -> bool: if not isinstance(beat_info, BeatInfo): raise ParamError( "beat_info should be type of BeatInfo, but {} found".format( type(beat_info))) param = InstanceBeatParam(beat_info) params = self.get_params(param) r = self.winney.instance_beat(data=params) if not r.ok(): raise RequestError( "failed to beat instance, http status = {}".format( r.status_code)) return True # 查看系统当前数据指标 @retry def metrics_get(self) -> MetricsEntity: r = self.winney.metrics_get() if not r.ok(): raise RequestError( "failed to get metrics, http status = {}".format( r.status_code)) return MetricsEntity.loads(**r.json()) # 查看集群 server 列表 @retry def server_list(self, param: ServerListParam) -> List[ServerEntity]: if not isinstance(param, ServerListParam): raise ParamError( "param should be type of ServerListParam, but {} found".format( type(param))) params = self.get_params(param) r = self.winney.instance_get(data=params) if not r.ok(): raise RequestError( "failed to list server, http status = {}".format( r.status_code)) servers = r.json()["servers"] servers = [ServerEntity.loads(**server) for server in servers] if servers else [] return servers # 查看当前集群 leader @retry def leader_get(self) -> LeaderEntity: r = self.winney.leader_get() if not r.ok(): raise RequestError("failed to get leader, http status = {}".format( r.status_code)) return LeaderEntity.loads(**r.json()) # 更新实例的健康状态 @retry def health_update(self, param: HealthUpdateParam) -> bool: if not isinstance(param, HealthUpdateParam): raise ParamError( "param should be type of HealthUpdateParam, but {} found". format(type(param))) params = self.get_params(param) r = self.winney.health_update(data=params) if not r.ok(): raise RequestError( "failed to update health, http status = {}".format( r.status_code)) return True # 获取配置 @retry def config_get(self, param: ConfigGetParam) -> str: if not isinstance(param, ConfigGetParam): raise ParamError( "param should be type of ConfigGetParam, but {} found".format( type(param))) params = param.to_json(namespace_id=self.namespace_id) r = self.winney.config_get(data=params) if not r.ok(): raise RequestError("failed to get config, http status = {}".format( r.status_code)) return r.get_text() # 监听配置 @retry def config_listen(self, param: ConfigListenParam) -> str: """监听 30s,返回空串表示无变化""" if not isinstance(param, ConfigListenParam): raise ParamError( "param should be type of ConfigListenParam, but {} found". format(type(param))) params = param.to_json(self.namespace_id) r = self.winney.config_listen( data=params, headers={"Long-Pulling-Timeout": "30000"}) if not r.ok(): raise RequestError( "failed to listen config, http status = {}".format( r.status_code)) return r.get_text() # 发布配置 @retry def config_publish(self, param: ConfigPublishParam) -> bool: if not isinstance(param, ConfigPublishParam): raise ParamError( "param should be type of ConfigPublishParam, but {} found". format(type(param))) params = param.to_json(namespace_id=self.namespace_id) r = self.winney.config_publish(data=params) if not r.ok(): raise RequestError( "failed to publish config, http status = {}".format( r.status_code)) return True # 删除配置 @retry def config_remove(self, param: ConfigRemoveParam) -> bool: if not isinstance(param, ConfigRemoveParam): raise ParamError( "param should be type of ConfigRemoveParam, but {} found". format(type(param))) params = param.to_json(namespace_id=self.namespace_id) r = self.winney.config_remove(data=params) if not r.ok(): raise RequestError( "failed to remove config, http status = {}".format( r.status_code)) return True