-
Notifications
You must be signed in to change notification settings - Fork 301
/
chunks.go
46 lines (39 loc) · 1.13 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
package api
import (
"bytes"
"compress/gzip"
"fmt"
)
// 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
Offset int
Size int
}
// Uploads the chunk to the Buildkite Agent API. This request sends the
// compressed log directly as a request body.
func (cs *ChunksService) Upload(jobId string, chunk *Chunk) (*Response, error) {
// Create a compressed buffer of the log content
body := &bytes.Buffer{}
gzipper := gzip.NewWriter(body)
gzipper.Write([]byte(chunk.Data))
if err := gzipper.Close(); err != nil {
return nil, err
}
// Pass most params as query
u := fmt.Sprintf("jobs/%s/chunks?sequence=%d&offset=%d&size=%d", jobId, chunk.Sequence, chunk.Offset, chunk.Size)
req, err := cs.client.NewFormRequest("POST", u, body)
if err != nil {
return nil, err
}
// Mark the request as a direct compressed log chunk
req.Header.Add("Content-Type", "text/plain")
req.Header.Add("Content-Encoding", "gzip")
return cs.client.Do(req, nil)
}