10666: Merge branch 'master' into 10666-report-version
[arvados.git] / sdk / go / arvados / client.go
index d7eb811b8a7a26a08931b07b9c66fcfec83d78a3..a38d95c2e68ee90e1d9f0d41bdef2b341127fd27 100644 (file)
@@ -1,6 +1,11 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: Apache-2.0
+
 package arvados
 
 import (
+       "bytes"
        "crypto/tls"
        "encoding/json"
        "fmt"
@@ -176,6 +181,10 @@ func anythingToValues(params interface{}) (url.Values, error) {
 //
 // path must not contain a query string.
 func (c *Client) RequestAndDecode(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()
+       }
        urlString := c.apiURL(path)
        urlValues, err := anythingToValues(params)
        if err != nil {
@@ -198,6 +207,24 @@ func (c *Client) RequestAndDecode(dst interface{}, method, path string, body io.
        return c.DoAndDecode(dst, req)
 }
 
+type resource interface {
+       resourceName() string
+}
+
+// UpdateBody returns an io.Reader suitable for use as an http.Request
+// Body for a create or update API call.
+func (c *Client) UpdateBody(rsc resource) io.Reader {
+       j, err := json.Marshal(rsc)
+       if err != nil {
+               // Return a reader that returns errors.
+               r, w := io.Pipe()
+               w.CloseWithError(err)
+               return r
+       }
+       v := url.Values{rsc.resourceName(): {string(j)}}
+       return bytes.NewBufferString(v.Encode())
+}
+
 func (c *Client) httpClient() *http.Client {
        switch {
        case c.Client != nil: