Skip to content

Commit

Permalink
Merge pull request #3 from llnw/master
Browse files Browse the repository at this point in the history
Feature: make session fields public and serializable
  • Loading branch information
cavaliercoder committed Mar 2, 2018
2 parents 921bd4e + 76ade43 commit afab812
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ var ErrNotFound = errors.New("No results were found matching the given search pa
// A Session is an authenticated Zabbix JSON-RPC API client. It must be
// initialized and connected with NewSession.
type Session struct {
// url is the URL of the Zabbix JSON-RPC API (ending in `/api_jsonrpc.php`).
url string
// URL of the Zabbix JSON-RPC API (ending in `/api_jsonrpc.php`).
URL string `json:"url"`

// authToken is the cached authentication token returned by `user.login` and
// Token is the cached authentication token returned by `user.login` and
// used to authenticate all API calls in this Session.
authToken string
Token string `json:"token"`

// apiVersion is the software version string of the connected Zabbix API.
apiVersion string
// ApiVersion is the software version string of the connected Zabbix API.
APIVersion string `json:"apiVersion"`
}

// NewSession returns a new Session given an API connection URL and an API
Expand All @@ -36,15 +36,15 @@ type Session struct {
// authenticate all subsequent requests in this Session.
func NewSession(url string, username string, password string) (session *Session, err error) {
// create session
session = &Session{url: url}
session = &Session{URL: url}

// get Zabbix API version
res, err := session.Do(NewRequest("apiinfo.version", nil))
if err != nil {
return nil, fmt.Errorf("Error getting Zabbix API version: %v", err)
}

err = res.Bind(&session.apiVersion)
err = res.Bind(&session.APIVersion)
if err != nil {
return
}
Expand All @@ -60,7 +60,7 @@ func NewSession(url string, username string, password string) (session *Session,
return nil, fmt.Errorf("Error logging in to Zabbix API: %v", err)
}

err = res.Bind(&session.authToken)
err = res.Bind(&session.Token)
if err != nil {
return
}
Expand All @@ -70,13 +70,13 @@ func NewSession(url string, username string, password string) (session *Session,

// Version returns the software version string of the connected Zabbix API.
func (c *Session) Version() string {
return c.apiVersion
return c.APIVersion
}

// AuthToken returns the authentication token used by this session to
// authentication all API calls.
func (c *Session) AuthToken() string {
return c.authToken
return c.Token
}

// Do sends a JSON-RPC request and returns an API Response, using connection
Expand All @@ -90,7 +90,7 @@ func (c *Session) AuthToken() string {
// Generally Get or a wrapper function will be used instead of Do.
func (c *Session) Do(req *Request) (resp *Response, err error) {
// configure request
req.AuthToken = c.authToken
req.AuthToken = c.Token

// encode request as json
b, err := json.Marshal(req)
Expand All @@ -101,7 +101,7 @@ func (c *Session) Do(req *Request) (resp *Response, err error) {
dprintf("Call [%s:%d]: %s\n", req.Method, req.RequestID, b)

// create HTTP request
r, err := http.NewRequest("POST", c.url, bytes.NewReader(b))
r, err := http.NewRequest("POST", c.URL, bytes.NewReader(b))
if err != nil {
return
}
Expand Down

0 comments on commit afab812

Please sign in to comment.