abrander / garmin-connect

Golang client for the (unoffical reverse engineered) Garmin Connect API
MIT License
136 stars 29 forks source link

Add support for workouts #21

Open tom-dudley opened 1 year ago

tom-dudley commented 1 year ago

This PR adds support for workouts (create, get, list, update and delete) and the ability to schedule them (create, list and delete). I've attached some docs at the end of the PR.

I've opted for a bunch of pointers in the Workout struct mainly to handle nil/null. This does mean consuming isn't 100% ideal, but seemed the best compromise (over pulling in other dependencies or leaving "" all over).

Happy to hear feedback and adjust accordingly.

API

Create workout

See below for example workout

workoutResponse, err := client.CreateWorkout(workout)

Get workout

workout, err := client.GetWorkout(workoutId)

Update workout

err = client.UpdateWorkout(workout)

Delete workout

client.DeleteScheduledWorkout(workoutScheduleId)

List workouts

workouts, err := client.Workout()

Schedule workout

workoutSchedule, err := client.ScheduleWorkout(workout, time.Date(...))

Get scheduled workouts

workoutSchedules, err := client.WorkoutSchedule(year, month)

Example workout

workoutToCreate := &connect.Workout{
    SportType: &connect.SportType{
        SportTypeId:  1,
        SportTypeKey: "running",
    },
    WorkoutName: "Test12345",
    WorkoutSegments: []*connect.WorkoutSegment{
        {
            SegmentOrder: 1,
            SportType: connect.SportType{
                SportTypeId:  1,
                SportTypeKey: "running",
            },
            WorkoutSteps: []connect.WorkoutStep{
                {
                    StepId:    1,
                    Type:      "ExecutableStepDTO",
                    StepOrder: 1,
                    StepType: &connect.StepType{
                        StepTypeId:  1,
                        StepTypeKey: "warmup",
                    },
                    EndCondition: &connect.EndCondition{
                        ConditionTypeId:  1,
                        ConditionTypeKey: "lap.button",
                    },
                    TargetType: &connect.TargetType{
                        WorkoutTargetTypeId:  1,
                        WorkoutTargetTypeKey: "no.target",
                    },
                },
            },
        },
    },
    AvgTrainingSpeed: 3.0,
}