-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
92 lines (72 loc) · 1.74 KB
/
api.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package shiftboard
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
)
func (c *Client) ListSites() (*Response, error) {
payload := new(bytes.Buffer)
if err := json.NewEncoder(payload).Encode(c.Auth); err != nil {
return nil, err
}
url := fmt.Sprintf("%s/sites", c.BaseURL)
req, err := http.NewRequest("POST", url, payload)
if err != nil {
return nil, err
}
resp := Response{}
err = c.sendRequest(req, &resp)
if err != nil {
return nil, err
}
if !resp.Success {
return nil, fmt.Errorf("ListSites failed: %v %v", resp.Message, resp.Error)
}
return &resp, nil
}
func (c *Client) Login(orgID string) (*Response, error) {
baseURL := fmt.Sprintf("%s/login", c.BaseURL)
req, err := http.NewRequest("GET", baseURL, nil)
if err != nil {
return nil, err
}
// Set query parameters
q := req.URL.Query()
q.Add("orgID", orgID)
q.Add("user_privacy", "true")
req.URL.RawQuery = q.Encode()
resp := Response{}
err = c.sendRequest(req, &resp)
if err != nil {
return nil, err
}
if !resp.Success {
return nil, fmt.Errorf("Login failed: %v %v", resp.Message, resp.Error)
}
c.Auth.AccessToken = resp.Data.AccessToken
return &resp, nil
}
func (c *Client) ListShifts(startDate string, endDate string) (*Response, error) {
baseURL := fmt.Sprintf("%s/shifts", c.BaseURL)
req, err := http.NewRequest("GET", baseURL, nil)
if err != nil {
log.Fatal(err)
}
// Set query parameters
q := req.URL.Query()
q.Add("start_date", startDate)
q.Add("end_date", endDate)
q.Add("batch", "1000")
req.URL.RawQuery = q.Encode()
resp := Response{}
err = c.sendRequest(req, &resp)
if err != nil {
return nil, err
}
if !resp.Success {
return nil, fmt.Errorf("ListShifts failed: %v %v", resp.Message, resp.Error)
}
return &resp, nil
}