Skip to content

Commit

Permalink
Add GetHabit
Browse files Browse the repository at this point in the history
  • Loading branch information
kentakozuka committed Jun 17, 2022
1 parent f4b1d2a commit 13c452d
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions habits.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@ package habitify

import (
"context"
"fmt"
"time"
)

type getHabitResp struct {
Message string `json:"message"`
Habit *Habit `json:"data"`
Version string `json:"version"`
Status bool `json:"status"`
}

type listHabitsResp struct {
Message string `json:"message"`
Habits []*Habit `json:"data"`
Expand Down Expand Up @@ -40,6 +48,21 @@ type Habit struct {
Priority float64 `json:"priority"`
}

func (c *Client) GetHabit(ctx context.Context, id string) (*Habit, error) {
resp, err := c.get(ctx, fmt.Sprintf("%s/%s", urlHabits, id))
if err != nil {
return nil, err
}
defer resp.Close()

var response getHabitResp
if err := resp.DecodeJSON(&response); err != nil {
return nil, err
}

return response.Habit, nil
}

func (c *Client) ListHabits(ctx context.Context) ([]*Habit, error) {
resp, err := c.get(ctx, urlHabits)
if err != nil {
Expand Down

0 comments on commit 13c452d

Please sign in to comment.