Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add retry logic to client's Validate method #150

Merged
merged 9 commits into from
Apr 18, 2018
Merged
32 changes: 9 additions & 23 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ package datadog

import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"net/http"
"os"
"time"
)

Expand Down Expand Up @@ -69,43 +67,31 @@ func (c *Client) GetBaseUrl() string {

// Validate checks if the API and application keys are valid.
func (client *Client) Validate() (bool, error) {
var bodyreader io.Reader
var out valid
var resp *http.Response

uri, err := client.uriForAPI("/v1/validate")
if err != nil {
return false, err
}
req, err := http.NewRequest("GET", uri, bodyreader)

if err != nil {
return false, err
}
if bodyreader != nil {
req.Header.Add("Content-Type", "application/json")
}
req, err := http.NewRequest("GET", uri, nil)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still needs checking if we got an error here.

@GrantSheehan btw, you were absolutely right about inline error checking. I stand corrected!


var resp *http.Response
resp, err = client.HttpClient.Do(req)
resp, err = client.doRequestWithRetries(req, client.RetryTimeout)
if err != nil {
return false, err
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still needs a defer to close the response.

defer resp.Body.Close()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not defer closing resp.Body?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was getting errors in unmarshaling JSON during testing, I think it was because I had it in the wrong scope. I've added it back.


// Only care about 200 OK or 403 which we'll unmarshal into struct valid. Everything else is of no interest to us.
if resp.StatusCode != 200 && resp.StatusCode != 403 {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return false, err
}
return false, fmt.Errorf("API error %s: %s", resp.Status, body)
}

body, err := ioutil.ReadAll(resp.Body)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check the value of err

err = json.Unmarshal(body, &out)
if err != nil {
return false, err
}

if err = json.Unmarshal(body, &out); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

return false, err
}

return out.IsValid, nil
}