Rename write -> writeJSON

This commit is contained in:
Clayton Coleman 2014-07-29 18:14:00 -04:00
parent b84c9f7a1c
commit 9069696514

View File

@ -98,8 +98,8 @@ func New(storage map[string]RESTStorage, prefix string) *APIServer {
}
// handleVersionReq writes the server's version information.
func (server *APIServer) handleVersionReq(w http.ResponseWriter, req *http.Request) {
server.writeRawJSON(http.StatusOK, version.Get(), w)
func (s *APIServer) handleVersionReq(w http.ResponseWriter, req *http.Request) {
writeRawJSON(http.StatusOK, version.Get(), w)
}
// HTTP Handler interface
@ -145,30 +145,6 @@ func (s *APIServer) handleREST(w http.ResponseWriter, req *http.Request) {
s.handleRESTStorage(requestParts, req, w, storage)
}
// write writes an API object in wire format.
func (s *APIServer) write(statusCode int, object interface{}, w http.ResponseWriter) {
output, err := api.Encode(object)
if err != nil {
internalError(err, w)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
w.Write(output)
}
// writeRawJSON writes a non-API object in JSON.
func (s *APIServer) writeRawJSON(statusCode int, object interface{}, w http.ResponseWriter) {
output, err := json.Marshal(object)
if err != nil {
internalError(err, w)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
w.Write(output)
}
// finishReq finishes up a request, waiting until the operation finishes or, after a timeout, creating an
// Operation to receive the result and returning its ID down the writer.
func (s *APIServer) finishReq(out <-chan interface{}, sync bool, timeout time.Duration, w http.ResponseWriter) {
@ -190,9 +166,9 @@ func (s *APIServer) finishReq(out <-chan interface{}, sync bool, timeout time.Du
status = stat.Code
}
}
s.write(status, obj, w)
writeJSON(status, obj, w)
} else {
s.write(http.StatusAccepted, obj, w)
writeJSON(http.StatusAccepted, obj, w)
}
}
@ -226,7 +202,7 @@ func (s *APIServer) handleRESTStorage(parts []string, req *http.Request, w http.
internalError(err, w)
return
}
s.write(http.StatusOK, list, w)
writeJSON(http.StatusOK, list, w)
case 2:
item, err := storage.Get(parts[1])
if IsNotFound(err) {
@ -237,7 +213,7 @@ func (s *APIServer) handleRESTStorage(parts []string, req *http.Request, w http.
internalError(err, w)
return
}
s.write(http.StatusOK, item, w)
writeJSON(http.StatusOK, item, w)
default:
notFound(w, req)
}
@ -342,7 +318,7 @@ func (s *APIServer) handleOperationRequest(w http.ResponseWriter, req *http.Requ
if len(parts) == 0 {
// List outstanding operations.
list := s.ops.List()
s.write(http.StatusOK, list, w)
writeJSON(http.StatusOK, list, w)
return
}
@ -354,9 +330,9 @@ func (s *APIServer) handleOperationRequest(w http.ResponseWriter, req *http.Requ
obj, complete := op.StatusOrResult()
if complete {
s.write(http.StatusOK, obj, w)
writeJSON(http.StatusOK, obj, w)
} else {
s.write(http.StatusAccepted, obj, w)
writeJSON(http.StatusAccepted, obj, w)
}
}
@ -405,6 +381,29 @@ func (s *APIServer) handleWatch(w http.ResponseWriter, req *http.Request) {
notFound(w, req)
}
func writeJSON(statusCode int, object interface{}, w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
output, err := api.Encode(object)
if err != nil {
internalError(err, w)
return
}
w.Write(output)
}
// writeRawJSON writes a non-API object in JSON.
func writeRawJSON(statusCode int, object interface{}, w http.ResponseWriter) {
output, err := json.Marshal(object)
if err != nil {
internalError(err, w)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
w.Write(output)
}
func parseTimeout(str string) time.Duration {
if str != "" {
timeout, err := time.ParseDuration(str)