Stream template upload (#6035)

Resolves #5718
This commit is contained in:
Ammar Bandukwala
2023-02-04 20:07:09 +00:00
committed by GitHub
parent 77fd34be94
commit a422cc00e8
12 changed files with 69 additions and 46 deletions
+6 -4
View File
@@ -109,10 +109,13 @@ func (c *Client) Request(ctx context.Context, method, path string, body interfac
var r io.Reader
if body != nil {
if data, ok := body.([]byte); ok {
switch data := body.(type) {
case io.Reader:
r = data
case []byte:
r = bytes.NewReader(data)
} else {
// Assume JSON if not bytes.
default:
// Assume JSON in all other cases.
buf := bytes.NewBuffer(nil)
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
@@ -120,7 +123,6 @@ func (c *Client) Request(ctx context.Context, method, path string, body interfac
if err != nil {
return nil, xerrors.Errorf("encode body: %w", err)
}
r = buf
}
}
+2 -2
View File
@@ -21,8 +21,8 @@ type UploadResponse struct {
// Upload uploads an arbitrary file with the content type provided.
// This is used to upload a source-code archive.
func (c *Client) Upload(ctx context.Context, contentType string, content []byte) (UploadResponse, error) {
res, err := c.Request(ctx, http.MethodPost, "/api/v2/files", content, func(r *http.Request) {
func (c *Client) Upload(ctx context.Context, contentType string, rd io.Reader) (UploadResponse, error) {
res, err := c.Request(ctx, http.MethodPost, "/api/v2/files", rd, func(r *http.Request) {
r.Header.Set("Content-Type", contentType)
})
if err != nil {