def _low_layer_execute(request: CloudSDKLowLayerRequest): """ 执行单个底层请求 :param request: 内部请求对象 :return: 响应结果 """ # 获取原生 sdk 对象 csp = request.parent.csp action = request.parent.action sdk_name = request.parent.csp_conf['native_sdk'] native_sdk_path = f'cloud.native_sdk.{csp}.{sdk_name}' native_sdk = dynamic_import_class(native_sdk_path)() # 设置并请求 native_sdk.set(request.parent.interface_conf, request.params) req_limit = request.parent.csp_conf['req_limit'] key = f'{csp}:{action}' if not semaphore_map.get(key): semaphore_map[key] = BoundedSemaphore(req_limit) sp = semaphore_map.get(key) # 用于计算消耗时间 start_time = datetime_to_timestamp() try_time = 3 resp = None # 请求重试机制 while try_time: with sp: resp = native_sdk.request() # 得到消耗时间 end_time = datetime_to_timestamp() expend_time = end_time - start_time # 将请求消耗时间控制在1秒左右,防止请求频率过高 if expend_time < 1: time.sleep(1 - expend_time) if 'Error' in resp: try_time -= 1 else: break # 预处理原始数据 cleaner_name = request.parent.csp_conf['cleaner'] cleaner_path = f'cloud.cleaner.{csp}.{cleaner_name}' cleaner = dynamic_import_class(cleaner_path)(request) data = cleaner.clean(resp) # 返回数据 return data
def _get_client(self) -> AbstractClient: """ 初始化客户端,并返回客户端对象 :return: 客户端对象 """ # 获取认证对象 if not self._credential: self._credential = self._get_credential() # 获取客户端配置,包括 http 配置 if not self._client_config: if not self._http_config: self._http_config = self._get_http_config() self._client_config = ClientProfile(httpProfile=self._http_config) # 获取接口信息 md = self._interface['module'] version = self._interface['version'] # 动态获取客户端类 client_module_path = f'tencentcloud.{md}.{version}.{md}_client' client_class_name = f'{md.capitalize()}Client' client_class = dynamic_import_class( f'{client_module_path}.{client_class_name}') # 实例化客户端对象 region = self._params.get('Region') return client_class(self._credential, region, self._client_config)
def add_interval_job(self, job_cls: str, interval_kwargs: dict, *args, **kwargs) -> dict: """ 添加间隔性任务 :param job_cls: 作业类 :param interval_kwargs: 间隔参数 :param args: 作业初始化位置参数 :param kwargs: 作业初始化关键字参数 :return: 任务名字对应 id 的字段 """ # 获取作业类 job_cls = dynamic_import_class(job_cls) job_ins = job_cls(*args, **kwargs) # 构造调度器的作业添加参数 func_kwargs = {'func': job_ins.run, 'args': None, 'kwargs': None} job_kwargs = {'id': job_ins.id, 'name': job_ins.name} trigger_kwargs = { 'trigger': 'interval', "start_date": None, "timezone": settings.TIME_ZONE, **interval_kwargs } # 添加作业 job = self._scheduler.add_job(**{ **func_kwargs, **trigger_kwargs, **job_kwargs }) return {job.name: job.id}
def _get_req(self) -> AbstractModel: """ 生成请求对象 :return: 请求对象 """ # 获取接口信息 name = self._interface['name'] md = self._interface['module'] version = self._interface['version'] # 动态获取请求类 req_class_path = f'tencentcloud.{md}.{version}.models' req_class_name = f'{name}Request' req_class = dynamic_import_class(f'{req_class_path}.{req_class_name}') # 实例化请求对象,并导入请求参数 assert req_class, 'request class has not been import, check cloud_interface object' req = req_class() param_json = safe_json_dumps(self._params) req.from_json_string(param_json) return req
def _get_req(self) -> AcsRequest: """ 生成请求对象 :return: 请求对象 """ # 提取接口信息 name = self._interface['name'] md = self._interface['module'] version = self._interface['version'] # 动态获取请求类 req_class_path = f'aliyunsdk{md}.request.{version}.{name}Request' req_class_name = f'{name}Request' req_class = dynamic_import_class(f'{req_class_path}.{req_class_name}') # 实例化请求对象,并导入请求参数 assert req_class, f'request class {req_class_path}.{req_class_name} has not been import' request = req_class() request.set_query_params(self._params) request.set_accept_format("json") return request