-
Notifications
You must be signed in to change notification settings - Fork 5
/
client.go
257 lines (209 loc) · 7.39 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package sheets
import (
"bytes"
"compress/gzip"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
)
// Client holds the google spreadsheet custom API Client.
type Client struct {
HTTPClient *http.Client
}
// NewClient creates and returns a new spreadsheet HTTP Client.
// It accepts `http.RoundTriper` which is used for oauth2 authentication,
// see `ServiceAccount` and `Token` package-level functions.
func NewClient(authentication http.RoundTripper) *Client {
return &Client{
HTTPClient: &http.Client{
Transport: authentication,
},
}
}
// A RequestOption can be passed on `Do` method to modify a Request.
type RequestOption interface{ Apply(*http.Request) }
// Query is a `RequestOption` which sets URL query values to the Request.
type Query url.Values
// Apply implements the `RequestOption` interface.
// It adds the "q" values to the request.
func (q Query) Apply(r *http.Request) {
query := r.URL.Query()
for k, v := range q {
query[k] = v
}
r.URL.RawQuery = query.Encode()
}
type gzipReadCloser struct {
gzipReader io.ReadCloser
responseReader io.ReadCloser
}
func (r *gzipReadCloser) Close() (lastErr error) {
r.gzipReader.Close()
return r.responseReader.Close()
}
func (r *gzipReadCloser) Read(p []byte) (n int, err error) {
return r.gzipReader.Read(p)
}
// Do sends an HTTP request and returns an HTTP response.
// It respects gzip and some settings specified to google's spreadsheet API.
// The last option can be used to modify a request before sent to the server.
func (c *Client) Do(ctx context.Context, method, url string, body io.Reader, options ...RequestOption) (*http.Response, error) {
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
query := req.URL.Query()
query.Set("prettyPrint", "false")
req.URL.RawQuery = query.Encode()
req.Header.Add("Accept", "application/json")
req.Header.Add("Accept-Encoding", "gzip")
for _, opt := range options {
opt.Apply(req)
}
response, err := c.HTTPClient.Do(req.WithContext(ctx))
if err != nil {
select {
case <-ctx.Done():
err = ctx.Err()
default:
}
if response != nil && response.Body != nil {
response.Body.Close()
}
return nil, err
}
if encoding := response.Header.Get("Content-Encoding"); encoding == "gzip" {
r, err := gzip.NewReader(response.Body)
if err != nil {
return nil, err
}
response.Body = &gzipReadCloser{responseReader: response.Body, gzipReader: r}
}
return response, err
}
// ReadJSON fires a request to "url" and binds a JSON response to the "toPtr".
func (c *Client) ReadJSON(ctx context.Context, method, url string, requestData, toPtr interface{}, options ...RequestOption) error {
var requestBody io.Reader
if requestData != nil {
buf := new(bytes.Buffer)
err := json.NewEncoder(buf).Encode(requestData)
if err != nil {
return err
}
requestBody = buf
}
resp, err := c.Do(ctx, method, url, requestBody, options...)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return newResourceError(resp)
}
return json.NewDecoder(resp.Body).Decode(toPtr)
}
const spreadsheetURL = "https://sheets.googleapis.com/v4/spreadsheets/%s"
// GetSpreadsheetInfo returns general information about a spreadsheet based on the provided "spreadsheetID".
func (c *Client) GetSpreadsheetInfo(ctx context.Context, spreadsheetID string) (*Spreadsheet, error) {
url := fmt.Sprintf(spreadsheetURL, spreadsheetID)
sd := &Spreadsheet{}
err := c.ReadJSON(ctx, http.MethodGet, url, nil, sd)
if err != nil {
return nil, err
}
return sd, nil
}
const (
spreadsheetValuesURL = spreadsheetURL + "/values/%s"
spreadsheetValuesBatchGetURL = spreadsheetURL + "/values:batchGet"
spreadsheetValuesClearURL = spreadsheetValuesURL + ":clear"
spreadsheetBatchUpdateURL = spreadsheetURL + ":batchUpdate"
)
// Range returns record values of a spreadsheet based on the provided "dataRanges", if more than one data range then it sends a batch request.
// See `ReadSpreadsheet` method too.
func (c *Client) Range(ctx context.Context, spreadsheetID string, dataRanges ...string) ([]ValueRange, error) {
if len(dataRanges) == 1 {
// https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/get
url := fmt.Sprintf(spreadsheetValuesURL, spreadsheetID, dataRanges[0])
var payload ValueRange
err := c.ReadJSON(ctx, http.MethodGet, url, nil, &payload)
if err != nil {
return nil, err
}
return []ValueRange{payload}, nil
}
// https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/batchGet
url := fmt.Sprintf(spreadsheetValuesBatchGetURL, spreadsheetID)
q := Query{"ranges": dataRanges}
var payload = struct {
ValueRanges []ValueRange `json:"valueRanges"`
}{}
err := c.ReadJSON(ctx, http.MethodGet, url, nil, &payload, q)
if err != nil {
return nil, err
}
return payload.ValueRanges, nil
}
// ReadSpreadsheet binds record values of a spreadsheet to the "dest".
// See `Range` method too.
func (c *Client) ReadSpreadsheet(ctx context.Context, dest interface{}, spreadsheetID string, dataRanges ...string) error {
valueRanges, err := c.Range(ctx, spreadsheetID, dataRanges...)
if err != nil {
return err
}
return DecodeValueRange(dest, valueRanges...)
}
// ClearSpreadsheet clears values from a spreadsheet. The caller must specify the spreadsheet ID and range.
// Only values are cleared -- all other properties of the cell (such as formatting, data validation, etc..) are kept.
func (c *Client) ClearSpreadsheet(ctx context.Context, spreadsheetID, dataRange string) (response ClearValuesResponse, err error) {
if dataRange == "" || dataRange == "*" {
dataRange = "A1:Z"
}
// https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/clear
url := fmt.Sprintf(spreadsheetValuesClearURL, spreadsheetID, dataRange)
err = c.ReadJSON(ctx, http.MethodPost, url, nil, &response)
return
}
// UpdateSpreadsheet updates a spreadsheet of a range of provided "dataRange",
// if "dataRange" is empty or "*" then it will update all columns specified by "values".
func (c *Client) UpdateSpreadsheet(ctx context.Context, spreadsheetID string, values ValueRange) (response UpdateValuesResponse, err error) {
if values.Range == "" || values.Range == "*" {
values.Range = "A1:Z"
}
if values.MajorDimension == "" {
values.MajorDimension = Rows
}
// https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/update
url := fmt.Sprintf(spreadsheetValuesURL, spreadsheetID, values.Range)
q := Query{
"valueInputOption": []string{"RAW"},
"includeValuesInResponse": []string{"false"},
}
err = c.ReadJSON(ctx, http.MethodPut, url, values, &response, q)
return
}
type batchUpdate struct {
Requests []batchUpdateRequest `json:"requests,omitempty"`
}
type batchUpdateRequest struct {
AddChart batchUpdateAddChartRequest `json:"addChart,omitempty"`
}
type batchUpdateAddChartRequest struct {
Chart Chart `json:"chart,omitempty"`
}
// AddChart creates or updates an existing chart to a spreadsheet.
func (c *Client) AddChart(ctx context.Context, spreadsheetID string, chart Chart) (response BatchUpdateResponse, err error) {
// https://developers.google.com/sheets/api/samples/charts#add_a_column_chart
url := fmt.Sprintf(spreadsheetBatchUpdateURL, spreadsheetID)
err = c.ReadJSON(ctx, http.MethodPost, url, batchUpdate{
Requests: []batchUpdateRequest{
{AddChart: batchUpdateAddChartRequest{
Chart: chart,
}},
},
}, &response)
return
}