LoginSignup
0
0

More than 5 years have passed since last update.

Domo のデータセットのデータをイクスポートする (go)

Last updated at Posted at 2018-02-25

こちらのプログラムを go で書きました。
Python で Domo のデータセットのデータをイクスポートする

export_dataset.go
// ----------------------------------------------------------------
/*
    export_dataset.go

                    Feb/26/2018
*/
// ----------------------------------------------------------------
package main
import (
    "fmt"
    "os"
    "log"
    "io/ioutil"
    "net/http"
)

// ----------------------------------------------------------------
func export_dataset_proc (access_token string,dataset_id string,file_out string) {
    fmt.Fprintf (os.Stderr,"*** export_dataset_proc ***\n")

    url_datasets :="https://api.domo.com/v1/datasets/" + dataset_id + "/data"
    req, _ := http.NewRequest("GET", url_datasets, nil)
    req.Header.Set("Authorization", "Bearer " +  access_token)
    req.Header.Set("Content-Type", "text/csv")

    client := new(http.Client)
    resp, err := client.Do(req)

    if err != nil {
        log.Fatalln(err)
        fmt.Printf("%s", err)
        os.Exit(1) 
    } else {
        defer resp.Body.Close()
        contents, err := ioutil.ReadAll(resp.Body)
        if err != nil {
            fmt.Printf("%s", err)
            os.Exit(1)
            }

        str_csv := string(contents)
        fmt.Printf("%s\n", str_csv)
        ioutil.WriteFile (file_out,[]byte(str_csv),0666)
        }
}

// ----------------------------------------------------------------
func main() {
    fmt.Fprintf (os.Stderr,"*** 開始 ***\n")
    dataset_id := os.Args[1]
    fmt.Printf ("%s\n",dataset_id)
    file_out := os.Args[2]
    fmt.Printf ("%s\n",file_out)

    access_token := get_token_proc ()

    fmt.Fprintf(os.Stderr,"%s\n", access_token)

    export_dataset_proc (access_token,dataset_id,file_out)

    fmt.Fprintf (os.Stderr,"*** 終了 ***\n")
}

// ----------------------------------------------------------------

get_token.go はこちらです。
Domo の アクセストークンを取得する (go)

Makefile
export_dataset: export_dataset.go get_token.go
    go build export_dataset.go get_token.go
clean:
    rm -f export_dataset
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0