X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/f69605f9427aa401b0f83c1e131e3c455eae4e38..e065d5863b9b36c1cd221f676baffa57e20e7498:/sdk/go/arvados/client.go diff --git a/sdk/go/arvados/client.go b/sdk/go/arvados/client.go index 5c6a7b52f4..5ec828667f 100644 --- a/sdk/go/arvados/client.go +++ b/sdk/go/arvados/client.go @@ -9,6 +9,7 @@ import ( "context" "crypto/tls" "encoding/json" + "errors" "fmt" "io" "io/ioutil" @@ -67,6 +68,10 @@ type Client struct { dd *DiscoveryDocument defaultRequestID string + + // APIHost and AuthToken were loaded from ARVADOS_* env vars + // (used to customize "no host/token" error messages) + loadedFromEnv bool } // InsecureHTTPClient is the default http.Client used by a Client with @@ -123,6 +128,7 @@ func NewClientFromEnv() *Client { Insecure: insecure, KeepServiceURIs: svcs, Timeout: 5 * time.Minute, + loadedFromEnv: true, } } @@ -211,6 +217,8 @@ func (c *Client) DoAndDecode(dst interface{}, req *http.Request) error { return err } switch { + case resp.StatusCode == http.StatusNoContent: + return nil case resp.StatusCode == http.StatusOK && dst == nil: return nil case resp.StatusCode == http.StatusOK: @@ -306,11 +314,18 @@ func (c *Client) RequestAndDecode(dst interface{}, method, path string, body io. return c.RequestAndDecodeContext(context.Background(), dst, method, path, body, params) } +// RequestAndDecodeContext does the same as RequestAndDecode, but with a context func (c *Client) RequestAndDecodeContext(ctx context.Context, dst interface{}, method, path string, body io.Reader, params interface{}) error { if body, ok := body.(io.Closer); ok { // Ensure body is closed even if we error out early defer body.Close() } + if c.APIHost == "" { + if c.loadedFromEnv { + return errors.New("ARVADOS_API_HOST and/or ARVADOS_API_TOKEN environment variables are not set") + } + return errors.New("arvados.Client cannot perform request: APIHost is not set") + } urlString := c.apiURL(path) urlValues, err := anythingToValues(params) if err != nil {