-
Notifications
You must be signed in to change notification settings - Fork 10
/
toggl.go
78 lines (72 loc) · 1.8 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
)
type workspaceResponse struct {
Workspace *Workspace `json:"data"`
}
func getTogglWorkspaceID(APIToken string) (int, error) {
var workspaceID int
url := fmt.Sprintf("%s/api/pipes/workspace", urls.TogglAPIHost[*environment])
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return workspaceID, err
}
req.Header.Set("User-Agent", "toggl-pipes")
req.SetBasicAuth(APIToken, "api_token")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return workspaceID, err
}
var b []byte
defer resp.Body.Close()
b, err = ioutil.ReadAll(resp.Body)
if err != nil {
return workspaceID, err
}
if http.StatusOK != resp.StatusCode {
return workspaceID, fmt.Errorf("GET workspace failed %d", resp.StatusCode)
}
var response workspaceResponse
if err := json.Unmarshal(b, &response); err != nil {
return workspaceID, err
}
return response.Workspace.ID, nil
}
func postPipesAPI(APIToken, pipeID string, payload interface{}) ([]byte, error) {
start := time.Now()
url := fmt.Sprintf("%s/api/pipes/%s", urls.TogglAPIHost[*environment], pipeID)
b, err := json.Marshal(payload)
if err != nil {
return nil, err
}
buf := bytes.NewBuffer(b)
req, err := http.NewRequest("POST", url, buf)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "toggl-pipes")
req.SetBasicAuth(APIToken, "api_token")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
b, err = ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if 200 != resp.StatusCode {
return b, fmt.Errorf("%s failed with status code %d", url, resp.StatusCode)
}
log.Println("Response from Toggl API:", string(b), "-", time.Since(start))
return b, nil
}