fix: api change needs account-id header
thanks https://gist.github.com/khskekec/6c13ba01b10d3018d816706a32ae8ab2
This commit is contained in:
parent
cddfc942bb
commit
4ba50c7e82
38
api/api.go
38
api/api.go
@ -3,6 +3,8 @@ package api
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@ -26,7 +28,7 @@ func request(
|
|||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
method string,
|
method string,
|
||||||
url string,
|
url string,
|
||||||
token string,
|
token, accountID string,
|
||||||
data []byte,
|
data []byte,
|
||||||
) (*http.Response, error) {
|
) (*http.Response, error) {
|
||||||
req, err := http.NewRequest(method, url, bytes.NewReader(data))
|
req, err := http.NewRequest(method, url, bytes.NewReader(data))
|
||||||
@ -34,14 +36,22 @@ func request(
|
|||||||
return &http.Response{}, fmt.Errorf("creating request: %w", err)
|
return &http.Response{}, fmt.Errorf("creating request: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Add("cache-control", "no-cache")
|
req.Header.Add("Cache-Control", "no-cache")
|
||||||
req.Header.Add("connection", "Keep-Alive")
|
req.Header.Add("Connection", "Keep-Alive")
|
||||||
req.Header.Add("content-type", "application/json")
|
req.Header.Add("Content-Type", "application/json")
|
||||||
req.Header.Add("product", "llu.android")
|
req.Header.Add("Product", "llu.android")
|
||||||
req.Header.Add("version", "4.7")
|
req.Header.Add("Version", "4.12.0")
|
||||||
|
|
||||||
if token != "" {
|
if token != "" {
|
||||||
req.Header.Add("authorization", "Bearer "+token)
|
req.Header.Add("Authorization", "Bearer "+token)
|
||||||
|
}
|
||||||
|
|
||||||
|
if accountID != "" {
|
||||||
|
h := sha256.New()
|
||||||
|
|
||||||
|
h.Write([]byte(accountID))
|
||||||
|
|
||||||
|
req.Header.Add("Account-Id", hex.EncodeToString(h.Sum(nil)))
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := client.Do(req.WithContext(ctx))
|
resp, err := client.Do(req.WithContext(ctx))
|
||||||
@ -57,9 +67,14 @@ type Ticket struct {
|
|||||||
Expires epoch.Epoch `json:"expires"`
|
Expires epoch.Epoch `json:"expires"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
}
|
||||||
|
|
||||||
type LoginResponse struct {
|
type LoginResponse struct {
|
||||||
Data struct {
|
Data struct {
|
||||||
AuthTicket Ticket `json:"authTicket"`
|
AuthTicket Ticket `json:"authTicket"`
|
||||||
|
User User `json:"user"`
|
||||||
} `json:"data"`
|
} `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,7 +94,7 @@ func Login(ctx context.Context, baseURL, username, password string) (LoginRespon
|
|||||||
return LoginResponse{}, fmt.Errorf("joining url: %w", err)
|
return LoginResponse{}, fmt.Errorf("joining url: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := request(ctx, http.MethodPost, url, "", d)
|
resp, err := request(ctx, http.MethodPost, url, "", "", d)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return LoginResponse{}, fmt.Errorf("doing request: %w", err)
|
return LoginResponse{}, fmt.Errorf("doing request: %w", err)
|
||||||
}
|
}
|
||||||
@ -116,13 +131,16 @@ type ConnectionsResponse struct {
|
|||||||
Ticket Ticket `json:"ticket"`
|
Ticket Ticket `json:"ticket"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func Connections(ctx context.Context, baseURL, token string) (ConnectionsResponse, error) {
|
func Connections(
|
||||||
|
ctx context.Context,
|
||||||
|
baseURL, token, accountID string,
|
||||||
|
) (ConnectionsResponse, error) {
|
||||||
url, err := url.JoinPath(baseURL, "/connections")
|
url, err := url.JoinPath(baseURL, "/connections")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ConnectionsResponse{}, fmt.Errorf("joining url: %w", err)
|
return ConnectionsResponse{}, fmt.Errorf("joining url: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := request(ctx, http.MethodGet, url, token, []byte{})
|
resp, err := request(ctx, http.MethodGet, url, token, accountID, []byte{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ConnectionsResponse{}, fmt.Errorf("doing request: %w", err)
|
return ConnectionsResponse{}, fmt.Errorf("doing request: %w", err)
|
||||||
}
|
}
|
||||||
|
1
internal/cache/cache.go
vendored
1
internal/cache/cache.go
vendored
@ -18,6 +18,7 @@ type Cache struct {
|
|||||||
JWT string `json:"jwt,omitempty"`
|
JWT string `json:"jwt,omitempty"`
|
||||||
Expires epoch.Epoch `json:"expires,omitempty"`
|
Expires epoch.Epoch `json:"expires,omitempty"`
|
||||||
BaseURL string `json:"base_url,omitempty"`
|
BaseURL string `json:"base_url,omitempty"`
|
||||||
|
AccountID string `json:"account_id,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func FullPath() string {
|
func FullPath() string {
|
||||||
|
@ -48,7 +48,7 @@ func glucose(ctx context.Context, w io.Writer) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Getting connections, which includes the glucose data.
|
// Getting connections, which includes the glucose data.
|
||||||
resp, err := api.Connections(ctx, api.BaseURL, c.JWT)
|
resp, err := api.Connections(ctx, api.BaseURL, c.JWT, c.AccountID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("connections: %w", err)
|
return fmt.Errorf("connections: %w", err)
|
||||||
}
|
}
|
||||||
@ -106,7 +106,7 @@ func token(ctx context.Context) error {
|
|||||||
slog.Debug("got token", "token", token)
|
slog.Debug("got token", "token", token)
|
||||||
|
|
||||||
if err := cache.Save(
|
if err := cache.Save(
|
||||||
cache.Cache{JWT: token.Data.AuthTicket.Token, Expires: token.Data.AuthTicket.Expires},
|
cache.Cache{JWT: token.Data.AuthTicket.Token, Expires: token.Data.AuthTicket.Expires, AccountID: token.Data.User.ID},
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return fmt.Errorf("saving cache: %w", err)
|
return fmt.Errorf("saving cache: %w", err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user