Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c3d7a38
Showing
7 changed files
with
230 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
secrets.json | ||
dev.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
client "github.com/highstead/catwalk-client" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
func main() { | ||
log.SetLevel(log.DebugLevel) | ||
svc := client.NewCatwalkClient() | ||
|
||
result, err := svc.GetModel("sales_rollup_v7") | ||
if err == nil { | ||
fmt.Println("**Result**\n", result) | ||
return | ||
} | ||
log.WithError(err).Errorln("failed to make request") | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"_public_key": "484fda0a8e33694f62eb26cbffd0758dfc7e75b3e793f0045876fdc7ba535c47" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module github.com/highstead/catwalk-client | ||
|
||
go 1.13 | ||
|
||
require ( | ||
github.com/pkg/errors v0.8.1 | ||
github.com/sirupsen/logrus v1.4.2 | ||
golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
cloud.google.com/go v0.34.0 h1:eOI3/cP2VTU6uZLDYAoic+eyzzB9YyGmJ7eIjl8rOPg= | ||
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= | ||
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/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= | ||
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= | ||
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= | ||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= | ||
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/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4= | ||
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= | ||
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= | ||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= | ||
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= | ||
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e h1:bRhVy7zSSasaqNksaRZiA5EEI+Ei4I1nO5Jh72wfHlg= | ||
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= | ||
golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6 h1:pE8b58s1HRDMi8RDc79m0HISf9D4TzseP40cEA6IGfs= | ||
golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= | ||
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | ||
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc= | ||
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= | ||
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package client | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
type Secrets struct { | ||
} | ||
|
||
var ( | ||
tsEndpoint = "" | ||
authEndpoint = "" | ||
secretToken = "" | ||
) | ||
|
||
func init() { | ||
tsEndpoint = os.Getenv("TS_ENDPOINT") | ||
authEndpoint = os.Getenv("AUTH_ENDPOINT") | ||
secretToken = os.Getenv("SECRET_TOKEN") | ||
} | ||
|
||
func ParseSecretsFile(installDir string) (*Secrets, error) { | ||
path := filepath.Join(installDir, "config", "secrets.json") | ||
data, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to read file: %s", path) | ||
} | ||
|
||
return ParseSecrets(path, data) | ||
} | ||
|
||
func ParseSecrets(path string, data []byte) (*Secrets, error) { | ||
s := &Secrets{} | ||
if err := json.Unmarshal(data, s); err != nil { | ||
return nil, errors.Wrapf(err, "failed to unmarshal json, path: %s", path) | ||
} | ||
|
||
return s, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package client | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type Service struct { | ||
secretToken string | ||
bearerToken *BearerToken | ||
} | ||
|
||
func NewCatwalkClient() *Service { | ||
log.WithField("secretToken", secretToken).Debugln("starting catwalk client") | ||
return &Service{secretToken: secretToken, bearerToken: &BearerToken{}} | ||
} | ||
|
||
func (s *Service) GetModel(modelName string) (string, error) { | ||
return s.GetModels([]string{modelName}) | ||
} | ||
|
||
func (s *Service) GetModels(models []string) (string, error) { | ||
token, err := s.GetBearerToken() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
req, err := NewCatwalkTimeseriesRequest(models) | ||
if err != nil { | ||
return "", err | ||
} | ||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token)) | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return "", errors.Wrap(err, "failed to get model data") | ||
} | ||
|
||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return "", errors.Wrap(err, "cant parse body of timeseries") | ||
} | ||
|
||
return string(body), nil | ||
} | ||
|
||
func (s *Service) UpdateBearerToken() error { | ||
req, err := http.NewRequest("GET", authEndpoint, nil) | ||
if err != nil { | ||
return errors.Wrap(err, "unable to parse url") | ||
} | ||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", s.secretToken)) | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return errors.Wrap(err, "unable to update bearer token") | ||
} | ||
|
||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return errors.Wrap(err, "cant parse body of bearer token requset") | ||
} | ||
bt := &BearerToken{} | ||
err = json.Unmarshal(body, bt) | ||
if err != nil { | ||
log.WithError(err).Debug(string(body)) | ||
log.WithFields(log.Fields{ | ||
"request": req, | ||
"resp": resp, | ||
}).Debug("request object") | ||
return errors.Wrap(err, "failed to unmarshal bearer token") | ||
} | ||
s.bearerToken = bt | ||
return nil | ||
} | ||
|
||
func (s *Service) GetBearerToken() (string, error) { | ||
if !s.bearerToken.Valid() { | ||
if err := s.UpdateBearerToken(); err != nil { | ||
return "", err | ||
} | ||
} | ||
return s.bearerToken.AccessToken, nil | ||
} | ||
|
||
type BearerToken struct { | ||
AccessToken string `json:"access_token"` | ||
ExpiresAt int64 `json:"expires_at"` | ||
} | ||
|
||
func (b *BearerToken) Valid() bool { | ||
if b.AccessToken == "" { | ||
return false | ||
} | ||
if time.Unix(b.ExpiresAt, 0).Before(time.Now()) { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
type CatwalkTimeseriesPayload struct { | ||
Grain string `json:"grain"` | ||
Model []string `json:"model"` | ||
Id int `json:"id"` | ||
Tz string `json:"tz"` | ||
} | ||
|
||
func NewCatwalkTimeseriesRequest(model []string) (*http.Request, error) { | ||
payload := &CatwalkTimeseriesPayload{ | ||
Grain: "daily", | ||
Model: model, | ||
Id: 2085268, | ||
Tz: time.UTC.String(), | ||
} | ||
url := fmt.Sprintf("%s?id=%d&tz=%s&grain=%s", tsEndpoint, payload.Id, payload.Tz, payload.Grain) | ||
for _, v := range model { | ||
url = url + "&model=" + v | ||
} | ||
return http.NewRequest("GET", url, nil) | ||
} |