diff options
author | OpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com> | 2020-12-01 22:32:08 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-01 22:32:08 +0100 |
commit | 7f084a8ae212f1c2c67e9cfb1f9e4c5470e01870 (patch) | |
tree | 7078a75662049ee596cbfecb96c41ad95cee3aa9 /vendor/github.com/docker/go-plugins-helpers/sdk/encoder.go | |
parent | c71ad9a557ee80eccde2608939c81906379fa7ae (diff) | |
parent | 594ac4a14658a90ad7bc5541dab6349a0c629a5c (diff) | |
download | podman-7f084a8ae212f1c2c67e9cfb1f9e4c5470e01870.tar.gz podman-7f084a8ae212f1c2c67e9cfb1f9e4c5470e01870.tar.bz2 podman-7f084a8ae212f1c2c67e9cfb1f9e4c5470e01870.zip |
Merge pull request #8357 from mheon/add_volume_interface_package
Add API for communicating with Docker volume plugins
Diffstat (limited to 'vendor/github.com/docker/go-plugins-helpers/sdk/encoder.go')
-rw-r--r-- | vendor/github.com/docker/go-plugins-helpers/sdk/encoder.go | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/vendor/github.com/docker/go-plugins-helpers/sdk/encoder.go b/vendor/github.com/docker/go-plugins-helpers/sdk/encoder.go new file mode 100644 index 000000000..195812a44 --- /dev/null +++ b/vendor/github.com/docker/go-plugins-helpers/sdk/encoder.go @@ -0,0 +1,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() +} |