/
request.go
48 lines (39 loc) · 1.24 KB
/
request.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
package at
import (
"encoding/json"
"time"
)
// Credentials holds the authentication details, currently unused in this snippet.
type Credentials struct {
Username string
Password string
}
// RequestEntry represents a single entry as received in a JSON request.
type RequestEntry struct {
Id int `json:"id"`
IsTicket bool `json:"isTicket"`
Date time.Time `json:"date"`
StartTime string `json:"startTime"`
Duration float32 `json:"duration"`
Summary string `json:"summary"`
Project string `json:"project"`
}
// UnmarshalToRequestEntries converts JSON data into a slice of RequestEntry.
func UnmarshalToRequestEntries(data []byte) ([]RequestEntry, error) {
var r []RequestEntry
err := json.Unmarshal(data, &r)
return r, err
}
// UnmarshalToTimeEntries converts JSON data into a TimeEntries.
func UnmarshalToTimeEntries(data []byte, dateFormat string) (TimeEntries, error) {
r, err := UnmarshalToRequestEntries(data)
if err != nil {
return nil, err
}
var entries TimeEntries
for _, e := range r {
te := NewEntry(e.Id, e.IsTicket, e.Date, e.StartTime, e.Duration, e.Summary, e.Project, dateFormat)
entries = append(entries, te)
}
return entries, nil
}