mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-20 09:33:52 +00:00
Rename write -> writeJSON
This commit is contained in:
@@ -98,8 +98,8 @@ func New(storage map[string]RESTStorage, prefix string) *APIServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// handleVersionReq writes the server's version information.
|
// handleVersionReq writes the server's version information.
|
||||||
func (server *APIServer) handleVersionReq(w http.ResponseWriter, req *http.Request) {
|
func (s *APIServer) handleVersionReq(w http.ResponseWriter, req *http.Request) {
|
||||||
server.writeRawJSON(http.StatusOK, version.Get(), w)
|
writeRawJSON(http.StatusOK, version.Get(), w)
|
||||||
}
|
}
|
||||||
|
|
||||||
// HTTP Handler interface
|
// HTTP Handler interface
|
||||||
@@ -145,30 +145,6 @@ func (s *APIServer) handleREST(w http.ResponseWriter, req *http.Request) {
|
|||||||
s.handleRESTStorage(requestParts, req, w, storage)
|
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
|
// 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.
|
// 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) {
|
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
|
status = stat.Code
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
s.write(status, obj, w)
|
writeJSON(status, obj, w)
|
||||||
} else {
|
} 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)
|
internalError(err, w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
s.write(http.StatusOK, list, w)
|
writeJSON(http.StatusOK, list, w)
|
||||||
case 2:
|
case 2:
|
||||||
item, err := storage.Get(parts[1])
|
item, err := storage.Get(parts[1])
|
||||||
if IsNotFound(err) {
|
if IsNotFound(err) {
|
||||||
@@ -237,7 +213,7 @@ func (s *APIServer) handleRESTStorage(parts []string, req *http.Request, w http.
|
|||||||
internalError(err, w)
|
internalError(err, w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
s.write(http.StatusOK, item, w)
|
writeJSON(http.StatusOK, item, w)
|
||||||
default:
|
default:
|
||||||
notFound(w, req)
|
notFound(w, req)
|
||||||
}
|
}
|
||||||
@@ -342,7 +318,7 @@ func (s *APIServer) handleOperationRequest(w http.ResponseWriter, req *http.Requ
|
|||||||
if len(parts) == 0 {
|
if len(parts) == 0 {
|
||||||
// List outstanding operations.
|
// List outstanding operations.
|
||||||
list := s.ops.List()
|
list := s.ops.List()
|
||||||
s.write(http.StatusOK, list, w)
|
writeJSON(http.StatusOK, list, w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -354,9 +330,9 @@ func (s *APIServer) handleOperationRequest(w http.ResponseWriter, req *http.Requ
|
|||||||
|
|
||||||
obj, complete := op.StatusOrResult()
|
obj, complete := op.StatusOrResult()
|
||||||
if complete {
|
if complete {
|
||||||
s.write(http.StatusOK, obj, w)
|
writeJSON(http.StatusOK, obj, w)
|
||||||
} else {
|
} 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)
|
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 {
|
func parseTimeout(str string) time.Duration {
|
||||||
if str != "" {
|
if str != "" {
|
||||||
timeout, err := time.ParseDuration(str)
|
timeout, err := time.ParseDuration(str)
|
||||||
|
Reference in New Issue
Block a user