[proxy] refactoring out common marshalling logic

This commit is contained in:
Paul Bellamy authored and Matthias Radestock committed 2015-09-04 13:40:07 +01:00
1 parent e21a204eed
commit 19ceb424c3
5 files changed
+46 -65

No files matched your search

+39
View File
@@ -40,6 +40,19 @@ func callWeave(args ...string) ([]byte, []byte, error) {
return stdout.Bytes(), stderr.Bytes(), err
}
func unmarshalRequestBody(r *http.Request, target interface{}) error {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
if err := r.Body.Close(); err != nil {
return err
}
r.Body = ioutil.NopCloser(bytes.NewReader(body))
return json.Unmarshal(body, &target)
}
func marshalRequestBody(r *http.Request, body interface{}) error {
newBody, err := json.Marshal(body)
if err != nil {
@@ -50,6 +63,32 @@ func marshalRequestBody(r *http.Request, body interface{}) error {
return nil
}
func unmarshalResponseBody(r *http.Response, target interface{}) error {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
if err := r.Body.Close(); err != nil {
return err
}
r.Body = ioutil.NopCloser(bytes.NewReader(body))
return json.Unmarshal(body, &target)
}
func marshalResponseBody(r *http.Response, body interface{}) error {
newBody, err := json.Marshal(body)
if err != nil {
return err
}
r.Body = ioutil.NopCloser(bytes.NewReader(newBody))
r.ContentLength = int64(len(newBody))
// Stop it being chunked, because that hangs
r.TransferEncoding = nil
return nil
}
func inspectContainerInPath(client *docker.Client, path string) (*docker.Container, error) {
subs := containerIDRegexp.FindStringSubmatch(path)
if subs == nil {