-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscoretrak.go
38 lines (35 loc) · 1.11 KB
/
scoretrak.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main
import (
"context"
"encoding/json"
"github.com/ScoreTrak/ScoreTrak/pkg/report"
authv1 "go.buf.build/grpc/go/scoretrak/scoretrakapis/scoretrak/auth/v1"
reportv1 "go.buf.build/grpc/go/scoretrak/scoretrakapis/scoretrak/report/v1"
"google.golang.org/grpc/metadata"
"log"
)
func getAuth(acli authv1.AuthServiceClient) (string, error) {
resp, err := acli.Login(context.Background(), &authv1.LoginRequest{Password: conf.ScoreTrakPassword, Username: conf.ScoreTrakUsername})
if err != nil {
return "", err
}
return resp.AccessToken, nil
}
func getReport(repCli reportv1.ReportServiceClient, token string) (*report.SimpleReport, error) {
ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs("authorization", token))
resp, err := repCli.Get(ctx, &reportv1.GetRequest{})
if err != nil {
return nil, err
}
latestReport := new(report.SimpleReport)
res, err := resp.Recv()
if err != nil {
log.Fatal("stream was closed: ", err)
}
defer resp.Context().Done()
err = json.Unmarshal([]byte(res.Report.Cache), latestReport)
if err != nil {
return nil, err
}
return latestReport, nil
}