mirror of
https://github.com/zxbit2011/hikvisionOpenAPIGo.git
synced 2025-12-24 13:37:59 +08:00
fix: support go module, fix dependecy and some error return
This commit is contained in:
7
go.mod
Normal file
7
go.mod
Normal file
@@ -0,0 +1,7 @@
|
||||
module github.com/zxbit2011/hikvisionOpenAPIGo
|
||||
|
||||
go 1.18
|
||||
|
||||
require github.com/satori/go.uuid v1.2.0
|
||||
|
||||
require gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
|
||||
9
go.sum
Normal file
9
go.sum
Normal file
@@ -0,0 +1,9 @@
|
||||
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
|
||||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
|
||||
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
68
sdk.go
68
sdk.go
@@ -14,6 +14,7 @@ import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@@ -48,17 +49,21 @@ type Data struct {
|
||||
// @return 请求结果 参数类型
|
||||
func (hk HKConfig) HttpPost(url string, body map[string]string, timeout int) (result Result, err error) {
|
||||
var header = make(map[string]string)
|
||||
bodyJson := MustJsonString(body)
|
||||
hk.initRequest(header, url, bodyJson, true)
|
||||
bodyJson, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
err = hk.initRequest(header, url, string(bodyJson), true)
|
||||
if err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
var sb []string
|
||||
if hk.IsHttps {
|
||||
sb = append(sb, "https://")
|
||||
} else {
|
||||
sb = append(sb, "http://")
|
||||
}
|
||||
sb = append(sb, hk.Ip)
|
||||
sb = append(sb, ":")
|
||||
sb = append(sb, fmt.Sprintf("%d", hk.Port))
|
||||
sb = append(sb, fmt.Sprintf("%s:%d", hk.Ip, hk.Port))
|
||||
sb = append(sb, url)
|
||||
|
||||
client := &http.Client{}
|
||||
@@ -72,7 +77,7 @@ func (hk HKConfig) HttpPost(url string, body map[string]string, timeout int) (re
|
||||
}
|
||||
client.Transport = tr
|
||||
}
|
||||
req, err := http.NewRequest("POST", strings.Join(sb, ""), bytes.NewReader([]byte(bodyJson)))
|
||||
req, err := http.NewRequest("POST", strings.Join(sb, ""), bytes.NewReader(bodyJson))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -106,14 +111,18 @@ func (hk HKConfig) HttpPost(url string, body map[string]string, timeout int) (re
|
||||
}
|
||||
|
||||
// initRequest 初始化请求头
|
||||
func (hk HKConfig) initRequest(header map[string]string, url, body string, isPost bool) {
|
||||
func (hk HKConfig) initRequest(header map[string]string, url, body string, isPost bool) error {
|
||||
header["Accept"] = "application/json"
|
||||
header["Content-Type"] = "application/json"
|
||||
if isPost {
|
||||
header["content-md5"] = computeContentMd5(body)
|
||||
var err error
|
||||
header["content-md5"], err = computeContentMd5(body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
header["x-ca-timestamp"] = MustString(time.Now().UnixNano() / 1e6)
|
||||
uid, _ := uuid.NewV4()
|
||||
header["x-ca-timestamp"] = strconv.FormatInt(time.Now().UnixMilli(), 10)
|
||||
uid := uuid.NewV4()
|
||||
header["x-ca-nonce"] = uid.String()
|
||||
header["x-ca-key"] = hk.AppKey
|
||||
|
||||
@@ -125,15 +134,21 @@ func (hk HKConfig) initRequest(header map[string]string, url, body string, isPos
|
||||
}
|
||||
signedStr, err := computeForHMACSHA256(strToSign, hk.Secret)
|
||||
if err != nil {
|
||||
println(err.Error())
|
||||
return
|
||||
return err
|
||||
}
|
||||
header["x-ca-signature"] = signedStr
|
||||
return nil
|
||||
}
|
||||
|
||||
// computeContentMd5 计算content-md5
|
||||
func computeContentMd5(body string) string {
|
||||
return base64.StdEncoding.EncodeToString([]byte(Md5(body)))
|
||||
func computeContentMd5(body string) (string, error) {
|
||||
h := md5.New()
|
||||
_, err := h.Write([]byte(body))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
md5Str := hex.EncodeToString(h.Sum(nil))
|
||||
return base64.StdEncoding.EncodeToString([]byte(md5Str)), nil
|
||||
}
|
||||
|
||||
// computeForHMACSHA256 计算HMACSHA265
|
||||
@@ -206,28 +221,3 @@ func buildSignHeader(header map[string]string) string {
|
||||
header["x-ca-signature-headers"] = strings.Join(sbSignHeader, "")
|
||||
return strings.Join(sb, "")
|
||||
}
|
||||
|
||||
func MustJson(i interface{}) []byte {
|
||||
if d, err := json.Marshal(i); err == nil {
|
||||
return d
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func MustJsonString(i interface{}) string {
|
||||
return string(MustJson(i))
|
||||
}
|
||||
|
||||
func MustString(value interface{}) string {
|
||||
if value == nil {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprintf("%v", value)
|
||||
}
|
||||
|
||||
func Md5(str string) string {
|
||||
h := md5.New()
|
||||
h.Write([]byte(str))
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user