-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
168 lines (146 loc) · 3.75 KB
/
client.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path"
"time"
)
type Client struct {
BaseURL *url.URL
HTTPClient *http.Client
Token string
}
type Item struct {
ID string `json:"id"`
Timetable Timetable
UpdatedAt time.Time `json:"updated-at"`
Name string `json:"name"`
CreatedAt time.Time `json:"created-at"`
ProjectSlug string `json:"project-slug"`
Parameters Parameters `json:"parameters"`
Actor Actor `json:"actor"`
Description string `json:"description"`
}
type Parameters map[string]interface{}
type Actor struct {
ID string `json:"id"`
Login string `json:"login"`
Name string `json:"name"`
}
type Timetable struct {
PerHour int `json:"per-hour"`
HourOfDay []int `json:"hours-of-day"`
DaysOfWeek []string `json:"days-of-week"`
}
type GetScheduleResponse struct {
Items []*Item `json:"items"`
NextPageToken string `json:"next_page_token"`
}
type DeleteResponse struct {
Message string `json:"message"`
}
func NewClient() (*Client, error) {
token := os.Getenv("CIRCLECI_TOKEN")
if len(token) == 0 {
return nil, fmt.Errorf("must set env `CIRCLECI_TOKEN`")
}
return &Client{
BaseURL: &url.URL{Host: "circleci.com", Scheme: "https", Path: "/api/v2/"},
HTTPClient: http.DefaultClient,
Token: token,
}, nil
}
func (c *Client) newRequest(ctx context.Context, method, spath string, bodyStruct interface{}) (*http.Request, error) {
u := *c.BaseURL
u.Path = path.Join(c.BaseURL.Path, spath)
req, err := http.NewRequestWithContext(ctx, method, u.String(), nil)
if bodyStruct != nil {
body, err := json.Marshal(bodyStruct)
if err != nil {
return nil, err
}
req.Body = io.NopCloser(bytes.NewBuffer(body))
}
if err != nil {
return nil, err
}
req.Header.Set("circle-token", c.Token)
req.Header.Set("Content-Type", "application/json")
return req, nil
}
func decodeBody(resp *http.Response, out interface{}) error {
defer resp.Body.Close()
dec := json.NewDecoder(resp.Body)
return dec.Decode(out)
}
// TODO: support page-token
func (c *Client) GetAllSchedules(ctx context.Context, project string) ([]*Item, error) {
spath := fmt.Sprintf("project/gh/%s/schedule", project)
req, err := c.newRequest(ctx, "GET", spath, nil)
if err != nil {
return nil, err
}
res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
var resp GetScheduleResponse
if err := decodeBody(res, &resp); err != nil {
return nil, err
}
return resp.Items, nil
}
func (c *Client) CreateSchedule(ctx context.Context, project string, body interface{}) (*Item, error) {
spath := fmt.Sprintf("project/gh/%s/schedule", project)
req, err := c.newRequest(ctx, "POST", spath, body)
if err != nil {
return nil, err
}
res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
var resp Item
if err := decodeBody(res, &resp); err != nil {
return nil, err
}
return &resp, nil
}
func (c *Client) UpdateSchedule(ctx context.Context, scheduleId string, body interface{}) (*Item, error) {
spath := fmt.Sprintf("schedule/%s", scheduleId)
req, err := c.newRequest(ctx, "PATCH", spath, body)
if err != nil {
return nil, err
}
res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
var resp *Item
if err := decodeBody(res, &resp); err != nil {
return nil, err
}
return resp, nil
}
func (c *Client) DeleteSchedule(ctx context.Context, scheduleId string) (string, error) {
spath := fmt.Sprintf("schedule/%s", scheduleId)
req, err := c.newRequest(ctx, "DELETE", spath, nil)
if err != nil {
return "", err
}
res, err := c.HTTPClient.Do(req)
if err != nil {
return "", err
}
var resp DeleteResponse
if err := decodeBody(res, &resp); err != nil {
return "", err
}
return resp.Message, nil
}