Skip to content

Commit f3a0b9b

Browse files
committed
DP-5290 python-sdk/cdn: 查询域名带宽,支持type参数
- 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
1 parent fca7849 commit f3a0b9b

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

examples/cdn_bandwidth.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
查询指定域名指定时间段内的带宽
66
"""
77
import qiniu
8-
from qiniu import CdnManager
8+
from qiniu import CdnManager, DataType
99

1010

1111
# 账户ak,sk
@@ -31,3 +31,9 @@
3131

3232
print(ret)
3333
print(info)
34+
35+
ret, info = cdn_manager.get_bandwidth_data(
36+
urls, startDate, endDate, granularity, data_type=DataType.BANDWIDTH)
37+
38+
print(ret)
39+
print(info)

qiniu/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
build_batch_stat, build_batch_delete, build_batch_restoreAr, build_batch_restore_ar
2222
from .services.storage.uploader import put_data, put_file, put_stream
2323
from .services.storage.upload_progress_recorder import UploadProgressRecorder
24-
from .services.cdn.manager import CdnManager, create_timestamp_anti_leech_url, DomainManager
24+
from .services.cdn.manager import CdnManager, DataType, create_timestamp_anti_leech_url, DomainManager
2525
from .services.processing.pfop import PersistentFop
2626
from .services.processing.cmd import build_op, pipe_cmd, op_save
2727
from .services.compute.app import AccountClient

qiniu/services/cdn/manager.py

+17-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,17 @@
55

66
from qiniu.compat import is_py2
77
from qiniu.compat import is_py3
8+
from enum import Enum
89

910
import hashlib
1011

12+
class DataType(Enum):
13+
BANDWIDTH = 'bandwidth'
14+
X302BANDWIDTH = '302bandwidth'
15+
X302MBANDWIDTH = '302mbandwidth'
16+
FLOW = 'flow'
17+
X302FLOW = '302flow'
18+
X302MFLOW = '302mflow'
1119

1220
def urlencode(str):
1321
if is_py2:
@@ -60,7 +68,7 @@ def refresh_urls_and_dirs(self, urls, dirs):
6068
Returns:
6169
一个dict变量和一个ResponseInfo对象
6270
参考代码 examples/cdn_manager.py
63-
"""
71+
"""
6472
req = {}
6573
if urls is not None and len(urls) > 0:
6674
req.update({"urls": urls})
@@ -89,7 +97,7 @@ def prefetch_urls(self, urls):
8997
url = '{0}/v2/tune/prefetch'.format(self.server)
9098
return self.__post(url, body)
9199

92-
def get_bandwidth_data(self, domains, start_date, end_date, granularity):
100+
def get_bandwidth_data(self, domains, start_date, end_date, granularity, data_type=None):
93101
"""
94102
查询带宽数据,文档 https://developer.qiniu.com/fusion/api/traffic-bandwidth
95103
@@ -98,6 +106,7 @@ def get_bandwidth_data(self, domains, start_date, end_date, granularity):
98106
start_date: 起始日期
99107
end_date: 结束日期
100108
granularity: 数据间隔
109+
data_type: 计量数据类型, see class DataType.XXXBANDWIDTH
101110
102111
Returns:
103112
一个dict变量和一个ResponseInfo对象
@@ -108,12 +117,14 @@ def get_bandwidth_data(self, domains, start_date, end_date, granularity):
108117
req.update({"startDate": start_date})
109118
req.update({"endDate": end_date})
110119
req.update({"granularity": granularity})
120+
if data_type is not None:
121+
req.update({'type': data_type.value}) # should be one of 'bandwidth', '302bandwidth', '302mbandwidth'
111122

112123
body = json.dumps(req)
113124
url = '{0}/v2/tune/bandwidth'.format(self.server)
114125
return self.__post(url, body)
115126

116-
def get_flux_data(self, domains, start_date, end_date, granularity):
127+
def get_flux_data(self, domains, start_date, end_date, granularity, data_type=None):
117128
"""
118129
查询流量数据,文档 https://developer.qiniu.com/fusion/api/traffic-bandwidth
119130
@@ -122,6 +133,7 @@ def get_flux_data(self, domains, start_date, end_date, granularity):
122133
start_date: 起始日期
123134
end_date: 结束日期
124135
granularity: 数据间隔
136+
data_type: 计量数据类型, see class DataType.XXXFLOW
125137
126138
Returns:
127139
一个dict变量和一个ResponseInfo对象
@@ -132,6 +144,8 @@ def get_flux_data(self, domains, start_date, end_date, granularity):
132144
req.update({"startDate": start_date})
133145
req.update({"endDate": end_date})
134146
req.update({"granularity": granularity})
147+
if data_type is not None:
148+
req.update({'type': data_type.value}) # should be one of 'flow', '302flow', '302mflow'
135149

136150
body = json.dumps(req)
137151
url = '{0}/v2/tune/flux'.format(self.server)

0 commit comments

Comments
 (0)