From 52808f0b3c2ba97fe17a4de96f5720f00ac487f5 Mon Sep 17 00:00:00 2001 From: zxbit2011 Date: Fri, 27 Nov 2020 15:12:13 +0800 Subject: [PATCH] add result --- README.md | 6 ++---- examples/sdk_test.go | 15 +++++++++------ sdk.go | 26 +++++++++++++++++++++++--- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index df9f0f9..a191ea4 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/examples/sdk_test.go b/examples/sdk_test.go index c69afc4..f6b1f9a 100644 --- a/examples/sdk_test.go +++ b/examples/sdk_test.go @@ -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))*/ } diff --git a/sdk.go b/sdk.go index c57c98b..7c0404f 100644 --- a/sdk.go +++ b/sdk.go @@ -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:%d,Location:%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