TreasureDataにaddRecordする

# TreasureDataにaddRecordする

TreasureData にデータ投げたいと思って調べてみたら
[Application SDKs](http://docs.treasuredata.com/categories/app-sdks) は `java` `ruby` `c/c++` しか無いみたいなので、
仕方ないので [td-js-sdk](https://github.com/treasure-data/td-js-sdk) の中身読んでみたらたいしたことやってなかったので、
`addRecord` 相当のとこだけ `golang`で書いた。

ちなみに `td-js-sdk` だとjsonpで叩いてた。

```go
package main

import (
	"encoding/base64"
	"fmt"
	"io/ioutil"
	"net/http"

	"github.com/google/go-querystring/query"
)

type TdAddRecordParams struct {
	ApiKey string `url:"api_key"`
	Data   string `url:"data"`
}

var DATABASE = "some_database"
var TABLE = "some_table"
var ENDPOINT = fmt.Sprintf("http://in.treasuredata.com/js/v3/event/%s/%s", DATABASE, TABLE)
var APIKEY = "YOUR API KEY HERE"

func main() {
	var record = []byte(`
{
  "Name": "Alice",
  "Body": "Hello",
  "Time": 1294706395881547000
}
`)

	err := func(data []byte) error {
		v, _ := query.Values(TdAddRecordParams{
			ApiKey: APIKEY,
			Data:   base64.StdEncoding.EncodeToString(data),
		})

		req, err := http.NewRequest("GET", ENDPOINT, nil)
		if err != nil {
			return err
		}
		req.URL.RawQuery = v.Encode()
		fmt.Println(req.URL.String())

		client := &http.Client{}
		resp, err := client.Do(req)
		if err != nil {
			return err
		}

		defer resp.Body.Close()
		body, err := ioutil.ReadAll(resp.Body)
		fmt.Println(string(body), err)

		return err
	}(record)
	fmt.Println(err)
}
```

## 追記＠整理したやつ

```go:tdclient/addrecord.go
package tdclient

import (
	"encoding/base64"
	"fmt"
	"net/http"

	"github.com/google/go-querystring/query"
)

var endpoint_format = "http://in.treasuredata.com/js/v3/event/%s/%s"

type Client struct {
	ApiKey   string
	Endpoint string
}

func New(api_key string, database string, table string) *Client {
	return &Client{
		ApiKey:   api_key,
		Endpoint: fmt.Sprintf(endpoint_format, database, table),
	}
}

func (self Client) AddRecord(data []byte) error {
	type Params struct {
		ApiKey string `url:"api_key"`
		Data   string `url:"data"`
	}

	v, _ := query.Values(Params{
		ApiKey: self.ApiKey,
		Data:   base64.StdEncoding.EncodeToString(data),
	})

	req, err := http.NewRequest("GET", self.Endpoint, nil)
	if err != nil {
		return err
	}
	req.URL.RawQuery = v.Encode()

	client := &http.Client{}
	_, err = client.Do(req)
	if err != nil {
		return err
	}

	return nil
}
```

```go:main.go
package main

import (
	"./tdclient"
	"fmt"
)

var DATABASE = "some_database"
var TABLE = "some_table"
var APIKEY = "YOUR API KEY HERE"

func main() {
	var record = []byte(`
{
  "Name": "Alice",
  "Body": "Hello",
  "Time": 1294706395881547000
}
`)

	cli := tdclient.New(APIKEY, DATABASE, TABLE)
	err := cli.AddRecord(record)
	fmt.Println(err)
}
```

## bashだとこう

`jq` と `python` 使ってるじゃんとか言わない。

```
#!/bin/bash

function urlencode() {
    python -c "import sys, urllib; sys.stdout.write(urllib.quote(sys.stdin.read()))"
}

PARAMS=$(cat <<EOD | jq -c . | base64 -w0
{
  "Name": "Alice",
  "Body": "Hello",
  "Time": 1294706395881547000
}
EOD
)

DATABASE=some_database
TABLE=some_table
ENDPOINT=http://in.treasuredata.com/js/v3/event/$DATABASE/$TABLE
APIKEY="YOUR API KEY HERE"

curl -s -v "$ENDPOINT?api_key=$(echo "$APIKEY" | urlencode)&data=$(echo "$PARAMS" | urlencode)"
```