reworked api interfaces

This commit is contained in:
2025-09-22 16:41:49 -07:00
parent f2256a9cfe
commit 1b3fb04dcd
44 changed files with 1356 additions and 207 deletions

7
internal/data/base.go Normal file
View File

@@ -0,0 +1,7 @@
package data
import shared "github.com/sstent/go-garth/shared/interfaces"
// Alias shared BaseData and Data into internal/data for backward compatibility with tests
type BaseData = shared.BaseData
type Data = shared.Data

View File

@@ -5,7 +5,8 @@ import (
"testing"
"time"
"github.com/sstent/go-garth/internal/api/client"
"github.com/sstent/go-garth/pkg/garth/client"
interfaces "github.com/sstent/go-garth/shared/interfaces"
"github.com/stretchr/testify/assert"
)
@@ -28,7 +29,7 @@ func (mc *MockClient) Get(endpoint string) (interface{}, error) {
func TestBaseData_List(t *testing.T) {
// Setup mock data type
mockData := &MockData{}
mockData.GetFunc = func(day time.Time, c *client.Client) (interface{}, error) {
mockData.GetFunc = func(day time.Time, c interfaces.APIClient) (interface{}, error) {
return "data for " + day.Format("2006-01-02"), nil
}
@@ -51,7 +52,7 @@ func TestBaseData_List(t *testing.T) {
func TestBaseData_List_ErrorHandling(t *testing.T) {
// Setup mock data type that returns error on specific date
mockData := &MockData{}
mockData.GetFunc = func(day time.Time, c *client.Client) (interface{}, error) {
mockData.GetFunc = func(day time.Time, c interfaces.APIClient) (interface{}, error) {
if day.Day() == 13 {
return nil, errors.New("bad luck day")
}

View File

@@ -6,7 +6,7 @@ import (
"sort"
"time"
types "github.com/sstent/go-garth/internal/models/types"
garth "github.com/sstent/go-garth/pkg/garth/types"
shared "github.com/sstent/go-garth/shared/interfaces"
)
@@ -19,38 +19,92 @@ type BodyBatteryReading struct {
}
// ParseBodyBatteryReadings converts body battery values array to structured readings
// Accepts mixed numeric types (int, int64, float64, json.Number) for robustness.
func ParseBodyBatteryReadings(valuesArray [][]any) []BodyBatteryReading {
readings := make([]BodyBatteryReading, 0)
readings := make([]BodyBatteryReading, 0, len(valuesArray))
toInt := func(v any) (int, bool) {
switch t := v.(type) {
case int:
return t, true
case int32:
return int(t), true
case int64:
return int(t), true
case float32:
return int(t), true
case float64:
return int(t), true
case json.Number:
i, err := t.Int64()
if err == nil {
return int(i), true
}
f, err := t.Float64()
if err == nil {
return int(f), true
}
return 0, false
default:
return 0, false
}
}
toFloat64 := func(v any) (float64, bool) {
switch t := v.(type) {
case float32:
return float64(t), true
case float64:
return t, true
case int:
return float64(t), true
case int32:
return float64(t), true
case int64:
return float64(t), true
case json.Number:
f, err := t.Float64()
if err == nil {
return f, true
}
return 0, false
default:
return 0, false
}
}
for _, values := range valuesArray {
if len(values) < 4 {
continue
}
timestamp, ok1 := values[0].(float64)
ts, ok1 := toInt(values[0])
status, ok2 := values[1].(string)
level, ok3 := values[2].(float64)
version, ok4 := values[3].(float64)
lvl, ok3 := toInt(values[2])
ver, ok4 := toFloat64(values[3])
if !ok1 || !ok2 || !ok3 || !ok4 {
continue
}
readings = append(readings, BodyBatteryReading{
Timestamp: int(timestamp),
Timestamp: ts,
Status: status,
Level: int(level),
Version: version,
Level: lvl,
Version: ver,
})
}
sort.Slice(readings, func(i, j int) bool {
return readings[i].Timestamp < readings[j].Timestamp
})
return readings
}
// BodyBatteryDataWithMethods embeds types.DetailedBodyBatteryData and adds methods
// BodyBatteryDataWithMethods embeds garth.DetailedBodyBatteryData and adds methods
type BodyBatteryDataWithMethods struct {
types.DetailedBodyBatteryData
garth.DetailedBodyBatteryData
}
func (d *BodyBatteryDataWithMethods) Get(day time.Time, c shared.APIClient) (interface{}, error) {
@@ -71,14 +125,14 @@ func (d *BodyBatteryDataWithMethods) Get(day time.Time, c shared.APIClient) (int
data2 = []byte("[]")
}
var result types.DetailedBodyBatteryData
var result garth.DetailedBodyBatteryData
if len(data1) > 0 {
if err := json.Unmarshal(data1, &result); err != nil {
return nil, fmt.Errorf("failed to parse Body Battery data: %w", err)
}
}
var events []types.BodyBatteryEvent
var events []garth.BodyBatteryEvent
if len(data2) > 0 {
if err := json.Unmarshal(data2, &events); err == nil {
result.Events = events
@@ -111,3 +165,48 @@ func (d *BodyBatteryDataWithMethods) GetDayChange() int {
return readings[len(readings)-1].Level - readings[0].Level
}
// Added for test compatibility and public API alignment
// DailyBodyBatteryStress wraps garth.DetailedBodyBatteryData and provides a Get method compatible with existing tests.
// See [type DailyBodyBatteryStress](internal/data/body_battery.go:0) and [func (*DailyBodyBatteryStress).Get](internal/data/body_battery.go:0)
type DailyBodyBatteryStress struct {
garth.DetailedBodyBatteryData
}
// Get retrieves Body Battery daily stress data and associated events for a given day.
// Mirrors logic in BodyBatteryDataWithMethods.Get to maintain a consistent behavior.
// Returns (*DailyBodyBatteryStress, nil) on success, (nil, nil) when no data available.
func (d *DailyBodyBatteryStress) Get(day time.Time, c shared.APIClient) (interface{}, error) {
dateStr := day.Format("2006-01-02")
// Get main Body Battery data
path1 := fmt.Sprintf("/wellness-service/wellness/dailyStress/%s", dateStr)
data1, err := c.ConnectAPI(path1, "GET", nil, nil)
if err != nil {
return nil, fmt.Errorf("failed to get Body Battery stress data: %w", err)
}
// Get Body Battery events
path2 := fmt.Sprintf("/wellness-service/wellness/bodyBattery/%s", dateStr)
data2, err := c.ConnectAPI(path2, "GET", nil, nil)
if err != nil {
// Events might not be available, continue without them
data2 = []byte("[]")
}
var result garth.DetailedBodyBatteryData
if len(data1) > 0 {
if err := json.Unmarshal(data1, &result); err != nil {
return nil, fmt.Errorf("failed to parse Body Battery data: %w", err)
}
}
var events []garth.BodyBatteryEvent
if len(data2) > 0 {
if err := json.Unmarshal(data2, &events); err == nil {
result.Events = events
}
}
return &DailyBodyBatteryStress{DetailedBodyBatteryData: result}, nil
}

View File

@@ -1,9 +1,9 @@
package data
import (
types "github.com/sstent/go-garth/internal/models/types"
"testing"
garth "github.com/sstent/go-garth/pkg/garth/types"
"github.com/stretchr/testify/assert"
)
@@ -53,7 +53,7 @@ func TestParseBodyBatteryReadings(t *testing.T) {
// Test for GetCurrentLevel and GetDayChange methods
func TestBodyBatteryDataWithMethods(t *testing.T) {
mockData := types.DetailedBodyBatteryData{
mockData := garth.DetailedBodyBatteryData{
BodyBatteryValuesArray: [][]interface{}{
{1000, "ACTIVE", 75, 1.0},
{2000, "ACTIVE", 70, 1.0},
@@ -72,7 +72,7 @@ func TestBodyBatteryDataWithMethods(t *testing.T) {
})
// Test with empty data
emptyData := types.DetailedBodyBatteryData{
emptyData := garth.DetailedBodyBatteryData{
BodyBatteryValuesArray: [][]interface{}{},
}
emptyBb := BodyBatteryDataWithMethods{DetailedBodyBatteryData: emptyData}
@@ -86,7 +86,7 @@ func TestBodyBatteryDataWithMethods(t *testing.T) {
})
// Test with single reading
singleReadingData := types.DetailedBodyBatteryData{
singleReadingData := garth.DetailedBodyBatteryData{
BodyBatteryValuesArray: [][]interface{}{
{1000, "ACTIVE", 80, 1.0},
},

View File

@@ -6,13 +6,13 @@ import (
"sort"
"time"
types "github.com/sstent/go-garth/internal/models/types"
garth "github.com/sstent/go-garth/pkg/garth/types"
shared "github.com/sstent/go-garth/shared/interfaces"
)
// DailyHRVDataWithMethods embeds types.DailyHRVData and adds methods
// DailyHRVDataWithMethods embeds garth.DailyHRVData and adds methods
type DailyHRVDataWithMethods struct {
types.DailyHRVData
garth.DailyHRVData
}
// Get implements the Data interface for DailyHRVData
@@ -31,8 +31,8 @@ func (h *DailyHRVDataWithMethods) Get(day time.Time, c shared.APIClient) (interf
}
var response struct {
HRVSummary types.DailyHRVData `json:"hrvSummary"`
HRVReadings []types.HRVReading `json:"hrvReadings"`
HRVSummary garth.DailyHRVData `json:"hrvSummary"`
HRVReadings []garth.HRVReading `json:"hrvReadings"`
}
if err := json.Unmarshal(data, &response); err != nil {
@@ -45,8 +45,8 @@ func (h *DailyHRVDataWithMethods) Get(day time.Time, c shared.APIClient) (interf
}
// ParseHRVReadings converts body battery values array to structured readings
func ParseHRVReadings(valuesArray [][]any) []types.HRVReading {
readings := make([]types.HRVReading, 0, len(valuesArray))
func ParseHRVReadings(valuesArray [][]any) []garth.HRVReading {
readings := make([]garth.HRVReading, 0, len(valuesArray))
for _, values := range valuesArray {
if len(values) < 6 {
continue
@@ -60,7 +60,7 @@ func ParseHRVReadings(valuesArray [][]any) []types.HRVReading {
status, _ := values[4].(string)
signalQuality, _ := values[5].(float64)
readings = append(readings, types.HRVReading{
readings = append(readings, garth.HRVReading{
Timestamp: timestamp,
StressLevel: stressLevel,
HeartRate: heartRate,

View File

@@ -5,17 +5,17 @@ import (
"fmt"
"time"
garth "github.com/sstent/go-garth/pkg/garth/types"
shared "github.com/sstent/go-garth/shared/interfaces"
types "github.com/sstent/go-garth/internal/models/types"
)
// DailySleepDTO represents daily sleep data
type DailySleepDTO struct {
UserProfilePK int `json:"userProfilePk"`
CalendarDate time.Time `json:"calendarDate"`
SleepStartTimestampGMT time.Time `json:"sleepStartTimestampGmt"`
SleepEndTimestampGMT time.Time `json:"sleepEndTimestampGmt"`
SleepScores types.SleepScore `json:"sleepScores"` // Using types.SleepScore
UserProfilePK int `json:"userProfilePk"`
CalendarDate time.Time `json:"calendarDate"`
SleepStartTimestampGMT time.Time `json:"sleepStartTimestampGmt"`
SleepEndTimestampGMT time.Time `json:"sleepEndTimestampGmt"`
SleepScores garth.SleepScore `json:"sleepScores"` // Using garth.SleepScore
shared.BaseData
}
@@ -35,8 +35,8 @@ func (d *DailySleepDTO) Get(day time.Time, c shared.APIClient) (any, error) {
}
var response struct {
DailySleepDTO *DailySleepDTO `json:"dailySleepDto"`
SleepMovement []types.SleepMovement `json:"sleepMovement"` // Using types.SleepMovement
DailySleepDTO *DailySleepDTO `json:"dailySleepDto"`
SleepMovement []garth.SleepMovement `json:"sleepMovement"` // Using garth.SleepMovement
}
if err := json.Unmarshal(data, &response); err != nil {
return nil, err

View File

@@ -5,13 +5,13 @@ import (
"fmt"
"time"
types "github.com/sstent/go-garth/internal/models/types"
garth "github.com/sstent/go-garth/pkg/garth/types"
shared "github.com/sstent/go-garth/shared/interfaces"
)
// DetailedSleepDataWithMethods embeds types.DetailedSleepData and adds methods
// DetailedSleepDataWithMethods embeds garth.DetailedSleepData and adds methods
type DetailedSleepDataWithMethods struct {
types.DetailedSleepData
garth.DetailedSleepData
}
func (d *DetailedSleepDataWithMethods) Get(day time.Time, c shared.APIClient) (interface{}, error) {
@@ -29,10 +29,10 @@ func (d *DetailedSleepDataWithMethods) Get(day time.Time, c shared.APIClient) (i
}
var response struct {
DailySleepDTO *types.DetailedSleepData `json:"dailySleepDTO"`
SleepMovement []types.SleepMovement `json:"sleepMovement"`
DailySleepDTO *garth.DetailedSleepData `json:"dailySleepDTO"`
SleepMovement []garth.SleepMovement `json:"sleepMovement"`
RemSleepData bool `json:"remSleepData"`
SleepLevels []types.SleepLevel `json:"sleepLevels"`
SleepLevels []garth.SleepLevel `json:"sleepLevels"`
SleepRestlessMoments []interface{} `json:"sleepRestlessMoments"`
RestlessMomentsCount int `json:"restlessMomentsCount"`
WellnessSpO2SleepSummaryDTO interface{} `json:"wellnessSpO2SleepSummaryDTO"`
@@ -58,8 +58,8 @@ func (d *DetailedSleepDataWithMethods) Get(day time.Time, c shared.APIClient) (i
// GetSleepEfficiency calculates sleep efficiency percentage
func (d *DetailedSleepDataWithMethods) GetSleepEfficiency() float64 {
totalTime := d.SleepEndTimestampGMT.Sub(d.SleepStartTimestampGMT).Seconds()
sleepTime := float64(d.DeepSleepSeconds + d.LightSleepSeconds + d.RemSleepSeconds)
totalTime := d.DetailedSleepData.SleepEndTimestampGMT.Sub(d.DetailedSleepData.SleepStartTimestampGMT).Seconds()
sleepTime := float64(d.DetailedSleepData.DeepSleepSeconds + d.DetailedSleepData.LightSleepSeconds + d.DetailedSleepData.RemSleepSeconds)
if totalTime == 0 {
return 0
}
@@ -68,6 +68,6 @@ func (d *DetailedSleepDataWithMethods) GetSleepEfficiency() float64 {
// GetTotalSleepTime returns total sleep time in hours
func (d *DetailedSleepDataWithMethods) GetTotalSleepTime() float64 {
totalSeconds := d.DeepSleepSeconds + d.LightSleepSeconds + d.RemSleepSeconds
totalSeconds := d.DetailedSleepData.DeepSleepSeconds + d.DetailedSleepData.LightSleepSeconds + d.DetailedSleepData.RemSleepSeconds
return float64(totalSeconds) / 3600.0
}

View File

@@ -5,13 +5,13 @@ import (
"fmt"
"time"
types "github.com/sstent/go-garth/internal/models/types"
garth "github.com/sstent/go-garth/pkg/garth/types"
shared "github.com/sstent/go-garth/shared/interfaces"
)
// TrainingStatusWithMethods embeds types.TrainingStatus and adds methods
// TrainingStatusWithMethods embeds garth.TrainingStatus and adds methods
type TrainingStatusWithMethods struct {
types.TrainingStatus
garth.TrainingStatus
}
func (t *TrainingStatusWithMethods) Get(day time.Time, c shared.APIClient) (interface{}, error) {
@@ -27,7 +27,7 @@ func (t *TrainingStatusWithMethods) Get(day time.Time, c shared.APIClient) (inte
return nil, nil
}
var result types.TrainingStatus
var result garth.TrainingStatus
if err := json.Unmarshal(data, &result); err != nil {
return nil, fmt.Errorf("failed to parse training status: %w", err)
}
@@ -35,9 +35,9 @@ func (t *TrainingStatusWithMethods) Get(day time.Time, c shared.APIClient) (inte
return &TrainingStatusWithMethods{TrainingStatus: result}, nil
}
// TrainingLoadWithMethods embeds types.TrainingLoad and adds methods
// TrainingLoadWithMethods embeds garth.TrainingLoad and adds methods
type TrainingLoadWithMethods struct {
types.TrainingLoad
garth.TrainingLoad
}
func (t *TrainingLoadWithMethods) Get(day time.Time, c shared.APIClient) (interface{}, error) {
@@ -54,7 +54,7 @@ func (t *TrainingLoadWithMethods) Get(day time.Time, c shared.APIClient) (interf
return nil, nil
}
var results []types.TrainingLoad
var results []garth.TrainingLoad
if err := json.Unmarshal(data, &results); err != nil {
return nil, fmt.Errorf("failed to parse training load: %w", err)
}

View File

@@ -4,8 +4,8 @@ import (
"fmt"
"time"
garth "github.com/sstent/go-garth/pkg/garth/types"
shared "github.com/sstent/go-garth/shared/interfaces"
types "github.com/sstent/go-garth/internal/models/types"
)
// VO2MaxData implements the Data interface for VO2 max retrieval
@@ -29,14 +29,14 @@ func (v *VO2MaxData) get(day time.Time, c shared.APIClient) (interface{}, error)
}
// Extract VO2 max data from user settings
vo2Profile := &types.VO2MaxProfile{
vo2Profile := &garth.VO2MaxProfile{
UserProfilePK: settings.ID,
LastUpdated: time.Now(),
}
// Add running VO2 max if available
if settings.UserData.VO2MaxRunning != nil && *settings.UserData.VO2MaxRunning > 0 {
vo2Profile.Running = &types.VO2MaxEntry{
vo2Profile.Running = &garth.VO2MaxEntry{
Value: *settings.UserData.VO2MaxRunning,
ActivityType: "running",
Date: day,
@@ -46,7 +46,7 @@ func (v *VO2MaxData) get(day time.Time, c shared.APIClient) (interface{}, error)
// Add cycling VO2 max if available
if settings.UserData.VO2MaxCycling != nil && *settings.UserData.VO2MaxCycling > 0 {
vo2Profile.Cycling = &types.VO2MaxEntry{
vo2Profile.Cycling = &garth.VO2MaxEntry{
Value: *settings.UserData.VO2MaxCycling,
ActivityType: "cycling",
Date: day,
@@ -77,14 +77,14 @@ func (v *VO2MaxData) List(end time.Time, days int, c shared.APIClient, maxWorker
}
// GetCurrentVO2Max is a convenience method to get current VO2 max values
func GetCurrentVO2Max(c shared.APIClient) (*types.VO2MaxProfile, error) {
func GetCurrentVO2Max(c shared.APIClient) (*garth.VO2MaxProfile, error) {
vo2Data := NewVO2MaxData()
result, err := vo2Data.get(time.Now(), c)
if err != nil {
return nil, err
}
vo2Profile, ok := result.(*types.VO2MaxProfile)
vo2Profile, ok := result.(*garth.VO2MaxProfile)
if !ok {
return nil, fmt.Errorf("unexpected result type")
}

View File

@@ -4,7 +4,7 @@ import (
"testing"
"time"
types "github.com/sstent/go-garth/internal/models/types"
garth "github.com/sstent/go-garth/pkg/garth/types"
"github.com/sstent/go-garth/shared/interfaces"
"github.com/sstent/go-garth/shared/models"
@@ -27,13 +27,13 @@ func TestVO2MaxData_Get(t *testing.T) {
// Mock the get function
vo2Data.GetFunc = func(day time.Time, c interfaces.APIClient) (interface{}, error) {
vo2Profile := &types.VO2MaxProfile{
vo2Profile := &garth.VO2MaxProfile{
UserProfilePK: settings.ID,
LastUpdated: time.Now(),
}
if settings.UserData.VO2MaxRunning != nil && *settings.UserData.VO2MaxRunning > 0 {
vo2Profile.Running = &types.VO2MaxEntry{
vo2Profile.Running = &garth.VO2MaxEntry{
Value: *settings.UserData.VO2MaxRunning,
ActivityType: "running",
Date: day,
@@ -42,7 +42,7 @@ func TestVO2MaxData_Get(t *testing.T) {
}
if settings.UserData.VO2MaxCycling != nil && *settings.UserData.VO2MaxCycling > 0 {
vo2Profile.Cycling = &types.VO2MaxEntry{
vo2Profile.Cycling = &garth.VO2MaxEntry{
Value: *settings.UserData.VO2MaxCycling,
ActivityType: "cycling",
Date: day,
@@ -59,7 +59,7 @@ func TestVO2MaxData_Get(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, result)
profile, ok := result.(*types.VO2MaxProfile)
profile, ok := result.(*garth.VO2MaxProfile)
assert.True(t, ok)
assert.Equal(t, 12345, profile.UserProfilePK)
assert.NotNil(t, profile.Running)

View File

@@ -74,7 +74,7 @@ func (w *WeightDataWithMethods) Get(day time.Time, c shared.APIClient) (any, err
// List implements the Data interface for concurrent fetching
func (w *WeightDataWithMethods) List(end time.Time, days int, c shared.APIClient, maxWorkers int) ([]any, error) {
// BaseData is not part of types.WeightData, so this line needs to be removed or re-evaluated.
// BaseData is not part of garth.WeightData, so this line needs to be removed or re-evaluated.
// For now, I will return an empty slice and no error, as this function is not directly related to the task.
return []any{}, nil
}