Skip to content

Commit

Permalink
DP-5182 go-sdk: fusion traffic api adds 'type' parameter (#151)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
yuansl authored Oct 26, 2024
1 parent f0db9ef commit 9b72b00
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 7 deletions.
51 changes: 49 additions & 2 deletions cdn/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ func NewCdnManager(mac *auth.Credentials) *CdnManager {
// EndDate 结束日期,格式例如:2016-07-03
// Granularity 取值粒度,取值可选值:5min/hour/day
// Domains 域名列表,彼此用 ; 连接
// DataType 计量数据类型, 可选 'bandwidth'(静态cdn带宽,默认)..., 参考 [DataType]
type TrafficReq struct {
StartDate string `json:"startDate"`
EndDate string `json:"endDate"`
Granularity string `json:"granularity"`
Domains string `json:"domains"`
DataType string `json:"type,omitempty"`
}

// TrafficResp 为带宽/流量查询响应内容
Expand All @@ -57,21 +59,49 @@ type TrafficData struct {
DomainOversea []int `json:"oversea"`
}

type options struct {
dataType DataType
}

func _WithDataType(dataType DataType) Option {
return OptionFunc(func(opt interface{}) {
opt.(*options).dataType = dataType
})
}

type BandwidthOption Option

func WithBandwidthDataType(dataType DataType) BandwidthOption {
if DataTypeBandwidth <= dataType && dataType <= DataType302mBandwidth {
return _WithDataType(dataType)
}
panic("cdn: invalid DataType for GetBandwidthData: " + dataType.String())
}

// GetBandwidthData 方法用来获取域名访问带宽数据
//
// StartDate string 必须 开始日期,例如:2016-07-01
// EndDate string 必须 结束日期,例如:2016-07-03
// Granularity string 必须 粒度,取值:5min / hour /day
// Domains []string 必须 域名列表
// Opts 非必须 可选项

func (m *CdnManager) GetBandwidthData(startDate, endDate, granularity string,
domainList []string) (bandwidthData TrafficResp, err error) {
domainList []string, opts ...BandwidthOption) (bandwidthData TrafficResp, err error) {
var options options
for _, opt := range opts {
opt.Apply(&options)
}
domains := strings.Join(domainList, ";")
reqBody := TrafficReq{
StartDate: startDate,
EndDate: endDate,
Granularity: granularity,
Domains: domains,
}
if options.dataType.Valid() {
reqBody.DataType = options.dataType.String()
}

resData, reqErr := postRequest(m.mac, "/v2/tune/bandwidth", reqBody)
if reqErr != nil {
Expand All @@ -86,21 +116,38 @@ func (m *CdnManager) GetBandwidthData(startDate, endDate, granularity string,
return
}

type FluxOption Option

func WithFluxDataType(dataType DataType) FluxOption {
if DataTypeFlow <= dataType && dataType <= DataType302mFlow {
return _WithDataType(dataType)
}
panic("cdn: invalid DataType for GetFluxData: " + dataType.String())
}

// GetFluxData 方法用来获取域名访问流量数据
//
// StartDate string 必须 开始日期,例如:2016-07-01
// EndDate string 必须 结束日期,例如:2016-07-03
// Granularity string 必须 粒度,取值:5min / hour /day
// Domains []string 必须 域名列表
// Opts 非必须 可选项
func (m *CdnManager) GetFluxData(startDate, endDate, granularity string,
domainList []string) (fluxData TrafficResp, err error) {
domainList []string, opts ...FluxOption) (fluxData TrafficResp, err error) {
var options options
for _, opt := range opts {
opt.Apply(&options)
}
domains := strings.Join(domainList, ";")
reqBody := TrafficReq{
StartDate: startDate,
EndDate: endDate,
Granularity: granularity,
Domains: domains,
}
if options.dataType.Valid() {
reqBody.DataType = options.dataType.String()
}

resData, reqErr := postRequest(m.mac, "/v2/tune/flux", reqBody)
if reqErr != nil {
Expand Down
14 changes: 9 additions & 5 deletions cdn/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func TestGetBandwidthData(t *testing.T) {
startDate string
endDate string
granularity string
dataType DataType
domainList []string
}

Expand All @@ -66,6 +67,7 @@ func TestGetBandwidthData(t *testing.T) {
startDate,
endDate,
"5min",
DataTypeBandwidth,
[]string{domain},
},
wantCode: 200,
Expand All @@ -75,7 +77,7 @@ func TestGetBandwidthData(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ret, err := cdnManager.GetBandwidthData(tc.args.startDate, tc.args.endDate,
tc.args.granularity, tc.args.domainList)
tc.args.granularity, tc.args.domainList, WithBandwidthDataType(tc.args.dataType))
if err != nil || ret.Code != tc.wantCode {
t.Errorf("GetBandwidth() error = %v, %v", err, ret.Error)
return
Expand All @@ -90,6 +92,7 @@ func TestGetFluxData(t *testing.T) {
startDate string
endDate string
granularity string
dataType DataType
domainList []string
}

Expand All @@ -104,6 +107,7 @@ func TestGetFluxData(t *testing.T) {
startDate,
endDate,
"5min",
DataTypeFlow,
[]string{domain},
},
wantCode: 200,
Expand All @@ -113,7 +117,7 @@ func TestGetFluxData(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ret, err := cdnManager.GetFluxData(tc.args.startDate, tc.args.endDate,
tc.args.granularity, tc.args.domainList)
tc.args.granularity, tc.args.domainList, WithFluxDataType(tc.args.dataType))
if err != nil || ret.Code != tc.wantCode {
t.Errorf("GetFlux() error = %v, %v", err, ret.Error)
return
Expand Down Expand Up @@ -189,8 +193,8 @@ func TestRefreshDirs(t *testing.T) {
}

/* 预取有额度限制
//TestPrefetchUrls
func TestPrefetchUrls(t *testing.T) {
//TestPrefetchUrls
func TestPrefetchUrls(t *testing.T) {
type args struct {
urls []string
}
Expand Down Expand Up @@ -218,7 +222,7 @@ func TestPrefetchUrls(t *testing.T) {
}
})
}
}
}
*/

// TestGetCdnLogList
Expand Down
32 changes: 32 additions & 0 deletions cdn/data_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cdn

//go:generate stringer -type DataType -linecomment
type DataType int

const (
// 静态cdn带宽(bps)
DataTypeBandwidth DataType = iota + 1 // bandwidth
// 302cdn带宽(bps)
DataType302Bandwidth // 302bandwidth
// 302MIX带宽(bps)
DataType302mBandwidth // 302mbandwidth
// 静态cdn流量(bytes)
DataTypeFlow // flow
// 302cdn流量(bytes)
DataType302Flow // 302flow
// 302MIX流量(bytes)
DataType302mFlow // 302mflow
)

func DataTypeOf(datatype string) DataType {
for d := DataTypeBandwidth; d <= DataType302mFlow; d++ {
if d.String() == datatype {
return d
}
}
return -1
}

func (d DataType) Valid() bool {
return DataTypeBandwidth <= d && d <= DataType302mFlow
}
29 changes: 29 additions & 0 deletions cdn/datatype_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions cdn/option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package cdn

type Option interface {
Apply(opt interface{})
}

type OptionFunc func(interface{})

func (f OptionFunc) Apply(opt interface{}) {
f(opt)
}

0 comments on commit 9b72b00

Please sign in to comment.