txim/api/api.go

53 lines
1.1 KiB
Go
Raw Permalink Normal View History

2020-03-24 20:19:47 +08:00
package api
import (
2020-03-24 20:38:26 +08:00
"encoding/json"
2020-03-24 20:19:47 +08:00
"fmt"
2022-01-11 10:14:11 +08:00
. "gitee.ltd/lxh/txim"
"gitee.ltd/lxh/txim/common"
2020-03-24 20:19:47 +08:00
"math/rand"
"github.com/imroc/req"
)
2022-01-11 10:14:11 +08:00
func Api(p common.Api, in, out interface{}) error {
host := fmt.Sprintf("https://%s/%s/%s/%s", ApiHost, ApiVersion, p.ServiceName, p.Command)
2020-04-07 16:36:02 +08:00
2022-01-11 10:14:11 +08:00
appId, identifier := GetInfo()
2020-03-24 20:19:47 +08:00
query := req.QueryParam{
2022-01-11 10:14:11 +08:00
"sdkappid": appId,
"identifier": identifier,
"usersig": UserSig(identifier, 5*60),
2020-03-24 20:19:47 +08:00
"random": rand.Uint32(),
"contenttype": "json",
}
resp, err := req.Post(host, query, req.BodyJSON(in))
if err != nil {
return err
}
2020-03-24 20:38:26 +08:00
respBytes := resp.Bytes()
errAns := struct {
ActionStatus string `json:"ActionStatus"`
ErrorInfo string `json:"ErrorInfo"`
ErrorCode int `json:"ErrorCode"`
}{}
if err := json.Unmarshal(respBytes, &errAns); err != nil {
return err
}
if errAns.ErrorCode != 0 {
return fmt.Errorf("ActionStatus:%s,ErrorInfo:%s,ErrorCode:%d", errAns.ActionStatus, errAns.ErrorInfo, errAns.ErrorCode)
}
if err := json.Unmarshal(respBytes, out); err != nil {
2020-03-24 20:19:47 +08:00
return err
}
return nil
}