<commit_before>package client

import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"net/http"
	"net/url"
	"time"

	"github.com/influxdb/influxdb/influxql"
)

type Query struct {
	Command  string
	Database string
}

type Config struct {
	URL       url.URL
	Username  string
	Password  string
	UserAgent string
}

type Client struct {
	url        url.URL
	username   string
	password   string
	httpClient *http.Client
	userAgent  string
}

func NewClient(c Config) (*Client, error) {
	client := Client{
		url:        c.URL,
		username:   c.Username,
		password:   c.Password,
		httpClient: &http.Client{},
		userAgent:  c.UserAgent,
	}
	return &client, nil
}

func (c *Client) Query(q Query) (*Results, error) {
	u := c.url

	u.Path = "query"
	if c.username != "" {
		u.User = url.UserPassword(c.username, c.password)
	}
	values := u.Query()
	values.Set("q", q.Command)
	values.Set("db", q.Database)
	u.RawQuery = values.Encode()

	req, err := http.NewRequest("GET", u.String(), nil)
	if err != nil {
		return nil, err
	}
	if c.userAgent != "" {
		req.Header.Set("User-Agent", c.userAgent)
	}
	resp, err := c.httpClient.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	var results Results
	dec := json.NewDecoder(resp.Body)
	dec.UseNumber()
	err = dec.Decode(&results)
	if err != nil {
		return nil, err
	}
	return &results, nil
}

func (c *Client) Write(bp BatchPoints) (*Results, error) {
	c.url.Path = "write"

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

	req, err := http.NewRequest("POST", c.url.String(), bytes.NewBuffer(b))
	if err != nil {
		return nil, err
	}
	req.Header.Set("Content-Type", "application/json")
	if c.userAgent != "" {
		req.Header.Set("User-Agent", c.userAgent)
	}
	resp, err := c.httpClient.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	var results Results
	dec := json.NewDecoder(resp.Body)
	dec.UseNumber()
	err = dec.Decode(&results)
	if err != nil && err.Error() != "EOF" {
		return nil, err
	}

	if resp.StatusCode != http.StatusOK {
		return &results, results.Error()
	}

	return &results, nil
}

func (c *Client) Ping() (time.Duration, string, error) {
	now := time.Now()
	u := c.url
	u.Path = "ping"

	req, err := http.NewRequest("GET", u.String(), nil)
	if err != nil {
		return 0, "", err
	}
	if c.userAgent != "" {
		req.Header.Set("User-Agent", c.userAgent)
	}
	resp, err := c.httpClient.Do(req)
	if err != nil {
		return 0, "", err
	}
	version := resp.Header.Get("X-Influxdb-Version")
	return time.Since(now), version, nil
}

// Structs

// Result represents a resultset returned from a single statement.
type Result struct {
	Series []influxql.Row
	Err    error
}<commit_msg>default and send user agent<commit_after>package client

import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"net/http"
	"net/url"
	"time"

	"github.com/influxdb/influxdb/influxql"
)

// These variables are populated via the Go linker.
var (
	version string = "0.9"
)

type Query struct {
	Command  string
	Database string
}

type Config struct {
	URL       url.URL
	Username  string
	Password  string
	UserAgent string
}

type Client struct {
	url        url.URL
	username   string
	password   string
	httpClient *http.Client
	userAgent  string
}

func NewClient(c Config) (*Client, error) {
	client := Client{
		url:        c.URL,
		username:   c.Username,
		password:   <PASSWORD>,
		httpClient: &http.Client{},
		userAgent:  c.UserAgent,
	}
	if client.userAgent == "" {
		client.userAgent = "InfluxDBClient/" + version
	}
	return &client, nil
}

func (c *Client) Query(q Query) (*Results, error) {
	u := c.url

	u.Path = "query"
	if c.username != "" {
		u.User = url.UserPassword(c.username, c.password)
	}
	values := u.Query()
	values.Set("q", q.Command)
	values.Set("db", q.Database)
	u.RawQuery = values.Encode()

	req, err := http.NewRequest("GET", u.String(), nil)
	if err != nil {
		return nil, err
	}
	req.Header.Set("User-Agent", c.userAgent)
	resp, err := c.httpClient.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	var results Results
	dec := json.NewDecoder(resp.Body)
	dec.UseNumber()
	err = dec.Decode(&results)
	if err != nil {
		return nil, err
	}
	return &results, nil
}

func (c *Client) Write(bp BatchPoints) (*Results, error) {
	c.url.Path = "write"

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

	req, err := http.NewRequest("POST", c.url.String(), bytes.NewBuffer(b))
	if err != nil {
		return nil, err
	}
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("User-Agent", c.userAgent)
	resp, err := c.httpClient.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	var results Results
	dec := json.NewDecoder(resp.Body)
	dec.UseNumber()
	err = dec.Decode(&results)
	if err != nil && err.Error() != "EOF" {
		return nil, err
	}

	if resp.StatusCode != http.StatusOK {
		return &results, results.Error()
	}

	return &results, nil
}

func (c *Client) Ping() (time.Duration, string, error) {
	now := time.Now()
	u := c.url
	u.Path = "ping"

	req, err := http.NewRequest("GET", u.String(), nil)
	if err != nil {
		return 0, "", err
	}
	req.Header.Set("User-Agent", c.userAgent)
	resp, err := c.httpClient.Do(req)
	if err != nil {
		return 0, "", err
	}
	version := resp.Header.Get("X-Influxdb-Version")
	return time.Since(now), version, nil
}

// Structs

// Result represents a resultset returned from a single statement.
type Result struct {
	Series []influxql.Row
	Err    error
}
{'fraction_non_alphanumeric': 0.15845198977729097, 'fraction_numerical': 0.001095290251916758, 'mean_word_length': 3.3278041074249605, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7346555', 'n_tokens_mistral': 2116, 'n_tokens_neox': 2051, 'n_words': 535}