-
Notifications
You must be signed in to change notification settings - Fork 296
/
chunks.go
63 lines (52 loc) · 1.4 KB
/
chunks.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
package api
import (
"bytes"
"compress/gzip"
"fmt"
"mime/multipart"
)
// ChunksService handles communication with the chunk related methods of the
// Buildkite Agent API.
type ChunksService struct {
client *Client
}
// Chunk represents a Buildkite Agent API Chunk
type Chunk struct {
Data string
Sequence int
}
// Uploads the chunk to the Buildkite Agent API. This request doesn't use JSON,
// but a multi-part HTTP form upload
func (cs *ChunksService) Upload(jobId string, chunk *Chunk) (*Response, error) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
// Gzip the chunk data
var b bytes.Buffer
gz := gzip.NewWriter(&b)
if _, err := gz.Write([]byte(chunk.Data)); err != nil {
return nil, err
}
if err := gz.Flush(); err != nil {
return nil, err
}
if err := gz.Close(); err != nil {
return nil, err
}
// Write the chunk to the form
part, _ := writer.CreateFormFile("chunk", "chunk.gz")
part.Write(b.Bytes())
// Write the sequence value to the form
writer.WriteField("sequence", fmt.Sprintf("%d", chunk.Sequence))
// Close the writer because we don't need to add any more values to it
err := writer.Close()
if err != nil {
return nil, err
}
u := fmt.Sprintf("jobs/%s/chunks", jobId)
req, err := cs.client.NewFormRequest("POST", u, body)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", writer.FormDataContentType())
return cs.client.Do(req, nil)
}