This commit is contained in:
2025-09-03 08:29:15 -07:00
parent 56f55daa85
commit 2898ccdb79
164 changed files with 275 additions and 25235 deletions

View File

@@ -0,0 +1,80 @@
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/joho/godotenv"
"github.com/sstent/go-garth"
)
func main() {
// Load environment variables from .env file
if err := godotenv.Load("../../../.env"); err != nil {
log.Println("Note: Using system environment variables (no .env file found)")
}
// Get credentials from environment
username := os.Getenv("GARMIN_USERNAME")
password := os.Getenv("GARMIN_PASSWORD")
if username == "" || password == "" {
log.Fatal("GARMIN_USERNAME or GARMIN_PASSWORD not set in environment")
}
// Create context with timeout
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
// Create token storage and authenticator
storage := garth.NewMemoryStorage()
auth := garth.NewAuthenticator(garth.ClientOptions{
Storage: storage,
TokenURL: "https://connectapi.garmin.com/oauth-service/oauth/token",
Timeout: 30 * time.Second,
})
// Authenticate
token, err := auth.Login(ctx, username, password, "")
if err != nil {
log.Fatalf("Authentication failed: %v", err)
}
log.Printf("Authenticated successfully! Token expires at: %s", token.Expiry.Format(time.RFC3339))
// Create HTTP client with authentication transport
httpClient := &http.Client{
Transport: garth.NewAuthTransport(auth.(*garth.GarthAuthenticator), storage, nil),
}
// Create API client
apiClient := garth.NewAPIClient("https://connectapi.garmin.com", httpClient)
// Create activity service
activityService := garth.NewActivityService(apiClient)
// List last 20 activities
activities, err := activityService.List(ctx, garth.ActivityListOptions{
Limit: 20,
})
if err != nil {
log.Fatalf("Failed to get activities: %v", err)
}
// Print activities
fmt.Println("\nLast 20 Activities:")
fmt.Println("=======================================")
for i, activity := range activities {
fmt.Printf("%d. %s [%s] - %s\n", i+1,
activity.Name,
activity.Type,
activity.StartTime.Format("2006-01-02 15:04"))
fmt.Printf(" Distance: %.2f km, Duration: %.0f min\n\n",
activity.Distance/1000,
activity.Duration/60)
}
fmt.Println("Example completed successfully!")
}

View File

@@ -0,0 +1,8 @@
module github.com/sstent/go-garth/examples/activities
go 1.22
require (
github.com/joho/godotenv v1.5.1
github.com/sstent/go-garth v0.1.0
)

View File

@@ -0,0 +1,80 @@
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/joho/godotenv"
"github.com/sstent/go-garth"
)
func main() {
// Load environment variables from .env file
if err := godotenv.Load("../../.env"); err != nil {
log.Println("Note: Using system environment variables (no .env file found)")
}
// Get credentials from environment
username := os.Getenv("GARMIN_USERNAME")
password := os.Getenv("GARMIN_PASSWORD")
if username == "" || password == "" {
log.Fatal("GARMIN_USERNAME or GARMIN_PASSWORD not set in environment")
}
// Create context with timeout
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
// Create token storage and authenticator
storage := garth.NewMemoryStorage()
auth := garth.NewAuthenticator(garth.ClientOptions{
Storage: storage,
TokenURL: "https://connectapi.garmin.com/oauth-service/oauth/token",
Timeout: 30 * time.Second,
})
// Authenticate
token, err := auth.Login(ctx, username, password, "")
if err != nil {
log.Fatalf("Authentication failed: %v", err)
}
log.Printf("Authenticated successfully! Token expires at: %s", token.Expiry.Format(time.RFC3339))
// Create HTTP client with authentication transport
httpClient := &http.Client{
Transport: garth.NewAuthTransport(auth.(*garth.GarthAuthenticator), storage, nil),
}
// Create API client
apiClient := garth.NewAPIClient("https://connectapi.garmin.com", httpClient)
// Create activity service
activityService := garth.NewActivityService(apiClient)
// List last 20 activities
activities, err := activityService.List(ctx, garth.ActivityListOptions{
Limit: 20,
})
if err != nil {
log.Fatalf("Failed to get activities: %v", err)
}
// Print activities
fmt.Println("\nLast 20 Activities:")
fmt.Println("=======================================")
for i, activity := range activities {
fmt.Printf("%d. %s [%s] - %s\n", i+1,
activity.Name,
activity.Type,
activity.StartTime.Format("2006-01-02 15:04"))
fmt.Printf(" Distance: %.2f km, Duration: %.0f min\n\n",
activity.Distance/1000,
activity.Duration/60)
}
fmt.Println("Example completed successfully!")
}

View File

@@ -9,8 +9,8 @@ import (
)
func main() {
// Create a new client
client := garth.New()
// Create a new client (placeholder - actual client creation depends on package structure)
// client := garth.NewClient(nil)
// For demonstration, we'll use a mock server or skip authentication
// In real usage, you would authenticate first:
@@ -42,12 +42,31 @@ func main() {
// List workouts with options
opts := garth.WorkoutListOptions{
Limit: 10,
Offset: 0,
StartDate: time.Now().AddDate(0, -1, 0), // Last month
EndDate: time.Now(),
SortBy: "createdDate",
SortOrder: "desc",
}
fmt.Printf("Workout list options: %+v\n", opts)
// List workouts with pagination
paginatedOpts := garth.WorkoutListOptions{
Limit: 5,
Offset: 10,
SortBy: "name",
}
fmt.Printf("Paginated workout options: %+v\n", paginatedOpts)
// List workouts with type and status filters
filteredOpts := garth.WorkoutListOptions{
Type: "running",
Status: "active",
Limit: 20,
}
fmt.Printf("Filtered workout options: %+v\n", filteredOpts)
// Get workout details
workoutID := "12345"
fmt.Printf("Would fetch workout details for ID: %s\n", workoutID)