Skip to content

Commit 13c452d

Browse files
committed
Add GetHabit
1 parent f4b1d2a commit 13c452d

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

habits.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@ package habitify
22

33
import (
44
"context"
5+
"fmt"
56
"time"
67
)
78

9+
type getHabitResp struct {
10+
Message string `json:"message"`
11+
Habit *Habit `json:"data"`
12+
Version string `json:"version"`
13+
Status bool `json:"status"`
14+
}
15+
816
type listHabitsResp struct {
917
Message string `json:"message"`
1018
Habits []*Habit `json:"data"`
@@ -40,6 +48,21 @@ type Habit struct {
4048
Priority float64 `json:"priority"`
4149
}
4250

51+
func (c *Client) GetHabit(ctx context.Context, id string) (*Habit, error) {
52+
resp, err := c.get(ctx, fmt.Sprintf("%s/%s", urlHabits, id))
53+
if err != nil {
54+
return nil, err
55+
}
56+
defer resp.Close()
57+
58+
var response getHabitResp
59+
if err := resp.DecodeJSON(&response); err != nil {
60+
return nil, err
61+
}
62+
63+
return response.Habit, nil
64+
}
65+
4366
func (c *Client) ListHabits(ctx context.Context) ([]*Habit, error) {
4467
resp, err := c.get(ctx, urlHabits)
4568
if err != nil {

0 commit comments

Comments
 (0)