Skip to content

Commit 9b72b00

Browse files
authored
DP-5182 go-sdk: fusion traffic api adds 'type' parameter (#151)
* DP-5182 go-sdk: fusion traffic api adds 'type' parameter - cdn: add 'BandwidthOption' & 'FluxOption' struct - see https://jira.qiniu.io/browse/DP-5182 * cdn: remove useless printf line
1 parent f0db9ef commit 9b72b00

File tree

5 files changed

+130
-7
lines changed

5 files changed

+130
-7
lines changed

cdn/api.go

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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]
3940
type 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+
6689
func (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 非必须 可选项
95135
func (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 {

cdn/api_test.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func TestGetBandwidthData(t *testing.T) {
5252
startDate string
5353
endDate string
5454
granularity string
55+
dataType DataType
5556
domainList []string
5657
}
5758

@@ -66,6 +67,7 @@ func TestGetBandwidthData(t *testing.T) {
6667
startDate,
6768
endDate,
6869
"5min",
70+
DataTypeBandwidth,
6971
[]string{domain},
7072
},
7173
wantCode: 200,
@@ -75,7 +77,7 @@ func TestGetBandwidthData(t *testing.T) {
7577
for _, tc := range testCases {
7678
t.Run(tc.name, func(t *testing.T) {
7779
ret, err := cdnManager.GetBandwidthData(tc.args.startDate, tc.args.endDate,
78-
tc.args.granularity, tc.args.domainList)
80+
tc.args.granularity, tc.args.domainList, WithBandwidthDataType(tc.args.dataType))
7981
if err != nil || ret.Code != tc.wantCode {
8082
t.Errorf("GetBandwidth() error = %v, %v", err, ret.Error)
8183
return
@@ -90,6 +92,7 @@ func TestGetFluxData(t *testing.T) {
9092
startDate string
9193
endDate string
9294
granularity string
95+
dataType DataType
9396
domainList []string
9497
}
9598

@@ -104,6 +107,7 @@ func TestGetFluxData(t *testing.T) {
104107
startDate,
105108
endDate,
106109
"5min",
110+
DataTypeFlow,
107111
[]string{domain},
108112
},
109113
wantCode: 200,
@@ -113,7 +117,7 @@ func TestGetFluxData(t *testing.T) {
113117
for _, tc := range testCases {
114118
t.Run(tc.name, func(t *testing.T) {
115119
ret, err := cdnManager.GetFluxData(tc.args.startDate, tc.args.endDate,
116-
tc.args.granularity, tc.args.domainList)
120+
tc.args.granularity, tc.args.domainList, WithFluxDataType(tc.args.dataType))
117121
if err != nil || ret.Code != tc.wantCode {
118122
t.Errorf("GetFlux() error = %v, %v", err, ret.Error)
119123
return
@@ -189,8 +193,8 @@ func TestRefreshDirs(t *testing.T) {
189193
}
190194

191195
/* 预取有额度限制
192-
//TestPrefetchUrls
193-
func TestPrefetchUrls(t *testing.T) {
196+
//TestPrefetchUrls
197+
func TestPrefetchUrls(t *testing.T) {
194198
type args struct {
195199
urls []string
196200
}
@@ -218,7 +222,7 @@ func TestPrefetchUrls(t *testing.T) {
218222
}
219223
})
220224
}
221-
}
225+
}
222226
*/
223227

224228
// TestGetCdnLogList

cdn/data_type.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package cdn
2+
3+
//go:generate stringer -type DataType -linecomment
4+
type DataType int
5+
6+
const (
7+
// 静态cdn带宽(bps)
8+
DataTypeBandwidth DataType = iota + 1 // bandwidth
9+
// 302cdn带宽(bps)
10+
DataType302Bandwidth // 302bandwidth
11+
// 302MIX带宽(bps)
12+
DataType302mBandwidth // 302mbandwidth
13+
// 静态cdn流量(bytes)
14+
DataTypeFlow // flow
15+
// 302cdn流量(bytes)
16+
DataType302Flow // 302flow
17+
// 302MIX流量(bytes)
18+
DataType302mFlow // 302mflow
19+
)
20+
21+
func DataTypeOf(datatype string) DataType {
22+
for d := DataTypeBandwidth; d <= DataType302mFlow; d++ {
23+
if d.String() == datatype {
24+
return d
25+
}
26+
}
27+
return -1
28+
}
29+
30+
func (d DataType) Valid() bool {
31+
return DataTypeBandwidth <= d && d <= DataType302mFlow
32+
}

cdn/datatype_string.go

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cdn/option.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package cdn
2+
3+
type Option interface {
4+
Apply(opt interface{})
5+
}
6+
7+
type OptionFunc func(interface{})
8+
9+
func (f OptionFunc) Apply(opt interface{}) {
10+
f(opt)
11+
}

0 commit comments

Comments
 (0)