mirror of
https://github.com/sstent/go-garth.git
synced 2026-01-26 09:03:00 +00:00
feat(refactor): Implement 1A.1 Package Structure Refactoring
This commit implements the package structure refactoring as outlined in phase1.md (Task 1A.1). Key changes include: - Reorganized packages into `pkg/garmin` for public API and `internal/` for internal implementations. - Updated all import paths to reflect the new structure. - Consolidated types and client logic into their respective new packages. - Updated `cmd/garth/main.go` to use the new public API. - Fixed various compilation and test issues encountered during the refactoring process. - Converted `internal/api/client/auth_test.go` to a functional test. This establishes a solid foundation for future enhancements and improves maintainability.
This commit is contained in:
81
internal/data/weight.go
Normal file
81
internal/data/weight.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"garmin-connect/internal/api/client"
|
||||
)
|
||||
|
||||
// WeightData represents weight measurement data
|
||||
type WeightData struct {
|
||||
UserProfilePK int `json:"userProfilePk"`
|
||||
CalendarDate time.Time `json:"calendarDate"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
Weight float64 `json:"weight"` // in kilograms
|
||||
BMI float64 `json:"bmi"`
|
||||
BodyFatPercentage float64 `json:"bodyFatPercentage"`
|
||||
BoneMass float64 `json:"boneMass"` // in kg
|
||||
MuscleMass float64 `json:"muscleMass"` // in kg
|
||||
Hydration float64 `json:"hydration"` // in kg
|
||||
BaseData
|
||||
}
|
||||
|
||||
// Validate checks if weight data contains valid values
|
||||
func (w *WeightData) Validate() error {
|
||||
if w.Weight <= 0 {
|
||||
return fmt.Errorf("invalid weight value")
|
||||
}
|
||||
if w.BMI < 10 || w.BMI > 50 {
|
||||
return fmt.Errorf("BMI out of valid range")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get implements the Data interface for WeightData
|
||||
func (w *WeightData) Get(day time.Time, client *client.Client) (any, error) {
|
||||
startDate := day.Format("2006-01-02")
|
||||
endDate := day.Format("2006-01-02")
|
||||
path := fmt.Sprintf("/weight-service/weight/dateRange?startDate=%s&endDate=%s",
|
||||
startDate, endDate)
|
||||
|
||||
data, err := client.ConnectAPI(path, "GET", nil, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(data) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var response struct {
|
||||
WeightList []WeightData `json:"weightList"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &response); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(response.WeightList) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
weightData := response.WeightList[0]
|
||||
// Convert grams to kilograms
|
||||
weightData.Weight = weightData.Weight / 1000
|
||||
weightData.BoneMass = weightData.BoneMass / 1000
|
||||
weightData.MuscleMass = weightData.MuscleMass / 1000
|
||||
weightData.Hydration = weightData.Hydration / 1000
|
||||
|
||||
return weightData, nil
|
||||
}
|
||||
|
||||
// List implements the Data interface for concurrent fetching
|
||||
func (w *WeightData) List(end time.Time, days int, client *client.Client, maxWorkers int) ([]any, error) {
|
||||
results, errs := w.BaseData.List(end, days, client, maxWorkers)
|
||||
if len(errs) > 0 {
|
||||
// Return first error for now
|
||||
return results, errs[0]
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
Reference in New Issue
Block a user