mirror of
https://github.com/eiblog/eiblog.git
synced 2026-02-16 19:32:27 +08:00
update vendor
This commit is contained in:
35
vendor/github.com/qiniu/api.v7/cdn/anti_leech.go
generated
vendored
Normal file
35
vendor/github.com/qiniu/api.v7/cdn/anti_leech.go
generated
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
package cdn
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
// CreateTimestampAntileechURL 构建带时间戳防盗链的链接
|
||||
// encryptKey 七牛防盗链key
|
||||
func CreateTimestampAntileechURL(urlStr string, encryptKey string, durationInSeconds int64) (antileechURL string, err error) {
|
||||
|
||||
u, err := url.Parse(urlStr)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
expireTime := time.Now().Add(time.Second * time.Duration(durationInSeconds)).Unix()
|
||||
toSignStr := fmt.Sprintf("%s%s%x", encryptKey, u.EscapedPath(), expireTime)
|
||||
signedStr := fmt.Sprintf("%x", md5.Sum([]byte(toSignStr)))
|
||||
|
||||
q := url.Values{}
|
||||
q.Add("sign", signedStr)
|
||||
q.Add("t", fmt.Sprintf("%x", expireTime))
|
||||
|
||||
if u.RawQuery == "" {
|
||||
antileechURL = u.String() + "?" + q.Encode()
|
||||
} else {
|
||||
|
||||
antileechURL = u.String() + "&" + q.Encode()
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
37
vendor/github.com/qiniu/api.v7/cdn/anti_leech_test.go
generated
vendored
Normal file
37
vendor/github.com/qiniu/api.v7/cdn/anti_leech_test.go
generated
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
package cdn
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCreateTimestampAntiLeech(t *testing.T) {
|
||||
type args struct {
|
||||
urlStr string
|
||||
encryptKey string
|
||||
durationInSeconds int64
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "antileech_1",
|
||||
args: args{
|
||||
urlStr: "http://www.abc.com/abc.jpg?stat",
|
||||
encryptKey: "abc",
|
||||
durationInSeconds: 20,
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, err := CreateTimestampAntileechURL(tt.args.urlStr, tt.args.encryptKey, tt.args.durationInSeconds)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("CreateTimestampAntiLeech() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
260
vendor/github.com/qiniu/api.v7/cdn/api.go
generated
vendored
Normal file
260
vendor/github.com/qiniu/api.v7/cdn/api.go
generated
vendored
Normal file
@@ -0,0 +1,260 @@
|
||||
package cdn
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/qiniu/api.v7/auth/qbox"
|
||||
. "github.com/qiniu/api.v7/conf"
|
||||
)
|
||||
|
||||
var (
|
||||
FUSION_HOST = "http://fusion.qiniuapi.com"
|
||||
)
|
||||
|
||||
/* TrafficReqBody
|
||||
|
||||
批量查询带宽/流量 请求内容
|
||||
|
||||
StartDate string 开始日期,例如:2016-07-01
|
||||
EndDate string 结束日期,例如:2016-07-03
|
||||
Granularity string 粒度,取值:5min / hour /day
|
||||
Domains string 域名列表,以 ;分割
|
||||
*/
|
||||
type TrafficReqBody struct {
|
||||
StartDate string `json:"startDate"`
|
||||
EndDate string `json:"endDate"`
|
||||
Granularity string `json:"granularity"`
|
||||
Domains string `json:"domains"`
|
||||
}
|
||||
|
||||
// TrafficResp
|
||||
// 带宽/流量查询响应内容
|
||||
type TrafficResp struct {
|
||||
Code int `json:"code"`
|
||||
Error string `json:"error"`
|
||||
Time []string `json:"time,omitempty"`
|
||||
Data map[string]TrafficRespData `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
// TrafficRespData
|
||||
// 带宽/流量数据
|
||||
type TrafficRespData struct {
|
||||
DomainChina []int `json:"china"`
|
||||
DomainOversea []int `json:"oversea"`
|
||||
}
|
||||
|
||||
/*
|
||||
// BandWidth
|
||||
|
||||
获取域名访问带宽数据
|
||||
http://developer.qiniu.com/article/fusion/api/traffic-bandwidth.html
|
||||
|
||||
StartDate string 必须 开始日期,例如:2016-07-01
|
||||
EndDate string 必须 结束日期,例如:2016-07-03
|
||||
Granularity string 必须 粒度,取值:5min / hour /day
|
||||
Domains []string 必须 域名列表
|
||||
*/
|
||||
func GetBandWidthData(startDate, endDate, granularity string, domainList []string) (bandwidthData TrafficResp, err error) {
|
||||
|
||||
domains := strings.Join(domainList, ";")
|
||||
reqBody := TrafficReqBody{
|
||||
StartDate: startDate,
|
||||
EndDate: endDate,
|
||||
Granularity: granularity,
|
||||
Domains: domains,
|
||||
}
|
||||
|
||||
resData, reqErr := postRequest("v2/tune/bandwidth", reqBody)
|
||||
if reqErr != nil {
|
||||
err = reqErr
|
||||
return
|
||||
}
|
||||
umErr := json.Unmarshal(resData, &bandwidthData)
|
||||
if umErr != nil {
|
||||
err = umErr
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
/* Flux
|
||||
|
||||
获取域名访问流量数据
|
||||
http://developer.qiniu.com/article/fusion/api/traffic-bandwidth.html
|
||||
|
||||
StartDate string 必须 开始日期,例如:2016-07-01
|
||||
EndDate string 必须 结束日期,例如:2016-07-03
|
||||
Granularity string 必须 粒度,取值:5min / hour /day
|
||||
Domains []string 必须 域名列表
|
||||
*/
|
||||
func GetFluxData(startDate, endDate, granularity string, domainList []string) (fluxData TrafficResp, err error) {
|
||||
|
||||
domains := strings.Join(domainList, ";")
|
||||
reqBody := TrafficReqBody{
|
||||
StartDate: startDate,
|
||||
EndDate: endDate,
|
||||
Granularity: granularity,
|
||||
Domains: domains,
|
||||
}
|
||||
|
||||
resData, reqErr := postRequest("v2/tune/flux", reqBody)
|
||||
if reqErr != nil {
|
||||
err = reqErr
|
||||
return
|
||||
}
|
||||
|
||||
umErr := json.Unmarshal(resData, &fluxData)
|
||||
if umErr != nil {
|
||||
err = umErr
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// RefreshReq
|
||||
// 缓存刷新请求内容
|
||||
type RefreshReq struct {
|
||||
Urls []string `json:"urls"`
|
||||
Dirs []string `json:"dirs"`
|
||||
}
|
||||
|
||||
// RefreshResp
|
||||
// 缓存刷新响应内容
|
||||
type RefreshResp struct {
|
||||
Code int `json:"code"`
|
||||
Error string `json:"error"`
|
||||
RequestID string `json:"requestId,omitempty"`
|
||||
InvalidUrls []string `json:"invalidUrls,omitempty"`
|
||||
InvalidDirs []string `json:"invalidDirs,omitempty"`
|
||||
UrlQuotaDay int `json:"urlQuotaDay,omitempty"`
|
||||
UrlSurplusDay int `json:"urlSurplusDay,omitempty"`
|
||||
DirQuotaDay int `json:"dirQuotaDay,omitempty"`
|
||||
DirSurplusDay int `json:"dirSurplusDay,omitempty"`
|
||||
}
|
||||
|
||||
/* RefreshUrlsAndDirs
|
||||
|
||||
刷新链接列表,每次最多不可以超过100条链接
|
||||
http://developer.qiniu.com/article/fusion/api/refresh.html
|
||||
|
||||
urls 要刷新的单个url列表,总数不超过100条;单个url,即一个具体的url,例如:http://bar.foo.com/index.html
|
||||
dirs 要刷新的目录url列表,总数不超过10条;目录dir,即表示一个目录级的url,例如:http://bar.foo.com/dir/,也支持在尾部使用通配符,例如:http://bar.foo.com/dir/*
|
||||
*/
|
||||
func RefreshUrlsAndDirs(urls, dirs []string) (result RefreshResp, err error) {
|
||||
|
||||
reqBody := RefreshReq{
|
||||
Urls: urls,
|
||||
Dirs: dirs,
|
||||
}
|
||||
|
||||
resData, reqErr := postRequest("v2/tune/refresh", reqBody)
|
||||
if reqErr != nil {
|
||||
err = reqErr
|
||||
return
|
||||
}
|
||||
umErr := json.Unmarshal(resData, &result)
|
||||
if umErr != nil {
|
||||
err = reqErr
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// RefreshUrls
|
||||
// 刷新文件
|
||||
func RefreshUrls(urls []string) (result RefreshResp, err error) {
|
||||
return RefreshUrlsAndDirs(urls, nil)
|
||||
}
|
||||
|
||||
// RefreshDirs
|
||||
// 刷新目录
|
||||
func RefreshDirs(dirs []string) (result RefreshResp, err error) {
|
||||
return RefreshUrlsAndDirs(nil, dirs)
|
||||
}
|
||||
|
||||
// PrefetchReq
|
||||
// 文件预取请求内容
|
||||
type PrefetchReq struct {
|
||||
Urls []string `json:"urls"`
|
||||
}
|
||||
|
||||
// PrefetchResp
|
||||
// 文件预取响应内容
|
||||
type PrefetchResp struct {
|
||||
Code int `json:"code"`
|
||||
Error string `json:"error"`
|
||||
RequestID string `json:"requestId,omitempty"`
|
||||
InvalidUrls []string `json:"invalidUrls,omitempty"`
|
||||
QuotaDay int `json:"quotaDay,omitempty"`
|
||||
SurplusDay int `json:"surplusDay,omitempty"`
|
||||
}
|
||||
|
||||
// PrefetchUrls
|
||||
// 预取文件链接,每次最多不可以超过100条
|
||||
// http://developer.qiniu.com/article/fusion/api/prefetch.html
|
||||
func PrefetchUrls(urls []string) (result PrefetchResp, err error) {
|
||||
|
||||
reqBody := PrefetchReq{
|
||||
Urls: urls,
|
||||
}
|
||||
|
||||
resData, reqErr := postRequest("v2/tune/prefetch", reqBody)
|
||||
if reqErr != nil {
|
||||
err = reqErr
|
||||
return
|
||||
}
|
||||
|
||||
umErr := json.Unmarshal(resData, &result)
|
||||
if umErr != nil {
|
||||
err = umErr
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// RequestWithBody
|
||||
// 带body对api发出请求并且返回response body
|
||||
func postRequest(path string, body interface{}) (resData []byte, err error) {
|
||||
|
||||
urlStr := fmt.Sprintf("%s/%s", FUSION_HOST, path)
|
||||
|
||||
reqData, _ := json.Marshal(body)
|
||||
req, reqErr := http.NewRequest("POST", urlStr, bytes.NewReader(reqData))
|
||||
if reqErr != nil {
|
||||
err = reqErr
|
||||
return
|
||||
}
|
||||
|
||||
mac := qbox.NewMac(ACCESS_KEY, SECRET_KEY)
|
||||
accessToken, signErr := mac.SignRequest(req, false)
|
||||
if signErr != nil {
|
||||
err = signErr
|
||||
return
|
||||
}
|
||||
|
||||
req.Header.Add("Authorization", "QBox "+accessToken)
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
|
||||
resp, httpErr := http.DefaultClient.Do(req)
|
||||
if httpErr != nil {
|
||||
err = httpErr
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
resData, ioErr := ioutil.ReadAll(resp.Body)
|
||||
if ioErr != nil {
|
||||
err = ioErr
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
243
vendor/github.com/qiniu/api.v7/cdn/api_test.go
generated
vendored
Normal file
243
vendor/github.com/qiniu/api.v7/cdn/api_test.go
generated
vendored
Normal file
@@ -0,0 +1,243 @@
|
||||
package cdn
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"os"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"time"
|
||||
|
||||
"github.com/qiniu/api.v7/kodo"
|
||||
)
|
||||
|
||||
var (
|
||||
ak = os.Getenv("QINIU_ACCESS_KEY")
|
||||
sk = os.Getenv("QINIU_SECRET_KEY")
|
||||
domain = os.Getenv("QINIU_TEST_DOMAIN")
|
||||
testBucketName = os.Getenv("QINIU_TEST_BUCKET")
|
||||
|
||||
testDate = time.Now().AddDate(0, 0, -3).Format("2006-01-02")
|
||||
bucket = newBucket()
|
||||
client *kodo.Client
|
||||
testKey = "fusionTest"
|
||||
testURL string
|
||||
)
|
||||
|
||||
func init() {
|
||||
kodo.SetMac(ak, sk)
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
testKey += strconv.Itoa(rand.Int())
|
||||
bucket.PutFile(nil, nil, testKey, "doc.go", nil)
|
||||
testURL = domain + "/" + testKey
|
||||
|
||||
}
|
||||
|
||||
func TestGetBandWidthData(t *testing.T) {
|
||||
type args struct {
|
||||
startDate string
|
||||
endDate string
|
||||
granularity string
|
||||
domainList []string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantTraffic TrafficResp
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "BandWidthTest_1",
|
||||
args: args{
|
||||
testDate,
|
||||
testDate,
|
||||
"5min",
|
||||
[]string{domain},
|
||||
},
|
||||
},
|
||||
}
|
||||
kodo.SetMac(ak, sk)
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, err := GetBandWidthData(tt.args.startDate, tt.args.endDate, tt.args.granularity, tt.args.domainList)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("GetBandWidthData() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetFluxData(t *testing.T) {
|
||||
type args struct {
|
||||
startDate string
|
||||
endDate string
|
||||
granularity string
|
||||
domainList []string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantTraffic TrafficResp
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "BandWidthTest_1",
|
||||
args: args{
|
||||
testDate,
|
||||
testDate,
|
||||
"5min",
|
||||
[]string{domain},
|
||||
},
|
||||
},
|
||||
}
|
||||
kodo.SetMac(ak, sk)
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, err := GetFluxData(tt.args.startDate, tt.args.endDate, tt.args.granularity, tt.args.domainList)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("GetFluxData() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRefreshUrlsAndDirs(t *testing.T) {
|
||||
kodo.SetMac(ak, sk)
|
||||
|
||||
type args struct {
|
||||
urls []string
|
||||
dirs []string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantResult RefreshResp
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "refresh_test_1",
|
||||
args: args{
|
||||
urls: []string{testURL},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, err := RefreshUrlsAndDirs(tt.args.urls, tt.args.dirs)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("RefreshUrlsAndDirs() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRefreshUrls(t *testing.T) {
|
||||
type args struct {
|
||||
urls []string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantResult RefreshResp
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "refresh_test_1",
|
||||
args: args{
|
||||
urls: []string{testURL},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, err := RefreshUrls(tt.args.urls)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("RefreshUrls() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRefreshDirs(t *testing.T) {
|
||||
type args struct {
|
||||
dirs []string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantResult RefreshResp
|
||||
wantErr bool
|
||||
}{}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
gotResult, err := RefreshDirs(tt.args.dirs)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("RefreshDirs() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(gotResult, tt.wantResult) {
|
||||
t.Errorf("RefreshDirs() = %v, want %v", gotResult, tt.wantResult)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrefetchUrls(t *testing.T) {
|
||||
type args struct {
|
||||
urls []string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantResult PrefetchResp
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "refresh_test_1",
|
||||
args: args{
|
||||
urls: []string{testURL},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
kodo.SetMac(ak, sk)
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, err := PrefetchUrls(tt.args.urls)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("PrefetchUrls() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func newBucket() (bucket kodo.Bucket) {
|
||||
|
||||
ak := os.Getenv("QINIU_ACCESS_KEY")
|
||||
sk := os.Getenv("QINIU_SECRET_KEY")
|
||||
if ak == "" || sk == "" {
|
||||
panic("require ACCESS_KEY & SECRET_KEY")
|
||||
}
|
||||
kodo.SetMac(ak, sk)
|
||||
|
||||
testBucketName = os.Getenv("QINIU_TEST_BUCKET")
|
||||
domain = os.Getenv("QINIU_TEST_DOMAIN")
|
||||
if testBucketName == "" || domain == "" {
|
||||
panic("require test env")
|
||||
}
|
||||
client = kodo.NewWithoutZone(nil)
|
||||
|
||||
return client.Bucket(testBucketName)
|
||||
}
|
||||
25
vendor/github.com/qiniu/api.v7/cdn/doc.go
generated
vendored
Normal file
25
vendor/github.com/qiniu/api.v7/cdn/doc.go
generated
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
包 github.com/qiniu/api.v7/cdn 提供了七牛CDN的API功能
|
||||
首先,我们要配置下 AccessKey/SecretKey,
|
||||
import "github.com/qiniu/api.v7/kodo"
|
||||
|
||||
kodo.SetMac("ak", "sk")
|
||||
设置了AccessKey/SecretKey 就可以使用cdn的各类功能
|
||||
|
||||
比如我们要生成一个带时间戳防盗链的链接:
|
||||
q :=url.Values{}// url.Values 请求参数
|
||||
link, err := cdn.CreateTimestampAntileechURL(""http://www.qiniu.com/abc/bcc/aa-s.mp4?x=2&y=3", "encryptedkey", 20)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println(link)
|
||||
|
||||
又或者我们要列出CDN日志及其下载地址:
|
||||
resp, err := cdn.GetCdnLogList("2016-12-26", "x-mas.com")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println(resp)
|
||||
|
||||
*/
|
||||
package cdn
|
||||
92
vendor/github.com/qiniu/api.v7/cdn/logs.go
generated
vendored
Normal file
92
vendor/github.com/qiniu/api.v7/cdn/logs.go
generated
vendored
Normal file
@@ -0,0 +1,92 @@
|
||||
package cdn
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/qiniu/api.v7/auth/qbox"
|
||||
)
|
||||
|
||||
const (
|
||||
LIST_LOG_API = "http://fusion.qiniuapi.com/v2/tune/log/list"
|
||||
)
|
||||
|
||||
// ListLogRequest 日志下载请求内容
|
||||
type ListLogRequest struct {
|
||||
Day string `json:"day"`
|
||||
Domains string `json:"domains"`
|
||||
}
|
||||
|
||||
// ListLogResult 日志下载相应内容
|
||||
type ListLogResult struct {
|
||||
Code int `json:"code"`
|
||||
Error string `json:"error"`
|
||||
Data map[string][]LogDomainInfo `json:"data"`
|
||||
}
|
||||
|
||||
// LogDomainInfo 日志下载信息
|
||||
type LogDomainInfo struct {
|
||||
Name string `json:"name"`
|
||||
Size int64 `json:"size"`
|
||||
ModifiedTime int64 `json:"mtime"`
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
// GetCdnLogList 获取CDN域名访问日志的下载链接
|
||||
// http://developer.qiniu.com/article/fusion/api/log.html
|
||||
func GetCdnLogList(date, domains string) (domainLogs []LogDomainInfo, err error) {
|
||||
|
||||
//new log query request
|
||||
logReq := ListLogRequest{
|
||||
Day: date,
|
||||
Domains: domains,
|
||||
}
|
||||
|
||||
logReqBytes, _ := json.Marshal(&logReq)
|
||||
req, reqErr := http.NewRequest("POST", LIST_LOG_API, bytes.NewReader(logReqBytes))
|
||||
if reqErr != nil {
|
||||
err = fmt.Errorf("New request error, %s", reqErr)
|
||||
return
|
||||
}
|
||||
|
||||
mac := qbox.NewMac("", "")
|
||||
token, signErr := mac.SignRequest(req, false)
|
||||
if signErr != nil {
|
||||
err = signErr
|
||||
return
|
||||
}
|
||||
|
||||
req.Header.Add("Authorization", "QBox "+token)
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
|
||||
resp, respErr := http.DefaultClient.Do(req)
|
||||
if respErr != nil {
|
||||
err = fmt.Errorf("Get response error, %s", respErr)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
listLogResult := ListLogResult{}
|
||||
decoder := json.NewDecoder(resp.Body)
|
||||
if decodeErr := decoder.Decode(&listLogResult); decodeErr != nil {
|
||||
err = fmt.Errorf("Parse response error, %s", decodeErr)
|
||||
return
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
err = fmt.Errorf("Get log list error, %d %s", listLogResult.Code, listLogResult.Error)
|
||||
return
|
||||
}
|
||||
|
||||
domainItems := strings.Split(domains, ";")
|
||||
|
||||
for _, domain := range domainItems {
|
||||
for _, v := range listLogResult.Data[domain] {
|
||||
domainLogs = append(domainLogs, v)
|
||||
}
|
||||
}
|
||||
return
|
||||
|
||||
}
|
||||
48
vendor/github.com/qiniu/api.v7/cdn/logs_test.go
generated
vendored
Normal file
48
vendor/github.com/qiniu/api.v7/cdn/logs_test.go
generated
vendored
Normal file
@@ -0,0 +1,48 @@
|
||||
package cdn
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/qiniu/api.v7/kodo"
|
||||
)
|
||||
|
||||
func init() {
|
||||
kodo.SetMac(ak, sk)
|
||||
}
|
||||
|
||||
func TestGetCdnLogList(t *testing.T) {
|
||||
type args struct {
|
||||
date string
|
||||
domains string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantDomainLogs []LogDomainInfo
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "getCdnLogListTest",
|
||||
args: args{
|
||||
date: testDate,
|
||||
domains: domain,
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
gotDomainLogs, err := GetCdnLogList(tt.args.date, tt.args.domains)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("GetCdnLogList() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
fmt.Println(domain, gotDomainLogs)
|
||||
if !reflect.DeepEqual(gotDomainLogs, tt.wantDomainLogs) {
|
||||
t.Errorf("GetCdnLogList() = %v, want %v", gotDomainLogs, tt.wantDomainLogs)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user