Commit c0a9871d authored by Matt Bell's avatar Matt Bell Committed by Juan Batiz-Benet

commands/http: Made HTTP client unmarshal response values based on the Command#Type

parent ef290faa
package http package http
import ( import (
//"encoding/json" "encoding/json"
//"fmt" "fmt"
"net/http" "net/http"
"strings" "strings"
...@@ -28,6 +28,16 @@ func Send(req cmds.Request) (cmds.Response, error) { ...@@ -28,6 +28,16 @@ func Send(req cmds.Request) (cmds.Response, error) {
url := "http://" + host + ApiPath url := "http://" + host + ApiPath
url += "/" + strings.Join(req.Path(), "/") 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 := "?" query := "?"
for k, v := range req.Options() { for k, v := range req.Options() {
query += "&" + k + "=" + v.(string) query += "&" + k + "=" + v.(string)
...@@ -41,11 +51,7 @@ func Send(req cmds.Request) (cmds.Response, error) { ...@@ -41,11 +51,7 @@ func Send(req cmds.Request) (cmds.Response, error) {
return nil, err 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 := cmds.NewResponse(req)
res.SetValue(httpRes.Body)
/*res := cmds.NewResponse(req)
contentType := httpRes.Header["Content-Type"][0] contentType := httpRes.Header["Content-Type"][0]
contentType = strings.Split(contentType, ";")[0] contentType = strings.Split(contentType, ";")[0]
...@@ -55,7 +61,6 @@ func Send(req cmds.Request) (cmds.Response, error) { ...@@ -55,7 +61,6 @@ func Send(req cmds.Request) (cmds.Response, error) {
return res, nil return res, nil
} }
// TODO: decode based on `encoding`, using multicodec
dec := json.NewDecoder(httpRes.Body) dec := json.NewDecoder(httpRes.Body)
if httpRes.StatusCode >= http.StatusBadRequest { if httpRes.StatusCode >= http.StatusBadRequest {
...@@ -69,7 +74,7 @@ func Send(req cmds.Request) (cmds.Response, error) { ...@@ -69,7 +74,7 @@ func Send(req cmds.Request) (cmds.Response, error) {
res.SetError(e, e.Code) res.SetError(e, e.Code)
} else { } else {
var v interface{} v := req.Command().Type
err = dec.Decode(&v) err = dec.Decode(&v)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
...@@ -77,7 +82,12 @@ func Send(req cmds.Request) (cmds.Response, error) { ...@@ -77,7 +82,12 @@ func Send(req cmds.Request) (cmds.Response, error) {
} }
res.SetValue(v) res.SetValue(v)
}*/ }
if len(userEncoding) > 0 {
req.SetOption(cmds.EncShort, userEncoding)
req.SetOption(cmds.EncLong, userEncoding)
}
return res, nil return res, nil
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment