-
Notifications
You must be signed in to change notification settings - Fork 1
/
time_entries.go
70 lines (57 loc) · 1.76 KB
/
time_entries.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
package harvest
import (
"encoding/json"
"log"
"time"
"github.com/polarsquad/harvest/structs"
)
// GetEntries fetches all the TimeEntries with the provided timespan.
// If from and/or to dates are not defined, it will fetch all the TimeEntries.
//
// Parameters for the function:
// from: time.Time with format "2006-01-02"
// to: time.Time with format "2006-01-02"
// u: User, specifies which users TimeEntries are fetched.
func (h *Harvest) GetEntries(from time.Time, to time.Time, u *User) *TimeEntries {
var times TimeEntries
params := GetTimeEntriesParams{
UserID: u.ID,
From: from.Format("2006-01-02"),
PerPage: 10,
To: to.Format("2006-01-02"),
}
urlWithParams, _ := addParamsToURL(timeEntriesURL, ¶ms)
body, err := h.getURL(urlWithParams)
if err != nil {
log.Fatalln(err.Error())
}
err = json.Unmarshal(body, ×)
if err != nil {
log.Fatalln(err.Error())
}
log.Printf("TotalEntries: %v, Pages: %v\n", times.TotalEntries, times.TotalPages)
if times.Links.Next != "" {
entries := h.getAllEntries(times.Links)
times.Entries = append(times.Entries, entries...)
}
return ×
}
// func (h *Harvest) getAllEntries(l structs.Links, entries *[]structs.Entries, i int) Entries {
func (h *Harvest) getAllEntries(l structs.Links) Entries {
var times TimeEntries
// getURL to fetch additional Entries from Links.Next URL
body, _ := h.getURL(l.Next)
err := json.Unmarshal(body, ×)
if err != nil {
log.Fatalln(err.Error())
}
if times.Links.Next != "" {
// IF Next URL still available, let's call getAllEntries() again.
entries := h.getAllEntries(times.Links)
times.Entries = append(times.Entries, entries...)
} else {
// Return times.Entries since no Links.Next URL available
return times.Entries
}
return times.Entries
}