mirror of
https://github.com/coder/coder.git
synced 2025-07-09 11:45:56 +00:00
* Add templates
* Move API structs to codersdk
* Back to green tests!
* It all works, but now with tea! 🧋
* It works!
* Add cancellation to provisionerd
* Tests pass!
* Add deletion of workspaces and projects
* Fix agent lock
* Add clog
* Fix linting errors
* Remove unused CLI tests
* Rename daemon to start
* Fix leaking command
* Fix promptui test
* Update agent connection frequency
* Skip login tests on Windows
* Increase tunnel connect timeout
* Fix templater
* Lower test requirements
* Fix embed
* Disable promptui tests for Windows
* Fix write newline
* Fix PTY write newline
* Fix CloseReader
* Fix compilation on Windows
* Fix linting error
* Remove bubbletea
* Cleanup readwriter
* Use embedded templates instead of serving over API
* Move templates to examples
* Improve workspace create flow
* Fix Windows build
* Fix tests
* Fix linting errors
* Fix untar with extracting max size
* Fix newline char
53 lines
1.4 KiB
Go
53 lines
1.4 KiB
Go
package codersdk
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
const (
|
|
ContentTypeTar = "application/x-tar"
|
|
)
|
|
|
|
// UploadResponse contains the hash to reference the uploaded file.
|
|
type UploadResponse struct {
|
|
Hash string `json:"hash"`
|
|
}
|
|
|
|
// 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) {
|
|
r.Header.Set("Content-Type", contentType)
|
|
})
|
|
if err != nil {
|
|
return UploadResponse{}, err
|
|
}
|
|
defer res.Body.Close()
|
|
if res.StatusCode != http.StatusCreated && res.StatusCode != http.StatusOK {
|
|
return UploadResponse{}, readBodyAsError(res)
|
|
}
|
|
var resp UploadResponse
|
|
return resp, json.NewDecoder(res.Body).Decode(&resp)
|
|
}
|
|
|
|
// Download fetches a file by uploaded hash.
|
|
func (c *Client) Download(ctx context.Context, hash string) ([]byte, string, error) {
|
|
res, err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/files/%s", hash), nil)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
defer res.Body.Close()
|
|
if res.StatusCode != http.StatusOK {
|
|
return nil, "", readBodyAsError(res)
|
|
}
|
|
data, err := io.ReadAll(res.Body)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
return data, res.Header.Get("Content-Type"), nil
|
|
}
|