You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

60 lines
1.4 KiB

package cloud
import (
"fmt"
hbtp "github.com/mgr9525/HyperByte-Transfer-Protocol"
gocloud "github.com/mgr9525/go-cloud"
"time"
)
func NewReq(code int, method string, ipr ...string) (*hbtp.Request, error) {
host := fmt.Sprintf("%v", gocloud.CloudConf.Custom["sysHbtpHost"])
req, err := hbtp.NewRPCReq(host, code, method)
if err != nil {
return nil, err
}
hd := req.ReqHeader()
if len(ipr) > 0 {
hd.RelIp = ipr[1]
}
hd.Times = time.Now().Format(time.RFC3339Nano)
//hd.Token = ruisUtil.Md5String(hd.Path + hd.RelIp + hd.Times + HbtpMD5Token)
return req, err
}
func DoJson(code int, method string, in, out interface{}, hd ...map[string]interface{}) error {
req, err := NewReq(code, method)
if err != nil {
return err
}
defer req.Close()
if len(hd) > 0 && hd[0] != nil {
for k, v := range hd[0] {
req.ReqHeader().Set(k, v)
}
}
err = req.Do(in)
if err != nil {
return err
}
if req.ResCode() != hbtp.ResStatusOk {
return fmt.Errorf("res err(%d):%s", req.ResCode(), string(req.ResBodyBytes()))
}
return req.ResBodyJson(out)
}
func DoString(code int, method string, in interface{}, hd ...hbtp.Mp) (int, []byte, error) {
req, err := NewReq(code, method)
if err != nil {
return 0, nil, err
}
defer req.Close()
if len(hd) > 0 && hd[0] != nil {
for k, v := range hd[0] {
req.ReqHeader().Set(k, v)
}
}
err = req.Do(in)
if err != nil {
return 0, nil, err
}
return req.ResCode(), req.ResBodyBytes(), nil
}