add result

This commit is contained in:
zxbit2011
2020-11-27 15:12:13 +08:00
parent 8659031d44
commit 52808f0b3c
3 changed files with 34 additions and 13 deletions

View File

@@ -2,11 +2,9 @@
> 海康威视OpenAPI安全认证库 - Golang版本实现
# 官网
接口调用认证:
https://open.hikvision.com/docs/7d0beeded66543999bff7bc2f91414d4
接口调用认证:[文档说明](https://open.hikvision.com/docs/7d0beeded66543999bff7bc2f91414d4)
其他语言版本:
https://open.hikvision.com/download/5c67f1e2f05948198c909700?type=10
其他语言版本:[下载链接](https://open.hikvision.com/download/5c67f1e2f05948198c909700?type=10)
# 快速使用
````
> go get github.com/zxbit2011/hikvisionOpenAPIGo

View File

@@ -1,16 +1,17 @@
package examples
import (
"encoding/json"
"github.com/zxbit2011/hikvisionOpenAPIGo"
"testing"
)
func TestSDK(t *testing.T) {
hk := hikvisionOpenAPIGo.HKConfig{
Ip: "172.17.207.240",
Ip: "127.0.0.1",
Port: 443,
AppKey: "28057383",
Secret: "dZztQSSUAF4kLpURGQMa",
AppKey: "28057000",
Secret: "dZztQSS0000kLpURG000",
IsHttps: true,
}
@@ -23,16 +24,18 @@ func TestSDK(t *testing.T) {
t.Fatal(err)
return
}
t.Log("OK", string(result))
resJson, err := json.Marshal(result)
t.Log("OK", string(resJson))
/*body := map[string]string{
"cameraIndexCode": "71c1e8bd1b0d406a94e7cdf88a251f9b",
"protocol": "rtmp",
}
result, err := hk.Post("/artemis/api/video/v2/cameras/previewURLs", body, 15)
result, err := hk.HttpPost("/artemis/api/video/v2/cameras/previewURLs", body, 15)
if err != nil {
t.Fatal(err)
return
}
t.Log("OK", string(result))*/
resJson, err := json.Marshal(result)
t.Log("OK", string(resJson))*/
}

26
sdk.go
View File

@@ -27,11 +27,26 @@ type HKConfig struct {
IsHttps bool //是否使用HTTPS协议
}
// 返回结果
type Result struct {
Code string `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
}
// 返回值data
type Data struct {
Total int `json:"total"`
PageSize int `json:"pageSize"`
PageNo int `json:"pageNo"`
List []map[string]interface{} `json:"list"`
}
// @title HTTP Post请求
// @url HTTP接口Url string HTTP接口Url不带协议和端口如/artemis/api/resource/v1/org/advance/orgList
// @body 请求参数 map[string]string
// @return 请求结果 参数类型
func (hk HKConfig) HttpPost(url string, body map[string]string, timeout int) (result []byte, err error) {
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)
@@ -75,7 +90,12 @@ func (hk HKConfig) HttpPost(url string, body map[string]string, timeout int) (re
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
result, err = ioutil.ReadAll(resp.Body)
var resBody []byte
resBody, err = ioutil.ReadAll(resp.Body)
if err != nil {
return
}
err = json.Unmarshal(resBody, &result)
} else if resp.StatusCode == http.StatusFound || resp.StatusCode == http.StatusMovedPermanently {
reqUrl := resp.Header.Get("Location")
panic(fmt.Errorf("HttpPost Response StatusCode%dLocation%s", resp.StatusCode, reqUrl))
@@ -113,7 +133,7 @@ func (hk HKConfig) initRequest(header map[string]string, url, body string, isPos
// computeContentMd5 计算content-md5
func computeContentMd5(body string) string {
return base64.StdEncoding.EncodeToString([]byte( Md5(body)))
return base64.StdEncoding.EncodeToString([]byte(Md5(body)))
}
// computeForHMACSHA256 计算HMACSHA265