Skip to content

Commit

Permalink
Merge pull request #240 from winebarrel/add_example
Browse files Browse the repository at this point in the history
Add example code
  • Loading branch information
winebarrel authored Jan 25, 2025
2 parents eee5dc9 + 4fda719 commit 65644e9
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 2 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,3 @@

# Dependency directories (remove the comment below to include it)
# vendor/

test.go
9 changes: 9 additions & 0 deletions example/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module example

go 1.23.4

replace github.com/winebarrel/redash-go/v2 => ../

require github.com/winebarrel/redash-go/v2 v2.0.0-00010101000000-000000000000

require github.com/google/go-querystring v1.1.0 // indirect
19 changes: 19 additions & 0 deletions example/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de h1:FxWPpzIjnTlhPwqqXc4/vE0f7GvRjuAsbW+HOIe8KnA=
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de/go.mod h1:DCaWoUhZrYW9p1lxo/cm8EmUOOzAPSEZNGF2DK1dJgw=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jarcoal/httpmock v1.3.1 h1:iUx3whfZWVf3jT01hQTO/Eo5sAYtB2/rqaUuOtpInww=
github.com/jarcoal/httpmock v1.3.1/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
59 changes: 59 additions & 0 deletions example/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main

import (
"bytes"
"context"
"fmt"

"github.com/winebarrel/redash-go/v2"
)

const (
testRedashEndpoint = "http://localhost:5001"
testRedashAPIKey = "6nh64ZsT66WeVJvNZ6WB5D2JKZULeC2VBdSD68wt"
)

func main() {
client := redash.MustNewClient(testRedashEndpoint, testRedashAPIKey)
ctx := context.Background()

ds, err := client.CreateDataSource(ctx, &redash.CreateDataSourceInput{
Name: "postgres",
Type: "pg",
Options: map[string]any{
"dbname": "postgres",
"host": "postgres",
"port": 5432,
"user": "postgres",
},
})

if err != nil {
panic(err)
}

query, err := client.CreateQuery(ctx, &redash.CreateQueryInput{
DataSourceID: ds.ID,
Name: "my-query1",
Query: "select 1",
})

if err != nil {
panic(err)
}

var buf bytes.Buffer
job, err := client.ExecQueryJSON(ctx, query.ID, nil, &buf)

if err != nil {
panic(err)
}

err = client.WaitQueryJSON(ctx, query.ID, job, nil, &buf)

if err != nil {
panic(err)
}

fmt.Println(buf.String())
}

0 comments on commit 65644e9

Please sign in to comment.