From c0a9871d4815c374be7588022fa4846bb71dafdf Mon Sep 17 00:00:00 2001 From: Matt Bell Date: Tue, 28 Oct 2014 22:19:48 -0700 Subject: [PATCH] commands/http: Made HTTP client unmarshal response values based on the Command#Type --- commands/http/client.go | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/commands/http/client.go b/commands/http/client.go index f9505cee8..4908f8da7 100644 --- a/commands/http/client.go +++ b/commands/http/client.go @@ -1,8 +1,8 @@ package http import ( - //"encoding/json" - //"fmt" + "encoding/json" + "fmt" "net/http" "strings" @@ -28,6 +28,16 @@ func Send(req cmds.Request) (cmds.Response, error) { url := "http://" + host + ApiPath url += "/" + strings.Join(req.Path(), "/") + var userEncoding string + if enc, found := req.Option(cmds.EncShort); found { + userEncoding = enc.(string) + req.SetOption(cmds.EncShort, cmds.JSON) + } else { + enc, _ := req.Option(cmds.EncLong) + userEncoding = enc.(string) + req.SetOption(cmds.EncLong, cmds.JSON) + } + query := "?" for k, v := range req.Options() { query += "&" + k + "=" + v.(string) @@ -41,11 +51,7 @@ func Send(req cmds.Request) (cmds.Response, error) { return nil, err } - // commented out: code to parse HTTP response and turn it into a cmds.Response - // for now, we are simply reading the data as a stream res := cmds.NewResponse(req) - res.SetValue(httpRes.Body) - /*res := cmds.NewResponse(req) contentType := httpRes.Header["Content-Type"][0] contentType = strings.Split(contentType, ";")[0] @@ -55,7 +61,6 @@ func Send(req cmds.Request) (cmds.Response, error) { return res, nil } - // TODO: decode based on `encoding`, using multicodec dec := json.NewDecoder(httpRes.Body) if httpRes.StatusCode >= http.StatusBadRequest { @@ -69,7 +74,7 @@ func Send(req cmds.Request) (cmds.Response, error) { res.SetError(e, e.Code) } else { - var v interface{} + v := req.Command().Type err = dec.Decode(&v) if err != nil { fmt.Println(err) @@ -77,7 +82,12 @@ func Send(req cmds.Request) (cmds.Response, error) { } res.SetValue(v) - }*/ + } + + if len(userEncoding) > 0 { + req.SetOption(cmds.EncShort, userEncoding) + req.SetOption(cmds.EncLong, userEncoding) + } return res, nil }