@@ -36,11 +36,13 @@ func NewCdnManager(mac *auth.Credentials) *CdnManager {
3636// EndDate 结束日期,格式例如:2016-07-03
3737// Granularity 取值粒度,取值可选值:5min/hour/day
3838// Domains 域名列表,彼此用 ; 连接
39+ // DataType 计量数据类型, 可选 'bandwidth'(静态cdn带宽,默认)..., 参考 [DataType]
3940type TrafficReq struct {
4041 StartDate string `json:"startDate"`
4142 EndDate string `json:"endDate"`
4243 Granularity string `json:"granularity"`
4344 Domains string `json:"domains"`
45+ DataType string `json:"type,omitempty"`
4446}
4547
4648// TrafficResp 为带宽/流量查询响应内容
@@ -57,21 +59,49 @@ type TrafficData struct {
5759 DomainOversea []int `json:"oversea"`
5860}
5961
62+ type options struct {
63+ dataType DataType
64+ }
65+
66+ func _WithDataType (dataType DataType ) Option {
67+ return OptionFunc (func (opt interface {}) {
68+ opt .(* options ).dataType = dataType
69+ })
70+ }
71+
72+ type BandwidthOption Option
73+
74+ func WithBandwidthDataType (dataType DataType ) BandwidthOption {
75+ if DataTypeBandwidth <= dataType && dataType <= DataType302mBandwidth {
76+ return _WithDataType (dataType )
77+ }
78+ panic ("cdn: invalid DataType for GetBandwidthData: " + dataType .String ())
79+ }
80+
6081// GetBandwidthData 方法用来获取域名访问带宽数据
6182//
6283// StartDate string 必须 开始日期,例如:2016-07-01
6384// EndDate string 必须 结束日期,例如:2016-07-03
6485// Granularity string 必须 粒度,取值:5min / hour /day
6586// Domains []string 必须 域名列表
87+ // Opts 非必须 可选项
88+
6689func (m * CdnManager ) GetBandwidthData (startDate , endDate , granularity string ,
67- domainList []string ) (bandwidthData TrafficResp , err error ) {
90+ domainList []string , opts ... BandwidthOption ) (bandwidthData TrafficResp , err error ) {
91+ var options options
92+ for _ , opt := range opts {
93+ opt .Apply (& options )
94+ }
6895 domains := strings .Join (domainList , ";" )
6996 reqBody := TrafficReq {
7097 StartDate : startDate ,
7198 EndDate : endDate ,
7299 Granularity : granularity ,
73100 Domains : domains ,
74101 }
102+ if options .dataType .Valid () {
103+ reqBody .DataType = options .dataType .String ()
104+ }
75105
76106 resData , reqErr := postRequest (m .mac , "/v2/tune/bandwidth" , reqBody )
77107 if reqErr != nil {
@@ -86,21 +116,38 @@ func (m *CdnManager) GetBandwidthData(startDate, endDate, granularity string,
86116 return
87117}
88118
119+ type FluxOption Option
120+
121+ func WithFluxDataType (dataType DataType ) FluxOption {
122+ if DataTypeFlow <= dataType && dataType <= DataType302mFlow {
123+ return _WithDataType (dataType )
124+ }
125+ panic ("cdn: invalid DataType for GetFluxData: " + dataType .String ())
126+ }
127+
89128// GetFluxData 方法用来获取域名访问流量数据
90129//
91130// StartDate string 必须 开始日期,例如:2016-07-01
92131// EndDate string 必须 结束日期,例如:2016-07-03
93132// Granularity string 必须 粒度,取值:5min / hour /day
94133// Domains []string 必须 域名列表
134+ // Opts 非必须 可选项
95135func (m * CdnManager ) GetFluxData (startDate , endDate , granularity string ,
96- domainList []string ) (fluxData TrafficResp , err error ) {
136+ domainList []string , opts ... FluxOption ) (fluxData TrafficResp , err error ) {
137+ var options options
138+ for _ , opt := range opts {
139+ opt .Apply (& options )
140+ }
97141 domains := strings .Join (domainList , ";" )
98142 reqBody := TrafficReq {
99143 StartDate : startDate ,
100144 EndDate : endDate ,
101145 Granularity : granularity ,
102146 Domains : domains ,
103147 }
148+ if options .dataType .Valid () {
149+ reqBody .DataType = options .dataType .String ()
150+ }
104151
105152 resData , reqErr := postRequest (m .mac , "/v2/tune/flux" , reqBody )
106153 if reqErr != nil {
0 commit comments