github.com/bshelton229/agent@v3.5.4+incompatible/api/chunks.go (about)

     1  package api
     2  
     3  import (
     4  	"bytes"
     5  	"compress/gzip"
     6  	"fmt"
     7  )
     8  
     9  // ChunksService handles communication with the chunk related methods of the
    10  // Buildkite Agent API.
    11  type ChunksService struct {
    12  	client *Client
    13  }
    14  
    15  // Chunk represents a Buildkite Agent API Chunk
    16  type Chunk struct {
    17  	Data     string
    18  	Sequence int
    19  	Offset   int
    20  	Size     int
    21  }
    22  
    23  // Uploads the chunk to the Buildkite Agent API. This request sends the
    24  // compressed log directly as a request body.
    25  func (cs *ChunksService) Upload(jobId string, chunk *Chunk) (*Response, error) {
    26  	// Create a compressed buffer of the log content
    27  	body := &bytes.Buffer{}
    28  	gzipper := gzip.NewWriter(body)
    29  	gzipper.Write([]byte(chunk.Data))
    30  	if err := gzipper.Close(); err != nil {
    31  		return nil, err
    32  	}
    33  
    34  	// Pass most params as query
    35  	u := fmt.Sprintf("jobs/%s/chunks?sequence=%d&offset=%d&size=%d", jobId, chunk.Sequence, chunk.Offset, chunk.Size)
    36  	req, err := cs.client.NewFormRequest("POST", u, body)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  
    41  	// Mark the request as a direct compressed log chunk
    42  	req.Header.Add("Content-Type", "text/plain")
    43  	req.Header.Add("Content-Encoding", "gzip")
    44  
    45  	return cs.client.Do(req, nil)
    46  }