Skip to content

Go

go
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"

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

const ak = "你的ak"
const sk = "你的sk"

type NodeStatReq struct {
	NodeIDs []string `json:"nodeIDs"`
	Start   string   `json:"start"`
	End     string   `json:"end"`
}

// go.mod require github.com/qiniu/go-sdk/v7 v7.11.1
func main() {
	get()

	post()
}

func get() {
	params := url.Values{}
	params.Add("province", "江苏")
	params.Add("isp", "移动")
	params.Add("usbw", fmt.Sprint(1000))
	params.Add("bwNum", fmt.Sprint(5))
	params.Add("natType", "public")
	params.Add("dialType", "staticNetSingle")

	reqUrl := "https://api.niulinkcloud.com/v1/vendorclaimablebw"
	reqUrl = fmt.Sprintf("%s?%s", reqUrl, params.Encode())
	req, err := http.NewRequest("GET", reqUrl, nil)
	if err != nil {
		return
	}
	// 设置Content-Type必须在调用SignRequestV2方法之前,因为Content-Type也需要参与签名
	req.Header.Set("Content-Type", "application/json")
	token, err := auth.New(ak, sk).SignRequestV2(req)
	if err != nil {
		return
	}
	req.Header.Set("Authorization", "Qiniu "+token)

	httpCli := &http.Client{}
	httpResp, err := httpCli.Do(req)
	if err != nil {
		return
	}
	respBody, err := ioutil.ReadAll(httpResp.Body)
	defer httpResp.Body.Close()
	if err != nil {
		return
	}
	if httpResp.StatusCode == 200 {
		println(fmt.Sprintf("resp body:%s", string(respBody)))
	} else {
		println(fmt.Sprintf("http resp code: %d, resp body:%s", httpResp.StatusCode, string(respBody)))
	}
}

func post() {
	reqData := NodeStatReq{
		NodeIDs: []string{"你的节点ID"},
		Start:   "2023-07-30",
		End:     "2023-07-31",
	}
	reqDataBytes, err := json.Marshal(reqData)
	if err != nil {
		return
	}
	reqUrl := "https://api.niulinkcloud.com/v1/nodes/stats"
	req, err := http.NewRequest("POST", reqUrl, bytes.NewReader(reqDataBytes))
	if err != nil {
		return
	}
	// 设置Content-Type必须在调用SignRequestV2方法之前,因为Content-Type也需要参与签名
	req.Header.Set("Content-Type", "application/json")
	token, err := auth.New(ak, sk).SignRequestV2(req)
	if err != nil {
		return
	}
	req.Header.Set("Authorization", "Qiniu "+token)

	httpCli := &http.Client{}
	httpResp, err := httpCli.Do(req)
	if err != nil {
		return
	}
	respBody, err := ioutil.ReadAll(httpResp.Body)
	defer httpResp.Body.Close()
	if err != nil {
		return
	}
	if httpResp.StatusCode == 200 {
		println(fmt.Sprintf("resp body:%s", string(respBody)))
	} else {
		println(fmt.Sprintf("http resp code: %d, resp body:%s", httpResp.StatusCode, string(respBody)))
	}
}