Skip to content

Commit a2ebe3b

Browse files
committed
Move FetchAll to method on *api.Client
1 parent 128a06b commit a2ebe3b

2 files changed

Lines changed: 30 additions & 6 deletions

File tree

api/api.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,24 @@ func (c *Client) Fetch(args []string) ([]*Problem, error) {
7575
return payload.Problems, nil
7676
}
7777

78+
// FetchAll retrieves all problems for a given language track from the API
79+
func (c *Client) FetchAll(trackID string) ([]*Problem, error) {
80+
list, err := c.List(trackID)
81+
if err != nil {
82+
return nil, err
83+
}
84+
85+
problems := make([]*Problem, len(list))
86+
for i, prob := range list {
87+
p, err := c.Fetch([]string{trackID, prob})
88+
if err != nil {
89+
return nil, err
90+
}
91+
problems[i] = p[0]
92+
}
93+
return problems, nil
94+
}
95+
7896
// Restore fetches the latest revision of a solution and writes it to disk.
7997
func (c *Client) Restore() ([]*Problem, error) {
8098
url := fmt.Sprintf("%s/v2/exercises/restore?key=%s", c.XAPIHost, c.APIKey)

cmd/fetch.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,25 @@ func Fetch(ctx *cli.Context) error {
1919
client := api.NewClient(c)
2020

2121
args := ctx.Args()
22-
problems, err := client.Fetch(args)
22+
var problems []*api.Problem
2323

2424
if ctx.Bool("all") {
2525
if len(args) > 0 {
2626
trackID := args[0]
27-
problems = fetchAll(trackID, client)
27+
p, err := client.FetchAll(trackID)
28+
if err != nil {
29+
log.Fatal(err)
30+
}
31+
problems = p
2832
} else {
2933
log.Fatalf("You must supply a track to fetch all exercises")
3034
}
31-
}
32-
33-
if err != nil {
34-
log.Fatal(err)
35+
} else {
36+
p, err := client.Fetch(args)
37+
if err != nil {
38+
log.Fatal(err)
39+
}
40+
problems = p
3541
}
3642

3743
submissionInfo, err := client.Submissions()

0 commit comments

Comments
 (0)