107 lines
3.5 KiB
Go
107 lines
3.5 KiB
Go
package recomapi
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"videoweb/pub/common/log"
|
|
"videoweb/pub/common/srvdef"
|
|
"videoweb/pub/common/utils"
|
|
)
|
|
|
|
func InsertOneItem(url string, key string, itemInfo *RSItemInfo) *srvdef.SrvErr {
|
|
reqUrl := url + "/api/item"
|
|
resp := &APIResult{}
|
|
reqBody, _ := json.Marshal(itemInfo)
|
|
header := map[string]string{"X-API-Key": key}
|
|
code, err := utils.POSTJsonWithJResp(resp, reqUrl, header, reqBody)
|
|
if err != nil {
|
|
log.Error("recomapi:InsertOneItem POSTJsonWithJResp failed",
|
|
log.Any("reqUrl", reqUrl), log.E(err))
|
|
return srvdef.New(srvdef.Err, "", "")
|
|
}
|
|
if code != http.StatusOK {
|
|
log.Error("recomapi:InsertOneItem POSTJsonWithJResp code error.",
|
|
log.Any("reqUrl", reqUrl), log.Any("code", code))
|
|
return srvdef.New(srvdef.Err, "", "")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func InsertManyItems(url string, key string, itemInfos RSItemInfoList) *srvdef.SrvErr {
|
|
reqUrl := url + "/api/items"
|
|
reqBody, _ := json.Marshal(itemInfos)
|
|
header := map[string]string{"X-API-Key": key}
|
|
code, err := utils.POSTJsonWithJResp(nil, reqUrl, header, reqBody)
|
|
if err != nil {
|
|
log.Error("recomapi:InsertManyItems POSTJsonWithJResp failed",
|
|
log.Any("reqUrl", reqUrl), log.E(err))
|
|
return srvdef.New(srvdef.Err, "", "")
|
|
}
|
|
if code != http.StatusOK {
|
|
log.Error("recomapi:InsertManyItems POSTJsonWithJResp code error.",
|
|
log.Any("reqUrl", reqUrl), log.Any("code", code))
|
|
return srvdef.New(srvdef.Err, "", "")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func GetOneItem(url string, key string, itemId string) (*RSItemInfo, *srvdef.SrvErr) {
|
|
reqUrl := fmt.Sprintf("%s/api/item/%s", url, itemId)
|
|
resp := &RSItemInfo{}
|
|
header := map[string]string{"X-API-Key": key}
|
|
code, err := utils.GetWithJResp(resp, reqUrl, header, nil)
|
|
if err != nil {
|
|
log.Error("recomapi:GetOneItem GetWithJResp failed",
|
|
log.Any("reqUrl", reqUrl), log.E(err))
|
|
return nil, srvdef.New(srvdef.Err, "", "")
|
|
}
|
|
if code != http.StatusOK {
|
|
log.Error("recomapi:GetOneItem GetWithJResp code error.",
|
|
log.Any("reqUrl", reqUrl), log.Any("code", code))
|
|
return nil, srvdef.New(srvdef.Err, "", "")
|
|
}
|
|
log.Info("Test", log.Any("resp", resp))
|
|
return resp, nil
|
|
}
|
|
|
|
func GetManyItems(url string, key string, cursor string, size int64) (RSItemInfoList, string, *srvdef.SrvErr) {
|
|
reqUrl := fmt.Sprintf("%s/api/items?n=%d&cursor=%s", url, size, cursor)
|
|
type Resp struct {
|
|
Cursor string `json:"Cursor"`
|
|
Items RSItemInfoList `json:"Items"`
|
|
}
|
|
var resp Resp
|
|
header := map[string]string{"X-API-Key": key}
|
|
code, err := utils.GetWithJResp(&resp, reqUrl, header, nil)
|
|
if err != nil {
|
|
log.Error("recomapi:GetOneItem GetWithJResp failed",
|
|
log.Any("reqUrl", reqUrl), log.E(err))
|
|
return nil, "", srvdef.New(srvdef.Err, "", "")
|
|
}
|
|
if code != http.StatusOK {
|
|
log.Error("recomapi:GetOneItem GetWithJResp code error.",
|
|
log.Any("reqUrl", reqUrl), log.Any("code", code))
|
|
return nil, "", srvdef.New(srvdef.Err, "", "")
|
|
}
|
|
return resp.Items, resp.Cursor, nil
|
|
}
|
|
|
|
func DeleteOneItem(url string, key string, itemId string) (int64, *srvdef.SrvErr) {
|
|
reqUrl := fmt.Sprintf("%s/api/item/%s", url, itemId)
|
|
resp := &APIResult{}
|
|
header := map[string]string{"X-API-Key": key}
|
|
code, err := utils.DeleteWithJResp(resp, reqUrl, header, nil)
|
|
if err != nil {
|
|
log.Error("recomapi:DeleteOneItem DeleteWithJResp failed",
|
|
log.Any("reqUrl", reqUrl), log.E(err))
|
|
return 0, srvdef.New(srvdef.Err, "", "")
|
|
}
|
|
if code != http.StatusOK {
|
|
log.Error("recomapi:DeleteOneItem DeleteWithJResp code error.",
|
|
log.Any("reqUrl", reqUrl), log.Any("code", code))
|
|
return 0, srvdef.New(srvdef.Err, "", "")
|
|
}
|
|
return resp.RowAffected, nil
|
|
}
|