-
Notifications
You must be signed in to change notification settings - Fork 156
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
Changes from 8 commits
cfd7c9c
3a3f08e
bb258a6
fb62889
f4ac3fe
b004ee9
0211f83
ea2618f
963eee6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,11 +10,9 @@ package datadog | |
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"net/http" | ||
"os" | ||
"time" | ||
) | ||
|
||
|
@@ -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) | ||
|
||
var resp *http.Response | ||
resp, err = client.HttpClient.Do(req) | ||
resp, err = client.doRequestWithRetries(req, client.RetryTimeout) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still needs a defer to close the response. |
||
defer resp.Body.Close() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not defer closing There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please check the value of |
||
err = json.Unmarshal(body, &out) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
if err = json.Unmarshal(body, &out); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
return false, err | ||
} | ||
|
||
return out.IsValid, nil | ||
} |
There was a problem hiding this comment.
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!