Skip to content

Commit

Permalink
Add retry logic to client's Validate method (#150)
Browse files Browse the repository at this point in the history
Add retry logic to client's Validate method
  • Loading branch information
GrantSheehan authored and ojongerius committed Apr 18, 2018
1 parent 591deef commit ea1134d
Showing 1 changed file with 9 additions and 20 deletions.
29 changes: 9 additions & 20 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,34 @@ 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)

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

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

defer resp.Body.Close()

// 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)
err = json.Unmarshal(body, &out)
if err != nil {
return false, err
}

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

return out.IsValid, nil
}

0 comments on commit ea1134d

Please sign in to comment.