forked from influxdata/influxdb-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
111 lines (96 loc) · 2.74 KB
/
client.go
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
102
103
104
105
106
107
108
109
110
111
package influxdb
import (
"context"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"sync"
)
// TODO(docmerlin): change the generator so we don't have to hand edit the generated code
//go:generate go run scripts/buildclient.go
// Client is a client for writing to influx.
type Client struct {
httpClient *http.Client
contentEncoding string
compressionLevel int
url *url.URL
password string
username string
l sync.Mutex
errOnFieldErr bool
userAgent string
authorization string // the Authorization header
maxLineBytes int
}
// New creates a new Client.
// The client is concurrency safe, so feel free to use it and abuse it to your heart's content.
func New(connection string, token string, options ...Option) (*Client, error) {
c := &Client{
contentEncoding: "gzip",
compressionLevel: 4,
errOnFieldErr: true,
authorization: "Token " + token,
}
if connection == "" {
connection = `http://127.0.0.1:9999`
}
u, err := url.Parse(connection)
if err != nil {
return nil, fmt.Errorf("Error: could not parse url: %v", err)
}
u.Path = `/api/v2`
c.url = u
c.userAgent = userAgent()
for i := range options {
// check for incompatible options
if options[i].name == "WithGZIP" {
for j := range options {
if options[j].name == "WithNoCompression" {
return nil, errors.New("the WithGzip is incompatible with the WithNoCompression option")
}
}
}
if err := options[i].f(c); err != nil {
return nil, err
}
}
if c.httpClient == nil {
c.httpClient = defaultHTTPClient()
}
if c.authorization == "" && !(c.username != "" || c.password != "") {
return nil, errors.New("a token or a username and password is required, pass a token to New(), or use WithUserAndPass(\"the_username\",\"the_password\")")
}
return c, nil
}
// Ping checks the status of cluster.
func (c *Client) Ping(ctx context.Context) error {
// deep copy c.url, because we have an entirely different path
pingURL, _ := url.Parse(c.url.String()) // we don't check the error here, because it just came from an already parsed url.URL object.
pingURL.Path = "/ready"
req, err := http.NewRequest(http.MethodGet, pingURL.String(), nil)
if err != nil {
return err
}
req = req.WithContext(ctx)
resp, err := c.httpClient.Do(req)
if err != nil {
return err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
// we shouldn't see this
if resp.StatusCode != http.StatusNoContent && resp.StatusCode != http.StatusOK {
var err = errors.New(string(body))
return err
}
return nil
}
// Close closes any idle connections on the Client.
func (c *Client) Close() error {
c.httpClient.CloseIdleConnections()
return nil // we do this, so it qualifies as a closer.
}