feat(cli/session-recordings): add download cmd
This commit is contained in:
1 parent
9207ec08f2
commit
d49fde2299
8 files changed
+224
-13
No files matched your search
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user