X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/3b3c3a0869c2cf528b9e1c45c969bbbd47f6446e..0c888bcc93b559339c8abbce784bdcc44746bca2:/sdk/go/arvados/client.go diff --git a/sdk/go/arvados/client.go b/sdk/go/arvados/client.go index d7eb811b8a..a38d95c2e6 100644 --- a/sdk/go/arvados/client.go +++ b/sdk/go/arvados/client.go @@ -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: