<commit_before>// Go wrapper for the BitX API.
// The API is documented here: https://bitx.co/api
package bitx

import "time"
import "net/http"
import "net/url"
import "errors"
import "encoding/json"
import "strconv"
import "fmt"
import "bytes"
import "io/ioutil"
import _ "crypto/sha512"

const userAgent = "bitx-go/0.0.2"

var base = url.URL{Scheme: "https", Host: "api.mybitx.com"}

type Client struct {
	api_key_id, api_key_secret string
}

// Pass an empty string for the api_key_id if you will only access the public
// API.
func NewClient(api_key_id, api_key_secret string) *Client {
	return &Client{api_key_id, api_key_secret}
}

func (c *Client) call(method, path string, params url.Values,
	result interface{}) error {
	u := base
	u.Path = path

	var body *bytes.Reader
	if method == "GET" {
		u.RawQuery = params.Encode()
		body = bytes.NewReader(nil)
	} else if method == "POST" {
		body = bytes.NewReader([]byte(params.Encode()))
	} else {
		return errors.New("Unsupported method")
	}

	req, err := http.NewRequest(method, u.String(), body)
	if err != nil {
		return err
	}
	if c.api_key_id != "" {
		req.SetBasicAuth(c.api_key_id, c.api_key_secret)
	}
	req.Header.Add("User-Agent", userAgent)
	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	r, err := (&http.Client{}).Do(req)
	if err != nil {
		return err
	}
	defer r.Body.Close()

	if r.StatusCode != http.StatusOK {
		body, _ := ioutil.ReadAll(r.Body)
		return errors.New(fmt.Sprintf(
			"BitX error %d: %s: %s",
			r.StatusCode, r.Status, string(body)))
	}

	if err := json.NewDecoder(r.Body).Decode(result); err != nil {
		return err
	}

	return nil
}

type ticker struct {
	Error     string `json:"error"`
	Timestamp int64  `json:"timestamp"`
	Bid       string `json:"bid"`
	Ask       string `json:"ask"`
	Last      string `json:"last_trade"`
	Volume    string `json:"rolling_24_hour_volume"`
}

type Ticker struct {
	Timestamp              time.Time
	Bid, Ask, Last, Volume float64
}

// Returns the latest ticker indicators for the given currency pair..
func (c *Client) Ticker(pair string) (Ticker, error) {
	var r ticker
	err := c.call("GET", "/api/1/ticker", url.Values{"pair": {pair}}, &r)
	if err != nil {
		return Ticker{}, err
	}
	if r.Error != "" {
		return Ticker{}, errors.New("BitX error: " + r.Error)
	}

	t := time.Unix(r.Timestamp/1000, 0)

	bid, err := strconv.ParseFloat(r.Bid, 64)
	if err != nil {
		return Ticker{}, err
	}

	ask, err := strconv.ParseFloat(r.Ask, 64)
	if err != nil {
		return Ticker{}, err
	}

	last, err := strconv.ParseFloat(r.Last, 64)
	if err != nil {
		return Ticker{}, err
	}

	volume, err := strconv.ParseFloat(r.Volume, 64)
	if err != nil {
		return Ticker{}, err
	}

	return Ticker{t, bid, ask, last, volume}, nil
}

type orderbookEntry struct {
	Price  string `json:"price"`
	Volume string `json:"volume"`
}

type orderbook struct {
	Error string           `json:"error"`
	Asks  []orderbookEntry `json:"asks"`
	Bids  []orderbookEntry `json:"bids"`
}

type OrderBookEntry struct {
	Price, Volume float64
}<commit_msg>renamed to Volume to Volume24H<commit_after>// Go wrapper for the BitX API.
// The API is documented here: https://bitx.co/api
package bitx

import "time"
import "net/http"
import "net/url"
import "errors"
import "encoding/json"
import "strconv"
import "fmt"
import "bytes"
import "io/ioutil"
import _ "crypto/sha512"

const userAgent = "bitx-go/0.0.2"

var base = url.URL{Scheme: "https", Host: "api.mybitx.com"}

type Client struct {
	api_key_id, api_key_secret string
}

// Pass an empty string for the api_key_id if you will only access the public
// API.
func NewClient(api_key_id, api_key_secret string) *Client {
	return &Client{api_key_id, api_key_secret}
}

func (c *Client) call(method, path string, params url.Values,
	result interface{}) error {
	u := base
	u.Path = path

	var body *bytes.Reader
	if method == "GET" {
		u.RawQuery = params.Encode()
		body = bytes.NewReader(nil)
	} else if method == "POST" {
		body = bytes.NewReader([]byte(params.Encode()))
	} else {
		return errors.New("Unsupported method")
	}

	req, err := http.NewRequest(method, u.String(), body)
	if err != nil {
		return err
	}
	if c.api_key_id != "" {
		req.SetBasicAuth(c.api_key_id, c.api_key_secret)
	}
	req.Header.Add("User-Agent", userAgent)
	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	r, err := (&http.Client{}).Do(req)
	if err != nil {
		return err
	}
	defer r.Body.Close()

	if r.StatusCode != http.StatusOK {
		body, _ := ioutil.ReadAll(r.Body)
		return errors.New(fmt.Sprintf(
			"BitX error %d: %s: %s",
			r.StatusCode, r.Status, string(body)))
	}

	if err := json.NewDecoder(r.Body).Decode(result); err != nil {
		return err
	}

	return nil
}

type ticker struct {
	Error     string `json:"error"`
	Timestamp int64  `json:"timestamp"`
	Bid       string `json:"bid"`
	Ask       string `json:"ask"`
	Last      string `json:"last_trade"`
	Volume24H string `json:"rolling_24_hour_volume"`
}

type Ticker struct {
	Timestamp                 time.Time
	Bid, Ask, Last, Volume24H float64
}

// Returns the latest ticker indicators for the given currency pair..
func (c *Client) Ticker(pair string) (Ticker, error) {
	var r ticker
	err := c.call("GET", "/api/1/ticker", url.Values{"pair": {pair}}, &r)
	if err != nil {
		return Ticker{}, err
	}
	if r.Error != "" {
		return Ticker{}, errors.New("BitX error: " + r.Error)
	}

	t := time.Unix(r.Timestamp/1000, 0)

	bid, err := strconv.ParseFloat(r.Bid, 64)
	if err != nil {
		return Ticker{}, err
	}

	ask, err := strconv.ParseFloat(r.Ask, 64)
	if err != nil {
		return Ticker{}, err
	}

	last, err := strconv.ParseFloat(r.Last, 64)
	if err != nil {
		return Ticker{}, err
	}

	volume24h, err := strconv.ParseFloat(r.Volume24H, 64)
	if err != nil {
		return Ticker{}, err
	}

	return Ticker{t, bid, ask, last, volume24h}, nil
}

type orderbookEntry struct {
	Price  string `json:"price"`
	Volume string `json:"volume"`
}

type orderbook struct {
	Error string           `json:"error"`
	Asks  []orderbookEntry `json:"asks"`
	Bids  []orderbookEntry `json:"bids"`
}

type OrderBookEntry struct {
	Price, Volume float64
}
{'fraction_non_alphanumeric': 0.1511400651465798, 'fraction_numerical': 0.011074918566775244, 'mean_word_length': 3.677075399847677, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '2151276', 'n_tokens_mistral': 2359, 'n_tokens_neox': 2247, 'n_words': 618}