api: use only one layer envelope

This commit is contained in:
Jimmy Zelinskie 2016-01-29 15:19:23 -05:00
parent fa45d516df
commit 1a5aa88b18

View File

@ -18,7 +18,6 @@ import (
"encoding/json" "encoding/json"
"io" "io"
"net/http" "net/http"
"net/url"
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
@ -37,10 +36,6 @@ func decodeJSON(r *http.Request, v interface{}) error {
return json.NewDecoder(io.LimitReader(r.Body, maxBodySize)).Decode(v) return json.NewDecoder(io.LimitReader(r.Body, maxBodySize)).Decode(v)
} }
func writeError(w http.ResponseWriter, err error) {
writeResponse(w, ErrorResponse{Error{err.Error()}})
}
func writeResponse(w io.Writer, resp interface{}) { func writeResponse(w io.Writer, resp interface{}) {
err := json.NewEncoder(w).Encode(resp) err := json.NewEncoder(w).Encode(resp)
if err != nil { if err != nil {
@ -54,20 +49,20 @@ func writeHeader(w http.ResponseWriter, status int) int {
} }
func postLayer(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) int { func postLayer(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) int {
request := LayerRequest{} request := LayerEnvelope{}
err := decodeJSON(r, &request) err := decodeJSON(r, &request)
if err != nil { if err != nil {
writeError(w, err) writeResponse(w, LayerEnvelope{Error: Error{err.Error()}})
return writeHeader(w, http.StatusBadRequest) return writeHeader(w, http.StatusBadRequest)
} }
err = worker.Process(ctx.Store, request.Layer.Name, request.Layer.ParentName, request.Layer.Path, request.Layer.Format) err = worker.Process(ctx.Store, request.Layer.Name, request.Layer.ParentName, request.Layer.Path, request.Layer.Format)
if err != nil { if err != nil {
if _, ok := err.(*cerrors.ErrBadRequest); ok { if _, ok := err.(*cerrors.ErrBadRequest); ok {
writeError(w, err) writeResponse(w, LayerEnvelope{Error: Error{err.Error()}})
return writeHeader(w, http.StatusBadRequest) return writeHeader(w, http.StatusBadRequest)
} }
writeError(w, err) writeResponse(w, LayerEnvelope{Error: Error{err.Error()}})
return writeHeader(w, http.StatusInternalServerError) return writeHeader(w, http.StatusInternalServerError)
} }
@ -75,21 +70,15 @@ func postLayer(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx
} }
func getLayer(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) int { func getLayer(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) int {
parsedQuery, err := url.ParseQuery(r.URL.RawQuery) _, withFeatures := r.URL.Query()["features"]
if err != nil { _, withVulnerabilities := r.URL.Query()["vulnerabilities"]
writeError(w, err)
return writeHeader(w, http.StatusBadRequest)
}
withFeatures := parsedQuery.Get("features") != ""
withVulnerabilities := parsedQuery.Get("vulnerabilities") != ""
dbLayer, err := ctx.Store.FindLayer(p.ByName("layerName"), withFeatures, withVulnerabilities) dbLayer, err := ctx.Store.FindLayer(p.ByName("layerName"), withFeatures, withVulnerabilities)
if err == cerrors.ErrNotFound { if err == cerrors.ErrNotFound {
writeError(w, err) writeResponse(w, LayerEnvelope{Error: Error{err.Error()}})
return writeHeader(w, http.StatusNotFound) return writeHeader(w, http.StatusNotFound)
} else if err != nil { } else if err != nil {
writeError(w, err) writeResponse(w, LayerEnvelope{Error: Error{err.Error()}})
return writeHeader(w, http.StatusInternalServerError) return writeHeader(w, http.StatusInternalServerError)
} }
@ -131,7 +120,8 @@ func getLayer(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *
} }
} }
writeResponse(w, layer) // add envelope
writeResponse(w, LayerEnvelope{Layer: layer})
return writeHeader(w, http.StatusOK) return writeHeader(w, http.StatusOK)
} }