Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v7.9.8 获取cdn域名列表url-query参数 limit不生效 #38

Open
mojocn opened this issue Aug 5, 2021 · 1 comment
Open

v7.9.8 获取cdn域名列表url-query参数 limit不生效 #38

mojocn opened this issue Aug 5, 2021 · 1 comment

Comments

@mojocn
Copy link

mojocn commented Aug 5, 2021

go

import (
	"bytes"
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"github.com/qiniu/go-sdk/v7/auth"
	"io"
	"log"
	"net/http"
	"regexp"
	"strings"
	"time"

	"github.com/qiniu/go-sdk/auth/v7"
)

type SevenCow struct {
	cred       *auth.Credentials
	Ak         string
	Sk         string
	host       string
	fusionHost string // fusion相关api
	timeout    time.Duration
}

func NewSevenCow(ak string, sk string) *SevenCow {
	return &SevenCow{
		Ak:         ak,
		Sk:         sk,
		cred:       auth.New(ak, sk),
		host:       "api.qiniu.com",
		fusionHost: "https://fusion.qiniuapi.com", // 为了单元测试,host配置带http(s)
		timeout:    time.Second * 30,
	}
}

func (c SevenCow) send(method, url string, bodyArg, result interface{}) error {
	ct, cancelFunc := context.WithTimeout(context.Background(), c.timeout)
	defer cancelFunc()

	buf := new(bytes.Buffer)
	if bodyArg != nil {
		err := json.NewEncoder(buf).Encode(bodyArg)
		if err != nil {
			return err
		}
	}

	req, err := http.NewRequestWithContext(ct, method, url, buf)
	if err != nil {
		return err
	}

	req.Header.Set("Content-Type", "application/json")
	token, err := c.cred.SignRequest(req)
	if err != nil {
		return err
	}
	req.Header.Set("Authorization", "QBox "+token)

	response, err := http.DefaultClient.Do(req)
	if err != nil {
		return err
	}

	defer response.Body.Close()
	all, err := io.ReadAll(response.Body)
	if err != nil {
		return err
	}
	if response.StatusCode != 200 {
		marshal, _ := json.Marshal(bodyArg)
		return fmt.Errorf("%s   %s --------- %d  ---   %s", method+url, string(marshal), response.StatusCode, string(all))
	}
	if result != nil {
		err = json.Unmarshal(all, result)
		if err != nil {
			return errors.New(string(all) + err.Error())
		}
	}
	return nil
}
//获取域名列表
func (c SevenCow) CdnDomainList() (*RetDomainList, error) {
	//  https://developer.qiniu.com/fusion/4246/the-domain-name#9
	url := fmt.Sprintf("https://%s/domain?limit=%d", c.host, 1000)
	result := new(RetDomainList)
	return result, c.send(http.MethodGet, url, nil, result)
}

testcase

import (
	"testing"
)

func TestSevenCow_CdnDomainList(t *testing.T) {
	list, err := NewSevenCow("IAM-xxxxxx", "xxx-xxxx-xx").CdnDomainList()
	if err != nil {
		t.Error(err)
	}
	t.Log(list)
}

@gnufree
Copy link

gnufree commented Mar 15, 2022

把req.Header.Set("Content-Type", "application/json") 这行去掉就可以了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants