We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
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) }
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) }
The text was updated successfully, but these errors were encountered:
把req.Header.Set("Content-Type", "application/json") 这行去掉就可以了。
Sorry, something went wrong.
No branches or pull requests
go
testcase
The text was updated successfully, but these errors were encountered: