From f3a0b9b49518d414c5f4444dc17c2d94467bf5f7 Mon Sep 17 00:00:00 2001 From: yuansl Date: Mon, 9 Dec 2024 11:31:37 +0800 Subject: [PATCH] =?UTF-8?q?DP-5290=20python-sdk/cdn:=20=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E5=9F=9F=E5=90=8D=E5=B8=A6=E5=AE=BD=EF=BC=8C=E6=94=AF=E6=8C=81?= =?UTF-8?q?type=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - cdn/manager: add DataType class(enum), add optional parameter 'data_type' to `CdnManager.get_bandwidth_data` & `CdnManager.get_flux_data` - see https://jira.qiniu.io/browse/DP-5290 --- examples/cdn_bandwidth.py | 8 +++++++- qiniu/__init__.py | 2 +- qiniu/services/cdn/manager.py | 20 +++++++++++++++++--- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/examples/cdn_bandwidth.py b/examples/cdn_bandwidth.py index ad4e97a8..32fbb40b 100644 --- a/examples/cdn_bandwidth.py +++ b/examples/cdn_bandwidth.py @@ -5,7 +5,7 @@ 查询指定域名指定时间段内的带宽 """ import qiniu -from qiniu import CdnManager +from qiniu import CdnManager, DataType # 账户ak,sk @@ -31,3 +31,9 @@ print(ret) print(info) + +ret, info = cdn_manager.get_bandwidth_data( + urls, startDate, endDate, granularity, data_type=DataType.BANDWIDTH) + +print(ret) +print(info) diff --git a/qiniu/__init__.py b/qiniu/__init__.py index 1ae68c00..a18aac2c 100644 --- a/qiniu/__init__.py +++ b/qiniu/__init__.py @@ -21,7 +21,7 @@ build_batch_stat, build_batch_delete, build_batch_restoreAr, build_batch_restore_ar from .services.storage.uploader import put_data, put_file, put_stream from .services.storage.upload_progress_recorder import UploadProgressRecorder -from .services.cdn.manager import CdnManager, create_timestamp_anti_leech_url, DomainManager +from .services.cdn.manager import CdnManager, DataType, create_timestamp_anti_leech_url, DomainManager from .services.processing.pfop import PersistentFop from .services.processing.cmd import build_op, pipe_cmd, op_save from .services.compute.app import AccountClient diff --git a/qiniu/services/cdn/manager.py b/qiniu/services/cdn/manager.py index bf107926..8ba944e5 100644 --- a/qiniu/services/cdn/manager.py +++ b/qiniu/services/cdn/manager.py @@ -5,9 +5,17 @@ from qiniu.compat import is_py2 from qiniu.compat import is_py3 +from enum import Enum import hashlib +class DataType(Enum): + BANDWIDTH = 'bandwidth' + X302BANDWIDTH = '302bandwidth' + X302MBANDWIDTH = '302mbandwidth' + FLOW = 'flow' + X302FLOW = '302flow' + X302MFLOW = '302mflow' def urlencode(str): if is_py2: @@ -60,7 +68,7 @@ def refresh_urls_and_dirs(self, urls, dirs): Returns: 一个dict变量和一个ResponseInfo对象 参考代码 examples/cdn_manager.py - """ + """ req = {} if urls is not None and len(urls) > 0: req.update({"urls": urls}) @@ -89,7 +97,7 @@ def prefetch_urls(self, urls): url = '{0}/v2/tune/prefetch'.format(self.server) return self.__post(url, body) - def get_bandwidth_data(self, domains, start_date, end_date, granularity): + def get_bandwidth_data(self, domains, start_date, end_date, granularity, data_type=None): """ 查询带宽数据,文档 https://developer.qiniu.com/fusion/api/traffic-bandwidth @@ -98,6 +106,7 @@ def get_bandwidth_data(self, domains, start_date, end_date, granularity): start_date: 起始日期 end_date: 结束日期 granularity: 数据间隔 + data_type: 计量数据类型, see class DataType.XXXBANDWIDTH Returns: 一个dict变量和一个ResponseInfo对象 @@ -108,12 +117,14 @@ def get_bandwidth_data(self, domains, start_date, end_date, granularity): req.update({"startDate": start_date}) req.update({"endDate": end_date}) req.update({"granularity": granularity}) + if data_type is not None: + req.update({'type': data_type.value}) # should be one of 'bandwidth', '302bandwidth', '302mbandwidth' body = json.dumps(req) url = '{0}/v2/tune/bandwidth'.format(self.server) return self.__post(url, body) - def get_flux_data(self, domains, start_date, end_date, granularity): + def get_flux_data(self, domains, start_date, end_date, granularity, data_type=None): """ 查询流量数据,文档 https://developer.qiniu.com/fusion/api/traffic-bandwidth @@ -122,6 +133,7 @@ def get_flux_data(self, domains, start_date, end_date, granularity): start_date: 起始日期 end_date: 结束日期 granularity: 数据间隔 + data_type: 计量数据类型, see class DataType.XXXFLOW Returns: 一个dict变量和一个ResponseInfo对象 @@ -132,6 +144,8 @@ def get_flux_data(self, domains, start_date, end_date, granularity): req.update({"startDate": start_date}) req.update({"endDate": end_date}) req.update({"granularity": granularity}) + if data_type is not None: + req.update({'type': data_type.value}) # should be one of 'flow', '302flow', '302mflow' body = json.dumps(req) url = '{0}/v2/tune/flux'.format(self.server)