aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/docker/go-plugins-helpers/sdk/encoder.go
blob: 195812a446200a1d4e4ce4acf2c54d8c562b0563 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package sdk

import (
	"encoding/json"
	"fmt"
	"io"
	"net/http"
)

// DefaultContentTypeV1_1 is the default content type accepted and sent by the plugins.
const DefaultContentTypeV1_1 = "application/vnd.docker.plugins.v1.1+json"

// DecodeRequest decodes an http request into a given structure.
func DecodeRequest(w http.ResponseWriter, r *http.Request, req interface{}) (err error) {
	if err = json.NewDecoder(r.Body).Decode(req); err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
	}
	return
}

// EncodeResponse encodes the given structure into an http response.
func EncodeResponse(w http.ResponseWriter, res interface{}, err bool) {
	w.Header().Set("Content-Type", DefaultContentTypeV1_1)
	if err {
		w.WriteHeader(http.StatusInternalServerError)
	}
	json.NewEncoder(w).Encode(res)
}

// StreamResponse streams a response object to the client
func StreamResponse(w http.ResponseWriter, data io.ReadCloser) {
	w.Header().Set("Content-Type", DefaultContentTypeV1_1)
	if _, err := copyBuf(w, data); err != nil {
		fmt.Printf("ERROR in stream: %v\n", err)
	}
	data.Close()
}