// writeNegotiated renders an object in the content type negotiated by the client func writeNegotiated(s runtime.NegotiatedSerializer, gv unversioned.GroupVersion, w http.ResponseWriter, req *http.Request, statusCode int, object runtime.Object) { serializer, err := negotiateOutputSerializer(req, s) if err != nil { status := errToAPIStatus(err) writeRawJSON(int(status.Code), status, w) return } w.Header().Set("Content-Type", serializer.MediaType) w.WriteHeader(statusCode) encoder := s.EncoderForVersion(serializer, gv) if err := encoder.Encode(object, w); err != nil { errorJSONFatal(err, encoder, w) } }
// Adds a service to return the supported resources, E.g., a such web service // will be registered at /apis/extensions/v1. func AddSupportedResourcesWebService(s runtime.NegotiatedSerializer, ws *restful.WebService, groupVersion unversioned.GroupVersion, apiResources []unversioned.APIResource) { ss := s if keepUnversioned(groupVersion.Group) { // Because in release 1.1, /apis/extensions/v1beta1 returns response // with empty APIVersion, we use StripVersionNegotiatedSerializer to // keep the response backwards compatible. ss = StripVersionNegotiatedSerializer{s} } resourceHandler := SupportedResourcesHandler(ss, groupVersion, apiResources) ws.Route(ws.GET("/").To(resourceHandler). Doc("get available resources"). Operation("getAPIResources"). Produces(s.SupportedMediaTypes()...). Consumes(s.SupportedMediaTypes()...). Writes(unversioned.APIResourceList{})) }
// Adds a service to return the supported api versions at /apis. func AddApisWebService(s runtime.NegotiatedSerializer, container *restful.Container, apiPrefix string, f func(req *restful.Request) []unversioned.APIGroup) { // Because in release 1.1, /apis returns response with empty APIVersion, we // use StripVersionNegotiatedSerializer to keep the response backwards // compatible. ss := StripVersionNegotiatedSerializer{s} rootAPIHandler := RootAPIHandler(ss, f) ws := new(restful.WebService) ws.Path(apiPrefix) ws.Doc("get available API versions") ws.Route(ws.GET("/").To(rootAPIHandler). Doc("get available API versions"). Operation("getAPIVersions"). Produces(s.SupportedMediaTypes()...). Consumes(s.SupportedMediaTypes()...). Writes(unversioned.APIGroupList{})) container.Add(ws) }
// Adds a service to return the supported api versions at the legacy /api. func AddApiWebService(s runtime.NegotiatedSerializer, container *restful.Container, apiPrefix string, getAPIVersionsFunc func(req *restful.Request) *unversioned.APIVersions) { // TODO: InstallREST should register each version automatically // Because in release 1.1, /api returns response with empty APIVersion, we // use StripVersionNegotiatedSerializer to keep the response backwards // compatible. ss := StripVersionNegotiatedSerializer{s} versionHandler := APIVersionHandler(ss, getAPIVersionsFunc) ws := new(restful.WebService) ws.Path(apiPrefix) ws.Doc("get available API versions") ws.Route(ws.GET("/").To(versionHandler). Doc("get available API versions"). Operation("getAPIVersions"). Produces(s.SupportedMediaTypes()...). Consumes(s.SupportedMediaTypes()...). Writes(unversioned.APIVersions{})) container.Add(ws) }
// Adds a service to return the supported versions, preferred version, and name // of a group. E.g., a such web service will be registered at /apis/extensions. func AddGroupWebService(s runtime.NegotiatedSerializer, container *restful.Container, path string, group unversioned.APIGroup) { ss := s if keepUnversioned(group.Name) { // Because in release 1.1, /apis/extensions returns response with empty // APIVersion, we use StripVersionNegotiatedSerializer to keep the // response backwards compatible. ss = StripVersionNegotiatedSerializer{s} } groupHandler := GroupHandler(ss, group) ws := new(restful.WebService) ws.Path(path) ws.Doc("get information of a group") ws.Route(ws.GET("/").To(groupHandler). Doc("get information of a group"). Operation("getAPIGroup"). Produces(s.SupportedMediaTypes()...). Consumes(s.SupportedMediaTypes()...). Writes(unversioned.APIGroup{})) container.Add(ws) }