本文整理汇总了Golang中github.com/gorilla/rpc/v2/json.DecodeClientResponse函数的典型用法代码示例。如果您正苦于以下问题:Golang DecodeClientResponse函数的具体用法?Golang DecodeClientResponse怎么用?Golang DecodeClientResponse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了DecodeClientResponse函数的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: GetAuthKeys
// GetAuthKeys get access key id and secret access key
func GetAuthKeys(url string) ([]byte, error) {
op := RPCOps{
Method: "Auth.Get",
Request: rpc.Args{Request: ""},
}
req, err := NewRequest(url, op, http.DefaultTransport)
if err != nil {
return nil, iodine.New(err, nil)
}
resp, err := req.Do()
defer closeResp(resp)
if err != nil {
return nil, iodine.New(err, nil)
}
var reply rpc.AuthReply
if err := jsonrpc.DecodeClientResponse(resp.Body, &reply); err != nil {
return nil, iodine.New(err, nil)
}
authConfig := &auth.Config{}
authConfig.Version = "0.0.1"
authConfig.Users = make(map[string]*auth.User)
user := &auth.User{}
user.Name = "testuser"
user.AccessKeyID = reply.AccessKeyID
user.SecretAccessKey = reply.SecretAccessKey
authConfig.Users[reply.AccessKeyID] = user
if err := auth.SaveConfig(authConfig); err != nil {
return nil, iodine.New(err, nil)
}
return json.MarshalIndent(reply, "", "\t")
}
示例2: SetDonut
// SetDonut - set donut config
func SetDonut(url, hostname string, disks []string) error {
op := RPCOps{
Method: "Donut.Set",
Request: rpc.DonutArgs{
Hostname: hostname,
Disks: disks,
Name: "default",
MaxSize: 512000000,
},
}
req, err := NewRequest(url, op, http.DefaultTransport)
if err != nil {
return iodine.New(err, nil)
}
resp, err := req.Do()
defer closeResp(resp)
if err != nil {
return iodine.New(err, nil)
}
var reply rpc.Reply
if err := jsonrpc.DecodeClientResponse(resp.Body, &reply); err != nil {
return iodine.New(err, nil)
}
return reply.Error
}
示例3: proxyRequest
func proxyRequest(method, host string, ssl bool, res interface{}) *probe.Error {
u := &url.URL{}
if ssl {
u.Scheme = "https"
} else {
u.Scheme = "http"
}
u.Host = host
if _, _, err := net.SplitHostPort(host); err == nil {
u.Host = host
} else {
u.Host = host + ":9002"
}
u.Path = "/rpc"
op := rpcOperation{
Method: method,
Request: ServerArg{},
}
request, err := newRPCRequest(u.String(), op, nil)
if err != nil {
return err.Trace()
}
var resp *http.Response
resp, err = request.Do()
if err != nil {
return err.Trace()
}
if err := json.DecodeClientResponse(resp.Body, res); err != nil {
return probe.NewError(err)
}
return nil
}
示例4: Do
// Do holds a JSON-RPC request.
func Do(url, method string, args *openstack.Args) error {
message, err := json.EncodeClientRequest(method, args)
checkErr(err)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(message))
checkErr(err)
req.Header.Set("Content-Type", "application/json")
client := new(http.Client)
resp, err := client.Do(req)
if err != nil {
log.Printf("Error in sending request to %s. %s", url, err)
return err
}
defer resp.Body.Close()
err = json.DecodeClientResponse(resp.Body, &result)
if err != nil {
log.Printf("Couldn't decode response. %s", err)
return err
}
log.Printf("url: %s, method: %s, args: %s", url, method, args)
return nil
}
示例5: proxyRequest
func proxyRequest(method, host string, ssl bool, res interface{}) *probe.Error {
u := &url.URL{
Scheme: func() string {
if ssl {
return "https"
}
return "http"
}(),
Host: host,
Path: "/rpc",
}
op := rpcOperation{
Method: method,
Request: ServerArg{},
}
request, err := newRPCRequest(u.String(), op, nil)
if err != nil {
return err.Trace()
}
var resp *http.Response
resp, err = request.Do()
if err != nil {
return err.Trace()
}
if err := json.DecodeClientResponse(resp.Body, res); err != nil {
return probe.NewError(err)
}
return nil
}
示例6: Execute
func (rc *RpcClient) Execute(method string, req, res interface{}) error {
buf, _ := json.EncodeClientRequest(method, req)
body := bytes.NewBuffer(buf)
log.Println(body)
httpreq, _ := http.NewRequest("POST", rc.Address, body)
httpreq.Header.Set("Content-Type", "application/json")
response, err := rc.Httpclient.Do(httpreq)
if err != nil {
return err
}
return json.DecodeClientResponse(response.Body, res)
}
示例7: TestDiskStats
func (s *ControllerRPCSuite) TestDiskStats(c *C) {
op := rpcOperation{
Method: "Controller.GetServerDiskStats",
Request: ControllerArgs{Hosts: []string{s.url.Host}},
}
req, err := newRPCRequest(testControllerRPC.URL+"/rpc", op, http.DefaultTransport)
c.Assert(err, IsNil)
c.Assert(req.Get("Content-Type"), Equals, "application/json")
resp, err := req.Do()
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusOK)
var reply MemStatsRep
c.Assert(json.DecodeClientResponse(resp.Body, &reply), IsNil)
resp.Body.Close()
c.Assert(reply, Not(DeepEquals), DiskStatsRep{})
}
示例8: TestServerAdd
func (s *ControllerRPCSuite) TestServerAdd(c *C) {
op := rpcOperation{
Method: "Controller.AddServer",
Request: ControllerArgs{Host: s.url.Host},
}
req, err := newRPCRequest(s.config, testControllerRPC.URL+"/rpc", op, http.DefaultTransport)
c.Assert(err, IsNil)
c.Assert(req.Get("Content-Type"), Equals, "application/json")
resp, err := req.Do()
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusOK)
var reply DefaultRep
c.Assert(json.DecodeClientResponse(resp.Body, &reply), IsNil)
resp.Body.Close()
c.Assert(reply, Not(DeepEquals), DefaultRep{nil, "Added"})
}
示例9: TestSysInfo
func (s *MyRPCSuite) TestSysInfo(c *C) {
op := controller.RPCOps{
Method: "SysInfo.Get",
Request: rpc.Args{Request: ""},
}
req, err := controller.NewRequest(testRPCServer.URL+"/rpc", op, http.DefaultTransport)
c.Assert(err, IsNil)
c.Assert(req.Get("Content-Type"), Equals, "application/json")
resp, err := req.Do()
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusOK)
var reply rpc.SysInfoReply
c.Assert(jsonrpc.DecodeClientResponse(resp.Body, &reply), IsNil)
resp.Body.Close()
c.Assert(reply, Not(DeepEquals), rpc.SysInfoReply{})
}
示例10: TestAuth
func (s *MySuite) TestAuth(c *C) {
op := rpc.Operation{
Method: "Auth.Get",
Request: rpc.Args{Request: ""},
}
req, err := rpc.NewRequest(testRPCServer.URL+"/rpc", op, http.DefaultTransport)
c.Assert(err, IsNil)
c.Assert(req.Get("Content-Type"), Equals, "application/json")
resp, err := req.Do()
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusOK)
var reply rpc.AuthReply
c.Assert(jsonrpc.DecodeClientResponse(resp.Body, &reply), IsNil)
resp.Body.Close()
c.Assert(reply, Not(DeepEquals), rpc.AuthReply{})
c.Assert(len(reply.AccessKeyID), Equals, 20)
c.Assert(len(reply.SecretAccessKey), Equals, 40)
}
示例11: GetSysInfo
// GetSysInfo get system status of the server at given url
func GetSysInfo(url string) ([]byte, error) {
op := RPCOps{
Method: "SysInfo.Get",
Request: rpc.Args{Request: ""},
}
req, err := NewRequest(url, op, http.DefaultTransport)
if err != nil {
return nil, iodine.New(err, nil)
}
resp, err := req.Do()
defer closeResp(resp)
if err != nil {
return nil, iodine.New(err, nil)
}
var reply rpc.SysInfoReply
if err := jsonrpc.DecodeClientResponse(resp.Body, &reply); err != nil {
return nil, iodine.New(err, nil)
}
return json.MarshalIndent(reply, "", "\t")
}
示例12: GetDisks
// GetDisks get disks info of the server at given url
func GetDisks(url string) ([]string, error) {
op := RPCOps{
Method: "DiskInfo.Get",
Request: rpc.Args{Request: ""},
}
req, err := NewRequest(url, op, http.DefaultTransport)
if err != nil {
return nil, iodine.New(err, nil)
}
resp, err := req.Do()
if err != nil {
return nil, iodine.New(err, nil)
}
defer resp.Body.Close()
var reply rpc.DiskInfoReply
if err := jsonrpc.DecodeClientResponse(resp.Body, &reply); err != nil {
return nil, iodine.New(err, nil)
}
return reply.Disks, nil
}
示例13: GetMemStats
// GetMemStats get memory status of the server at given url
func GetMemStats(url string) ([]byte, *probe.Error) {
op := RPCOps{
Method: "MemStats.Get",
Request: rpc.Args{Request: ""},
}
req, perr := NewRequest(url, op, http.DefaultTransport)
if perr != nil {
return nil, perr.Trace()
}
resp, perr := req.Do()
defer closeResp(resp)
if perr != nil {
return nil, perr.Trace()
}
var reply rpc.MemStatsReply
if err := jsonrpc.DecodeClientResponse(resp.Body, &reply); err != nil {
return nil, probe.NewError(err)
}
jsonRespBytes, err := json.MarshalIndent(reply, "", "\t")
if err != nil {
return nil, probe.NewError(err)
}
return jsonRespBytes, nil
}
示例14: TestAuth
func (s *ControllerRPCSuite) TestAuth(c *C) {
op := rpcOperation{
Method: "Controller.GenerateAuth",
Request: AuthArgs{User: "newuser"},
}
req, err := newRPCRequest(testControllerRPC.URL+"/rpc", op, http.DefaultTransport)
c.Assert(err, IsNil)
c.Assert(req.Get("Content-Type"), Equals, "application/json")
resp, err := req.Do()
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusOK)
var reply AuthRep
c.Assert(json.DecodeClientResponse(resp.Body, &reply), IsNil)
resp.Body.Close()
c.Assert(reply, Not(DeepEquals), AuthRep{})
c.Assert(len(reply.AccessKeyID), Equals, 20)
c.Assert(len(reply.SecretAccessKey), Equals, 40)
c.Assert(len(reply.Name), Not(Equals), 0)
op = rpcOperation{
Method: "Controller.FetchAuth",
Request: AuthArgs{User: "newuser"},
}
req, err = newRPCRequest(testControllerRPC.URL+"/rpc", op, http.DefaultTransport)
c.Assert(err, IsNil)
c.Assert(req.Get("Content-Type"), Equals, "application/json")
resp, err = req.Do()
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusOK)
var newReply AuthRep
c.Assert(json.DecodeClientResponse(resp.Body, &newReply), IsNil)
resp.Body.Close()
c.Assert(newReply, Not(DeepEquals), AuthRep{})
c.Assert(reply.AccessKeyID, Equals, newReply.AccessKeyID)
c.Assert(reply.SecretAccessKey, Equals, newReply.SecretAccessKey)
c.Assert(len(reply.Name), Not(Equals), 0)
op = rpcOperation{
Method: "Controller.ResetAuth",
Request: AuthArgs{User: "newuser"},
}
req, err = newRPCRequest(testControllerRPC.URL+"/rpc", op, http.DefaultTransport)
c.Assert(err, IsNil)
c.Assert(req.Get("Content-Type"), Equals, "application/json")
resp, err = req.Do()
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusOK)
var resetReply AuthRep
c.Assert(json.DecodeClientResponse(resp.Body, &resetReply), IsNil)
resp.Body.Close()
c.Assert(newReply, Not(DeepEquals), AuthRep{})
c.Assert(reply.AccessKeyID, Not(Equals), resetReply.AccessKeyID)
c.Assert(reply.SecretAccessKey, Not(Equals), resetReply.SecretAccessKey)
c.Assert(len(reply.Name), Not(Equals), 0)
// these operations should fail
/// generating access for existing user fails
op = rpcOperation{
Method: "Controller.GenerateAuth",
Request: AuthArgs{User: "newuser"},
}
req, err = newRPCRequest(testControllerRPC.URL+"/rpc", op, http.DefaultTransport)
c.Assert(err, IsNil)
c.Assert(req.Get("Content-Type"), Equals, "application/json")
resp, err = req.Do()
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusBadRequest)
/// null user provided invalid
op = rpcOperation{
Method: "Controller.GenerateAuth",
Request: AuthArgs{User: ""},
}
req, err = newRPCRequest(testControllerRPC.URL+"/rpc", op, http.DefaultTransport)
c.Assert(err, IsNil)
c.Assert(req.Get("Content-Type"), Equals, "application/json")
resp, err = req.Do()
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusBadRequest)
}