http endpoint to accept approvals

pull/157/head
Karolis Rusenas 2018-03-11 13:13:54 +00:00
parent 22664fc74c
commit b35e0dab93
1 changed files with 46 additions and 0 deletions

View File

@ -5,9 +5,15 @@ import (
"fmt"
"net/http"
"github.com/keel-hq/keel/cache"
"github.com/keel-hq/keel/types"
)
type approveRequest struct {
Identifier string `json:"identifier"`
Voter string `json:"voter"`
}
func (s *TriggerServer) approvalsHandler(resp http.ResponseWriter, req *http.Request) {
// unknown lists all
approvals, err := s.approvalsManager.List()
@ -31,6 +37,46 @@ func (s *TriggerServer) approvalsHandler(resp http.ResponseWriter, req *http.Req
resp.Write(bts)
}
func (s *TriggerServer) approvalApproveHandler(resp http.ResponseWriter, req *http.Request) {
var ar approveRequest
dec := json.NewDecoder(req.Body)
defer req.Body.Close()
err := dec.Decode(&ar)
if err != nil {
fmt.Fprintf(resp, "%s", err)
resp.WriteHeader(http.StatusBadRequest)
return
}
if ar.Identifier == "" {
http.Error(resp, "identifier not supplied", http.StatusBadRequest)
return
}
approval, err := s.approvalsManager.Approve(ar.Identifier, ar.Voter)
if err != nil {
if err == cache.ErrNotFound {
http.Error(resp, fmt.Sprintf("approval '%s' not found", ar.Identifier), http.StatusNotFound)
return
}
fmt.Fprintf(resp, "%s", err)
resp.WriteHeader(http.StatusInternalServerError)
return
}
bts, err := json.Marshal(&approval)
if err != nil {
fmt.Fprintf(resp, "%s", err)
resp.WriteHeader(http.StatusInternalServerError)
return
}
resp.Write(bts)
}
func (s *TriggerServer) approvalDeleteHandler(resp http.ResponseWriter, req *http.Request) {
identifier := getID(req)