| 1 |
package client |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/json" |
| 6 |
"fmt" |
| 7 |
"net" |
| 8 |
"net/http" |
| 9 |
"time" |
| 10 |
) |
| 11 |
|
| 12 |
// URL is the default URL for the host of Enterprise. |
| 13 |
// This variable can be set globally or on a per Client |
| 14 |
// instance. |
| 15 |
var URL = "https://enterprise.influxdata.com" |
| 16 |
|
| 17 |
// Client handles all of the heavy lifting of talking |
| 18 |
// to Enterprise for you. |
| 19 |
type Client struct { |
| 20 |
URL string // Defaults to `client.URL` |
| 21 |
Token string // OPTIONAL: The token of the customer making the request |
| 22 |
} |
| 23 |
|
| 24 |
// New returns a configured `Client`. The `token` |
| 25 |
// is optional, but if you have it, you should pass |
| 26 |
// it in. |
| 27 |
func New(token string) *Client { |
| 28 |
return &Client{ |
| 29 |
URL: URL, |
| 30 |
Token: token, |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
// Saveable needs to be implemented for types that |
| 35 |
// want to be able to be saved to the Enterprise API. |
| 36 |
type Saveable interface { |
| 37 |
// Path returns specific path to where this type should |
| 38 |
// be saved, that is everything in the path __after__ "/api/v1". |
| 39 |
Path() string |
| 40 |
} |
| 41 |
|
| 42 |
// Save does all of the heavy lifting of saving a Saveable |
| 43 |
// Type to the Enterpise API. This will take care of things |
| 44 |
// like building the full path, setting the `token` on the |
| 45 |
// request if one is available, etc... It will also check |
| 46 |
// the status code of the response and handle non-successful |
| 47 |
// responses by generating a proper `error` for them. |
| 48 |
func (c *Client) Save(s Saveable) (*http.Response, error) { |
| 49 |
u := fmt.Sprintf("%s/api/v1%s", c.URL, s.Path()) |
| 50 |
|
| 51 |
b, err := json.Marshal(s) |
| 52 |
if err != nil { |
| 53 |
return nil, err |
| 54 |
} |
| 55 |
|
| 56 |
req, err := http.NewRequest("POST", u, bytes.NewReader(b)) |
| 57 |
req.Header.Set("Content-Type", "application/json") |
| 58 |
if err != nil { |
| 59 |
return nil, err |
| 60 |
} |
| 61 |
if c.Token != "" { |
| 62 |
req.Header.Set("X-Authorization", c.Token) |
| 63 |
} |
| 64 |
|
| 65 |
cl := http.Client{ |
| 66 |
Timeout: time.Minute, |
| 67 |
Transport: &http.Transport{ |
| 68 |
MaxIdleConnsPerHost: 0, |
| 69 |
Proxy: http.ProxyFromEnvironment, |
| 70 |
Dial: (&net.Dialer{ |
| 71 |
Timeout: 30 * time.Second, |
| 72 |
KeepAlive: 30 * time.Second, |
| 73 |
}).Dial, |
| 74 |
TLSHandshakeTimeout: 10 * time.Second, |
| 75 |
}, |
| 76 |
} |
| 77 |
res, err := cl.Do(req) |
| 78 |
if err != nil { |
| 79 |
return res, err |
| 80 |
} |
| 81 |
|
| 82 |
code := res.StatusCode |
| 83 |
switch code { |
| 84 |
case 401, 404, 500: |
| 85 |
se := SimpleError{} |
| 86 |
err = json.NewDecoder(res.Body).Decode(&se) |
| 87 |
if err != nil { |
| 88 |
return res, err |
| 89 |
} |
| 90 |
return res, se |
| 91 |
case 422: |
| 92 |
ve := ValidationErrors{} |
| 93 |
err = json.NewDecoder(res.Body).Decode(&ve) |
| 94 |
if err != nil { |
| 95 |
return res, err |
| 96 |
} |
| 97 |
return res, ve |
| 98 |
} |
| 99 |
|
| 100 |
return res, err |
| 101 |
} |