这个类 我应该如何去调用 login() ,像这种SinAPIClient 怎么把账号密码HOST等数据穿进去等 这样的一个方法
第一次用到go 希望大牛可以帮帮我, 测试一天了 也都搞不定 总报错 因为急着要调试点东西。所以没办法了
```go
package client
import (
"errors"
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/nilshell/xmlrpc"
)
type SinAPIClient struct {
Session interface{}
Host string
Url string
Username string
Password string
RPC *xmlrpc.Client
}
type APIResult struct {
Status string
Value interface{}
ErrorDescription string
}
type XenAPIObject struct {
Ref string
Client *SinAPIClient
}
func (c *SinAPIClient) RPCCall(result interface{}, method string, params []interface{}) (err error) {
log.Debugf("RPCCall method=%v params=%v\n", method, params)
p := new(xmlrpc.Params)
p.Params = params
err = c.RPC.Call(method, *p, result)
return err
}
func (client *SinAPIClient) Login() (err error) {
//Do loging call
result := xmlrpc.Struct{}
params := make([]interface{}, 2)
params[0] = client.Username
params[1] = client.Password
err = client.RPCCall(&result, "session.login", params)
if err == nil {
// err might not be set properly, so check the reference
if result["Value"] == nil {
return errors.New("Invalid credentials supplied")
}
}
client.Session = result["Value"]
return err
}
func (client *SinAPIClient) APICall(result *APIResult, method string, params ...interface{}) (err error) {
if client.Session == nil {
log.Errorf("no session\n")
return fmt.Errorf("No session. Unable to make call")
}
//Make a params slice which will include the session
p := make([]interface{}, len(params)+1)
p[0] = client.Session
if params != nil {
for idx, element := range params {
p[idx+1] = element
}
}
res := xmlrpc.Struct{}
err = client.RPCCall(&res, method, p)
if err != nil {
return err
}
result.Status = res["Status"].(string)
if result.Status != "Success" {
log.Errorf("Encountered an API error: %v %v", result.Status, res["ErrorDescription"])
return fmt.Errorf("API Error: %s", res["ErrorDescription"])
} else {
result.Value = res["Value"]
}
return
}
```
有疑问加站长微信联系(非本文作者)