/
toggl.go
56 lines (49 loc) · 1.15 KB
/
toggl.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
package toggl
import (
"context"
"encoding/json"
"io"
"net/http"
"net/url"
"path"
"time"
)
const (
togglAPIBaseURL = "https://api.track.toggl.com/api/v8"
)
// Client facilitates making HTTP requests to the Toggl API
type Client struct {
apiKey string
URL *url.URL
httpClient *http.Client
}
// NewClient initializes a Client
func NewClient(apiKey string, timeout time.Duration) *Client {
parsedURL, _ := url.ParseRequestURI(togglAPIBaseURL)
httpClient := &http.Client{
Timeout: timeout,
}
return &Client{
apiKey: apiKey,
URL: parsedURL,
httpClient: httpClient,
}
}
func (c *Client) newRequest(ctx context.Context, method, spath string, rawQuery *string, body io.Reader) (*http.Request, error) {
u := *c.URL
u.Path = path.Join(c.URL.Path, spath)
if rawQuery != nil {
u.RawQuery = *rawQuery
}
req, err := http.NewRequestWithContext(ctx, method, u.String(), body)
if err != nil {
return nil, err
}
req.SetBasicAuth(c.apiKey, "api_token")
return req, nil
}
func decodeBody(resp *http.Response, out interface{}) error {
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
return decoder.Decode(out)
}