txim/api/api.go

53 lines
1.1 KiB
Go

package api
import (
"encoding/json"
"fmt"
. "gitee.ltd/lxh/txim"
"gitee.ltd/lxh/txim/common"
"math/rand"
"github.com/imroc/req"
)
func Api(p common.Api, in, out interface{}) error {
host := fmt.Sprintf("https://%s/%s/%s/%s", ApiHost, ApiVersion, p.ServiceName, p.Command)
appId, identifier := GetInfo()
query := req.QueryParam{
"sdkappid": appId,
"identifier": identifier,
"usersig": UserSig(identifier, 5*60),
"random": rand.Uint32(),
"contenttype": "json",
}
resp, err := req.Post(host, query, req.BodyJSON(in))
if err != nil {
return err
}
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 {
return err
}
return nil
}