Fix a few more golint warnings.
[arvados.git] / sdk / go / arvados / client.go
index 562c8c1e7d7c66528a2ce0874eca034c9eb7b328..13bb3bf80de70c11e4567ab69ea56c9c03b28a8f 100644 (file)
@@ -9,6 +9,7 @@ import (
        "context"
        "crypto/tls"
        "encoding/json"
+       "errors"
        "fmt"
        "io"
        "io/ioutil"
@@ -67,16 +68,20 @@ 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
 }
 
-// The default http.Client used by a Client with Insecure==true and
-// Client==nil.
+// InsecureHTTPClient is the default http.Client used by a Client with
+// Insecure==true and Client==nil.
 var InsecureHTTPClient = &http.Client{
        Transport: &http.Transport{
                TLSClientConfig: &tls.Config{
                        InsecureSkipVerify: true}}}
 
-// The default http.Client used by a Client otherwise.
+// DefaultSecureClient is the default http.Client used by a Client otherwise.
 var DefaultSecureClient = &http.Client{}
 
 // NewClientFromConfig creates a new Client that uses the endpoints in
@@ -123,6 +128,7 @@ func NewClientFromEnv() *Client {
                Insecure:        insecure,
                KeepServiceURIs: svcs,
                Timeout:         5 * time.Minute,
+               loadedFromEnv:   true,
        }
 }
 
@@ -306,11 +312,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 {