-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathGoal.go
115 lines (93 loc) · 2.83 KB
/
Goal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package connect
import (
"fmt"
)
// Goal represents a fitness or health goal.
type Goal struct {
ID int64 `json:"id"`
ProfileID int64 `json:"userProfilePK"`
GoalCategory int `json:"userGoalCategoryPK"`
GoalType GoalType `json:"userGoalTypePK"`
Start Date `json:"startDate"`
End Date `json:"endDate,omitempty"`
Value int `json:"goalValue"`
Created Date `json:"createDate"`
}
// GoalType represents different types of goals.
type GoalType int
// String implements Stringer.
func (t GoalType) String() string {
switch t {
case 0:
return "steps-per-day"
case 4:
return "weight"
case 7:
return "floors-ascended"
default:
return fmt.Sprintf("unknown:%d", t)
}
}
// Goals lists all goals for displayName of type goalType. If displayName is
// empty, the currently authenticated user will be used.
func (c *Client) Goals(displayName string, goalType int) ([]Goal, error) {
if displayName == "" && c.Profile == nil {
return nil, ErrNotAuthenticated
}
if displayName == "" && c.Profile != nil {
displayName = c.Profile.DisplayName
}
URL := fmt.Sprintf("https://connect.garmin.com/modern/proxy/wellness-service/wellness/wellness-goals/%s?userGoalType=%d",
displayName,
goalType,
)
goals := make([]Goal, 0, 20)
err := c.getJSON(URL, &goals)
if err != nil {
return nil, err
}
return goals, nil
}
// AddGoal will add a new goal. If displayName is empty, the currently
// authenticated user will be used.
func (c *Client) AddGoal(displayName string, goal Goal) error {
if displayName == "" && c.Profile == nil {
return ErrNotAuthenticated
}
if displayName == "" && c.Profile != nil {
displayName = c.Profile.DisplayName
}
URL := fmt.Sprintf("https://connect.garmin.com/modern/proxy/wellness-service/wellness/wellness-goals/%s",
displayName,
)
return c.write("POST", URL, goal, 204)
}
// DeleteGoal will delete an existing goal. If displayName is empty, the
// currently authenticated user will be used.
func (c *Client) DeleteGoal(displayName string, goalID int) error {
if displayName == "" && c.Profile == nil {
return ErrNotAuthenticated
}
if displayName == "" && c.Profile != nil {
displayName = c.Profile.DisplayName
}
URL := fmt.Sprintf("https://connect.garmin.com/modern/proxy/wellness-service/wellness/wellness-goals/%d/%s",
goalID,
displayName,
)
return c.write("DELETE", URL, nil, 204)
}
// UpdateGoal will update an existing goal.
func (c *Client) UpdateGoal(displayName string, goal Goal) error {
if displayName == "" && c.Profile == nil {
return ErrNotAuthenticated
}
if displayName == "" && c.Profile != nil {
displayName = c.Profile.DisplayName
}
URL := fmt.Sprintf("https://connect.garmin.com/modern/proxy/wellness-service/wellness/wellness-goals/%d/%s",
goal.ID,
displayName,
)
return c.write("PUT", URL, goal, 204)
}