mirror of
https://github.com/sstent/go-garminconnect.git
synced 2026-02-07 15:01:38 +00:00
sync
This commit is contained in:
66
cmd/garmin-cli/main.go
Normal file
66
cmd/garmin-cli/main.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/sstent/go-garminconnect/internal/api"
|
||||
"github.com/sstent/go-garminconnect/internal/auth"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Verify required environment variables
|
||||
if os.Getenv("GARMIN_USERNAME") == "" || os.Getenv("GARMIN_PASSWORD") == "" {
|
||||
fmt.Println("GARMIN_USERNAME and GARMIN_PASSWORD must be set")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Set up authentication client
|
||||
client := auth.NewAuthClient()
|
||||
token, err := client.Authenticate(
|
||||
context.Background(),
|
||||
os.Getenv("GARMIN_USERNAME"),
|
||||
os.Getenv("GARMIN_PASSWORD"),
|
||||
"", // MFA token if needed
|
||||
)
|
||||
if err != nil {
|
||||
fmt.Printf("Authentication failed: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Create API client
|
||||
apiClient, err := api.NewClient(token.AccessToken)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to create API client: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Parse date range (default: last 7 days)
|
||||
endDate := time.Now()
|
||||
startDate := endDate.AddDate(0, 0, -7)
|
||||
|
||||
// Get body composition data
|
||||
composition, err := apiClient.GetBodyComposition(context.Background(), api.BodyCompositionRequest{
|
||||
StartDate: api.Time(startDate),
|
||||
EndDate: api.Time(endDate),
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to get body composition: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Print results
|
||||
fmt.Println("Body Composition Data:")
|
||||
fmt.Println("Date\t\tBone Mass\tMuscle Mass\tBody Fat\tHydration")
|
||||
for _, entry := range composition {
|
||||
fmt.Printf("%s\t%.1fg\t\t%.1fg\t\t%.1f%%\t\t%.1f%%\n",
|
||||
time.Time(entry.Timestamp).Format("2006-01-02"),
|
||||
entry.BoneMass,
|
||||
entry.MuscleMass,
|
||||
entry.BodyFat,
|
||||
entry.Hydration,
|
||||
)
|
||||
}
|
||||
}
|
||||
58
cmd/main.go
58
cmd/main.go
@@ -1,38 +1,50 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/sstent/go-garminconnect/internal/auth"
|
||||
"github.com/sstent/go-garminconnect/internal/api"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Get consumer key and secret from environment
|
||||
consumerKey := os.Getenv("GARMIN_CONSUMER_KEY")
|
||||
consumerSecret := os.Getenv("GARMIN_CONSUMER_SECRET")
|
||||
if consumerKey == "" || consumerSecret == "" {
|
||||
fmt.Println("GARMIN_CONSUMER_KEY and GARMIN_CONSUMER_SECRET must be set")
|
||||
// Get credentials from environment
|
||||
username := os.Getenv("GARMIN_USERNAME")
|
||||
password := os.Getenv("GARMIN_PASSWORD")
|
||||
if username == "" || password == "" {
|
||||
fmt.Println("GARMIN_USERNAME and GARMIN_PASSWORD must be set")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Configure authentication
|
||||
oauthConfig := &auth.OAuthConfig{
|
||||
ConsumerKey: consumerKey,
|
||||
ConsumerSecret: consumerSecret,
|
||||
// Create authentication client
|
||||
authClient := auth.NewAuthClient()
|
||||
|
||||
// Authenticate with credentials
|
||||
token, err := authClient.Authenticate(context.Background(), username, password, "")
|
||||
if err != nil {
|
||||
fmt.Printf("Authentication failed: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Set up token storage
|
||||
tokenStorage := auth.NewFileStorage()
|
||||
// API client not currently used in this simple server
|
||||
// It's created here for demonstration purposes only
|
||||
_, err = api.NewClient(token.AccessToken)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to create API client: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Create HTTP server
|
||||
http.HandleFunc("/", homeHandler)
|
||||
http.HandleFunc("/login", loginHandler(oauthConfig, tokenStorage))
|
||||
http.HandleFunc("/callback", callbackHandler(oauthConfig, tokenStorage))
|
||||
http.HandleFunc("/mfa", auth.MFAHandler)
|
||||
http.HandleFunc("/health", healthHandler)
|
||||
|
||||
// For demonstration purposes, print API client status
|
||||
// This line was removed because baseURL is unexported
|
||||
// fmt.Printf("API client initialized for %s\n", apiClient.baseURL)
|
||||
|
||||
fmt.Println("Server listening on :8080")
|
||||
http.ListenAndServe(":8080", nil)
|
||||
}
|
||||
@@ -42,27 +54,13 @@ func homeHandler(w http.ResponseWriter, r *http.Request) {
|
||||
<html>
|
||||
<body>
|
||||
<h1>Go GarminConnect Client</h1>
|
||||
<a href="/login">Login with Garmin</a>
|
||||
<p>Authentication successful! API client ready.</p>
|
||||
</body>
|
||||
</html>
|
||||
`))
|
||||
}
|
||||
|
||||
func loginHandler(config *auth.OAuthConfig, storage auth.TokenStorage) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
auth.Authenticate(w, r, config, storage)
|
||||
}
|
||||
}
|
||||
|
||||
func callbackHandler(config *auth.OAuthConfig, storage auth.TokenStorage) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
// In a real app, we'd retrieve the request secret from session storage
|
||||
// For now, we'll use a placeholder
|
||||
requestSecret := "placeholder-secret"
|
||||
|
||||
auth.Callback(w, r, config, storage, requestSecret)
|
||||
}
|
||||
}
|
||||
// Removed OAuth handlers since we're using credentials-based auth
|
||||
|
||||
func healthHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
Reference in New Issue
Block a user