feat(cli/session-recordings): add download cmd

This commit is contained in:
Jim authored and Timothy Messier committed 2023-05-31 12:18:22 +00:00
1 parent 9207ec08f2
commit d49fde2299
8 files changed
+224 -13

No files matched your search

+3
View File
@@ -44,6 +44,9 @@ const (
EnvBoundaryToken = "BOUNDARY_TOKEN"
EnvBoundaryRateLimit = "BOUNDARY_RATE_LIMIT"
EnvBoundarySRVLookup = "BOUNDARY_SRV_LOOKUP"
AsciiCastMimeType = "application/x-asciicast"
StreamChunkSize = 1024 * 64 // stream chuck buffer size
)
// Config is used to configure the creation of the client
+19 -4
View File
@@ -4,14 +4,18 @@
package sessionrecordings
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/url"
"github.com/hashicorp/boundary/api"
)
const chunkSize = 64 * 1024 // assume we're using 64 KiB see: https://github.com/grpc/grpc.github.io/issues/371
// Download will of course download the request session recording resource.
// Currently it always requests a mime-type of asciicast.
func (c *Client) Download(ctx context.Context, contentId string, opt ...Option) (io.ReadCloser, error) {
switch {
case contentId == "":
@@ -22,11 +26,12 @@ func (c *Client) Download(ctx context.Context, contentId string, opt ...Option)
opts, apiOpts := getOpts(opt...)
req, err := c.client.NewRequest(ctx, "GET", "session_recordings/"+url.PathEscape(contentId)+":download", nil, apiOpts...)
req, err := c.client.NewRequest(ctx, "GET", "session-recordings/"+url.PathEscape(contentId)+":download", nil, apiOpts...)
if err != nil {
return nil, fmt.Errorf("error creating download request: %w", err)
}
req.Header.Set("Accept", "application/x-asciicast")
opts.queryMap["mime_type"] = api.AsciiCastMimeType
req.Header.Set("Accept", api.AsciiCastMimeType)
if len(opts.queryMap) > 0 {
q := url.Values{}
@@ -40,5 +45,15 @@ func (c *Client) Download(ctx context.Context, contentId string, opt ...Option)
if err != nil {
return nil, fmt.Errorf("error performing client request during download call: %w", err)
}
if resp.StatusCode() >= 400 {
resp.Body = new(bytes.Buffer)
if _, err := resp.Body.ReadFrom(resp.HttpResponse().Body); err != nil {
return nil, fmt.Errorf("error reading response body: %w", err)
}
if resp.Body.Len() > 0 {
return nil, errors.New(resp.Body.String())
}
return nil, fmt.Errorf("error reading response body: status was %d", resp.StatusCode())
}
return resp.HttpResponse().Body, nil
}