hikvision/cloud/base-cloud.go

48 lines
963 B
Go

package cloud
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"myschools.me/suguo/hikvision/model"
)
// 基础云眸http请求
func BaseCloudHttpRequest(accesstoken *string, method string, url string, body interface{}) ([]byte, error) {
b, err := json.Marshal(body)
if err != nil {
return nil, err
}
reqbody := strings.NewReader(string(b))
req, err := http.NewRequest(method, url, reqbody)
if err != nil {
return nil, err
}
req.Header.Add("authorization", fmt.Sprintf(" bearer %s", *accesstoken))
if strings.ToLower(method) == "post" {
req.Header.Add("Content-Type", "application/json")
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var result *model.BaseOperationRes
if err := json.Unmarshal(respBody, &result); err != nil {
return nil, err
}
return respBody, nil
}