Skip to content

Commit

Permalink
Update session.go
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiang committed Feb 22, 2025
1 parent 6118754 commit 80d8061
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (c *Session) login(username, password string) error {
params["username"] = username
}

res, err := c.Do(NewRequest("user.login", params))
res, err := c.Do(NewRequest("user.login", params), true)
if err != nil {
return fmt.Errorf("Error logging in to Zabbix API: %v", err)
}
Expand All @@ -89,7 +89,7 @@ func (c *Session) login(username, password string) error {
func (c *Session) GetVersion() (*types.ZBXVersion, error) {
if c.APIVersion == nil {
// get Zabbix API version
res, err := c.Do(NewRequest("apiinfo.version", nil))
res, err := c.Do(NewRequest("apiinfo.version", nil), true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -118,16 +118,18 @@ func (c *Session) AuthToken() string {
// When err is nil, resp always contains a non-nil resp.Body.
//
// Generally Get or a wrapper function will be used instead of Do.
func (c *Session) Do(req *Request) (resp *Response, err error) {
// get Zabbix API version
ver, err := c.GetVersion()
if err != nil {
return nil, fmt.Errorf("Failed to retrieve Zabbix API version: %v", err)
}
func (c *Session) Do(req *Request, noAuth bool) (resp *Response, err error) {
if noAuth == false {
// get Zabbix API version
ver, err := c.GetVersion()
if err != nil {
return nil, fmt.Errorf("Failed to retrieve Zabbix API version: %v", err)
}

// configure request
if ver.Compare(zabbixVersion640) < 0 {
req.AuthToken = c.Token
// configure request
if ver.Compare(zabbixVersion640) < 0 {
req.AuthToken = c.Token
}
}

// encode request as json
Expand All @@ -145,7 +147,9 @@ func (c *Session) Do(req *Request) (resp *Response, err error) {
}
r.ContentLength = int64(len(b))
r.Header.Add("Content-Type", "application/json-rpc")
r.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.Token))
if noAuth == false {
r.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.Token))
}

// send request
client := c.client
Expand Down Expand Up @@ -192,7 +196,7 @@ func (c *Session) Do(req *Request) (resp *Response, err error) {
// An error is return if a transport, marshalling or API error happened.
func (c *Session) Get(method string, params interface{}, v interface{}) error {
req := NewRequest(method, params)
resp, err := c.Do(req)
resp, err := c.Do(req, false)
if err != nil {
return err
}
Expand Down

0 comments on commit 80d8061

Please sign in to comment.