From: Peter Amstutz Date: Thu, 1 Nov 2018 15:04:01 +0000 (-0400) Subject: 14262: Revert changes to client.go X-Git-Tag: 1.3.0~55^2~2 X-Git-Url: https://git.arvados.org/arvados.git/commitdiff_plain/6a7a7920e8ce4b6f6743d0a644afb87e6bae63c1 14262: Revert changes to client.go Arvados-DCO-1.1-Signed-off-by: Peter Amstutz --- diff --git a/sdk/go/arvados/client.go b/sdk/go/arvados/client.go index 254a0fa7d3..cca9f9bf1b 100644 --- a/sdk/go/arvados/client.go +++ b/sdk/go/arvados/client.go @@ -103,7 +103,7 @@ var reqIDGen = httpserver.IDGenerator{Prefix: "req-"} // (*http.Client)Do(). func (c *Client) Do(req *http.Request) (*http.Response, error) { if c.AuthToken != "" { - req.Header.Set("Authorization", "OAuth2 "+c.AuthToken) + req.Header.Add("Authorization", "OAuth2 "+c.AuthToken) } if req.Header.Get("X-Request-Id") == "" { @@ -193,62 +193,37 @@ func anythingToValues(params interface{}) (url.Values, error) { return urlValues, nil } -func (c *Client) MakeRequest(method, path string, body io.Reader, params interface{}) (*http.Request, error) { +// RequestAndDecode performs an API request and unmarshals the +// response (which must be JSON) into dst. Method and body arguments +// are the same as for http.NewRequest(). The given path is added to +// the server's scheme/host/port to form the request URL. The given +// params are passed via POST form or query string. +// +// 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 { - return nil, err + return err } if (method == "GET" || body != nil) && urlValues != nil { // FIXME: what if params don't fit in URL u, err := url.Parse(urlString) if err != nil { - return nil, err + return err } u.RawQuery = urlValues.Encode() urlString = u.String() } req, err := http.NewRequest(method, urlString, body) - if err != nil { - return nil, err - } - req.Header.Set("Content-type", "application/x-www-form-urlencoded") - - if c.AuthToken != "" { - req.Header.Set("Authorization", "OAuth2 "+c.AuthToken) - } - - if req.Header.Get("X-Request-Id") == "" { - reqid, _ := c.context().Value(contextKeyRequestID).(string) - if reqid == "" { - reqid = reqIDGen.Next() - } - if req.Header == nil { - req.Header = http.Header{"X-Request-Id": {reqid}} - } else { - req.Header.Set("X-Request-Id", reqid) - } - } - - return req, nil -} - -// RequestAndDecode performs an API request and unmarshals the -// response (which must be JSON) into dst. Method and body arguments -// are the same as for http.NewRequest(). The given path is added to -// the server's scheme/host/port to form the request URL. The given -// params are passed via POST form or query string. -// -// 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() - } - req, err := c.MakeRequest(method, path, body, params) if err != nil { return err } + req.Header.Set("Content-type", "application/x-www-form-urlencoded") return c.DoAndDecode(dst, req) }