File: client.go

package info (click to toggle)
golang-github-influxdb-usage-client 0.0~git20151204.0.475977e-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 152 kB
  • sloc: makefile: 2
file content (101 lines) | stat: -rw-r--r-- 2,402 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package client

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net"
	"net/http"
	"time"
)

// URL is the default URL for the host of the Usage API.
// This variable can be set globally or on a per Client
// instance.
var URL = "https://usage.influxdata.com"

// Client handles all of the heavy lifting of talking
// to the Usage API for you.
type Client struct {
	URL   string // Defaults to `client.URL`
	Token string // OPTIONAL: The token of the customer making the request
}

// New returns a configured `Client`. The `token`
// is optional, but if you have it, you should pass
// it in.
func New(token string) *Client {
	return &Client{
		URL:   URL,
		Token: token,
	}
}

// Saveable needs to be implemented for types that
// want to be able to be saved to the Usage API.
type Saveable interface {
	// Path returns specific path to where this type should
	// be saved, that is everything in the path __after__ "/api/v1".
	Path() string
}

// Save does all of the heavy lifting of saving a Saveable
// Type to the Usage API. This will take care of things
// like building the full path, setting the `token` on the
// request if one is available, etc... It will also check
// the status code of the response and handle non-successful
// responses by generating a proper `error` for them.
func (c *Client) Save(s Saveable) (*http.Response, error) {
	u := fmt.Sprintf("%s/api/v1%s", c.URL, s.Path())

	b, err := json.Marshal(s)
	if err != nil {
		return nil, err
	}

	req, err := http.NewRequest("POST", u, bytes.NewReader(b))
	req.Header.Set("Content-Type", "application/json")
	if err != nil {
		return nil, err
	}
	if c.Token != "" {
		req.Header.Set("X-Authorization", c.Token)
	}

	cl := http.Client{
		Timeout: time.Minute,
		Transport: &http.Transport{
			MaxIdleConnsPerHost: 0,
			Proxy:               http.ProxyFromEnvironment,
			Dial: (&net.Dialer{
				Timeout:   30 * time.Second,
				KeepAlive: 30 * time.Second,
			}).Dial,
			TLSHandshakeTimeout: 10 * time.Second,
		},
	}
	res, err := cl.Do(req)
	if err != nil {
		return res, err
	}

	code := res.StatusCode
	switch code {
	case 401, 404, 500:
		se := SimpleError{}
		err = json.NewDecoder(res.Body).Decode(&se)
		if err != nil {
			return res, err
		}
		return res, se
	case 422:
		ve := ValidationErrors{}
		err = json.NewDecoder(res.Body).Decode(&ve)
		if err != nil {
			return res, err
		}
		return res, ve
	}

	return res, err
}