Skip to content

Commit

Permalink
fix issue with client.Refresh timer
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Hemingway committed Dec 11, 2019
1 parent a399c8f commit b536484
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func RequestTimeout(x time.Duration) func(*Client) {
}

// Get makes a GET request and returns a GJSON result.
func (client Client) Get(path string, mods ...func(*Req)) (Res, error) {
func (client *Client) Get(path string, mods ...func(*Req)) (Res, error) {
req := NewReq("GET", client.url+path, nil, mods...)

if req.refresh && time.Now().Sub(client.lastRefresh) > 480*time.Second {
Expand All @@ -87,7 +87,7 @@ func (client Client) Get(path string, mods ...func(*Req)) (Res, error) {
}

// GetClass makes a GET request by class and unwraps the results.
func (client Client) GetClass(class string, mods ...func(*Req)) (Res, error) {
func (client *Client) GetClass(class string, mods ...func(*Req)) (Res, error) {
res, err := client.Get(fmt.Sprintf("/api/class/%s", class), mods...)
if err != nil {
return res, err
Expand All @@ -96,7 +96,7 @@ func (client Client) GetClass(class string, mods ...func(*Req)) (Res, error) {
}

// GetDn makes a GET request by DN.
func (client Client) GetDn(dn string, mods ...func(*Req)) (Res, error) {
func (client *Client) GetDn(dn string, mods ...func(*Req)) (Res, error) {
res, err := client.Get(fmt.Sprintf("/api/mo/%s", dn), mods...)
if err != nil {
return res, err
Expand All @@ -105,7 +105,7 @@ func (client Client) GetDn(dn string, mods ...func(*Req)) (Res, error) {
}

// Post makes a POST request and returns a GJSON result.
func (client Client) Post(path, data string, mods ...func(*Req)) (Res, error) {
func (client *Client) Post(path, data string, mods ...func(*Req)) (Res, error) {
req := NewReq("POST", client.url+path, strings.NewReader(data), mods...)
if req.refresh && time.Now().Sub(client.lastRefresh) > 480*time.Second {
if err := client.Refresh(); err != nil {
Expand All @@ -129,7 +129,7 @@ func (client Client) Post(path, data string, mods ...func(*Req)) (Res, error) {
}

// Login authenticates to the Client.
func (client Client) Login() error {
func (client *Client) Login() error {
data := fmt.Sprintf(`{"aaaUser":{"attributes":{"name":"%s","pwd":"%s"}}}`,
client.usr,
client.pwd,
Expand All @@ -147,7 +147,11 @@ func (client Client) Login() error {
}

// Refresh refreshes the authentication token.
func (client Client) Refresh() error {
func (client *Client) Refresh() error {
_, err := client.Get("/api/aaaRefresh", NoRefresh)
return err
if err != nil {
return err
}
client.lastRefresh = time.Now()
return nil
}

0 comments on commit b536484

Please sign in to comment.