Skip to content

Commit

Permalink
refactoring based on feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
corylanou committed Aug 3, 2015
1 parent 981319e commit 2e3b595
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 263 deletions.
54 changes: 29 additions & 25 deletions client/influxdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ import (
)

const (
// DEFAULT_HOST is the default host used to connect to an InfluxDB instance
DEFAULT_HOST = "localhost"
// DEFAULT_PORT is the default port used to connect to an InfluxDB instance
DEFAULT_PORT = 8086
// DEFAULT_TIMEOUT is the default connection timeout used to connect to an InfluxDB instance
DEFAULT_TIMEOUT = 0
// DefaultHost is the default host used to connect to an InfluxDB instance
DefaultHost = "localhost"

// DefaultPort is the default port used to connect to an InfluxDB instance
DefaultPort = 8086

// DefaultTimeout is the default connection timeout used to connect to an InfluxDB instance
DefaultTimeout = 0
)

// Query is used to send a command to the server. Both Command and Database are required.
Expand All @@ -46,14 +48,14 @@ func ParseConnectionString(path string, ssl bool) (url.URL, error) {
}
port = i
if h[0] == "" {
host = DEFAULT_HOST
host = DefaultHost
} else {
host = h[0]
}
} else {
host = path
// If they didn't specify a port, always use the default port
port = DEFAULT_PORT
port = DefaultPort
}

u := url.URL{
Expand Down Expand Up @@ -87,6 +89,7 @@ func NewConfig(u url.URL, username, password, userAgent string, timeout time.Dur
Username: username,
Password: password,
UserAgent: userAgent,
Timeout: timeout,
}
}

Expand Down Expand Up @@ -176,7 +179,8 @@ func (c *Client) Query(q Query) (*Response, error) {
// If successful, error is nil and Response is nil
// If an error occurs, Response may contain additional information if populated.
func (c *Client) Write(bp BatchPoints) (*Response, error) {
c.url.Path = "write"
u := c.url
u.Path = "write"

var b bytes.Buffer
for _, p := range bp.Points {
Expand All @@ -202,7 +206,7 @@ func (c *Client) Write(bp BatchPoints) (*Response, error) {
}
}

req, err := http.NewRequest("POST", c.url.String(), &b)
req, err := http.NewRequest("POST", u.String(), &b)
if err != nil {
return nil, err
}
Expand All @@ -212,10 +216,10 @@ func (c *Client) Write(bp BatchPoints) (*Response, error) {
req.SetBasicAuth(c.username, c.password)
}
params := req.URL.Query()
params.Add("db", bp.Database)
params.Add("rp", bp.RetentionPolicy)
params.Add("precision", bp.Precision)
params.Add("consistency", bp.WriteConsistency)
params.Set("db", bp.Database)
params.Set("rp", bp.RetentionPolicy)
params.Set("precision", bp.Precision)
params.Set("consistency", bp.WriteConsistency)
req.URL.RawQuery = params.Encode()

resp, err := c.httpClient.Do(req)
Expand All @@ -226,7 +230,7 @@ func (c *Client) Write(bp BatchPoints) (*Response, error) {

var response Response
body, err := ioutil.ReadAll(resp.Body)
if err != nil && err.Error() != "EOF" {
if err != nil {
return nil, err
}

Expand All @@ -243,12 +247,12 @@ func (c *Client) Write(bp BatchPoints) (*Response, error) {
// If successful, error is nil and Response is nil
// If an error occurs, Response may contain additional information if populated.
func (c *Client) WriteLineProtocol(data, database, retentionPolicy, precision, writeConsistency string) (*Response, error) {
c.url.Path = "write"
u := c.url
u.Path = "write"

var b bytes.Buffer
b.WriteString(data)
r := strings.NewReader(data)

req, err := http.NewRequest("POST", c.url.String(), &b)
req, err := http.NewRequest("POST", u.String(), r)
if err != nil {
return nil, err
}
Expand All @@ -258,10 +262,10 @@ func (c *Client) WriteLineProtocol(data, database, retentionPolicy, precision, w
req.SetBasicAuth(c.username, c.password)
}
params := req.URL.Query()
params.Add("db", database)
params.Add("rp", retentionPolicy)
params.Add("precision", precision)
params.Add("consistency", writeConsistency)
params.Set("db", database)
params.Set("rp", retentionPolicy)
params.Set("precision", precision)
params.Set("consistency", writeConsistency)
req.URL.RawQuery = params.Encode()

resp, err := c.httpClient.Do(req)
Expand All @@ -272,12 +276,12 @@ func (c *Client) WriteLineProtocol(data, database, retentionPolicy, precision, w

var response Response
body, err := ioutil.ReadAll(resp.Body)
if err != nil && err.Error() != "EOF" {
if err != nil {
return nil, err
}

if resp.StatusCode != http.StatusNoContent && resp.StatusCode != http.StatusOK {
var err = fmt.Errorf(string(body))
err := fmt.Errorf(string(body))
response.Err = err
return &response, err
}
Expand Down
222 changes: 0 additions & 222 deletions cmd/influx/importer/v8.go

This file was deleted.

Loading

0 comments on commit 2e3b595

Please sign in to comment.