createSerializers creates all necessary serializers for given contentType. TODO: the negotiated serializer passed to this method should probably return serializers that control decoding and versioning without this package being aware of the types. Depends on whether RESTClient must deal with generic
(config ContentConfig)
| 159 | // being aware of the types. Depends on whether RESTClient must deal with |
| 160 | // generic infrastructure. |
| 161 | func createSerializers(config ContentConfig) (*Serializers, error) { |
| 162 | mediaTypes := config.NegotiatedSerializer.SupportedMediaTypes() |
| 163 | contentType := config.ContentType |
| 164 | mediaType, _, err := mime.ParseMediaType(contentType) |
| 165 | if err != nil { |
| 166 | return nil, fmt.Errorf("the content type specified in the client configuration is not recognized: %v", err) |
| 167 | } |
| 168 | info, ok := runtime.SerializerInfoForMediaType(mediaTypes, mediaType) |
| 169 | if !ok { |
| 170 | if len(contentType) != 0 || len(mediaTypes) == 0 { |
| 171 | return nil, fmt.Errorf("no serializers registered for %s", contentType) |
| 172 | } |
| 173 | info = mediaTypes[0] |
| 174 | } |
| 175 | |
| 176 | internalGV := schema.GroupVersions{ |
| 177 | { |
| 178 | Group: config.GroupVersion.Group, |
| 179 | Version: runtime.APIVersionInternal, |
| 180 | }, |
| 181 | // always include the legacy group as a decoding target to handle non-error `Status` return types |
| 182 | { |
| 183 | Group: "", |
| 184 | Version: runtime.APIVersionInternal, |
| 185 | }, |
| 186 | } |
| 187 | |
| 188 | s := &Serializers{ |
| 189 | Encoder: config.NegotiatedSerializer.EncoderForVersion(info.Serializer, *config.GroupVersion), |
| 190 | Decoder: config.NegotiatedSerializer.DecoderToVersion(info.Serializer, internalGV), |
| 191 | |
| 192 | RenegotiatedDecoder: func(contentType string, params map[string]string) (runtime.Decoder, error) { |
| 193 | info, ok := runtime.SerializerInfoForMediaType(mediaTypes, contentType) |
| 194 | if !ok { |
| 195 | return nil, fmt.Errorf("serializer for %s not registered", contentType) |
| 196 | } |
| 197 | return config.NegotiatedSerializer.DecoderToVersion(info.Serializer, internalGV), nil |
| 198 | }, |
| 199 | } |
| 200 | if info.StreamSerializer != nil { |
| 201 | s.StreamingSerializer = info.StreamSerializer.Serializer |
| 202 | s.Framer = info.StreamSerializer.Framer |
| 203 | } |
| 204 | |
| 205 | return s, nil |
| 206 | } |
| 207 | |
| 208 | // Verb begins a request with a verb (GET, POST, PUT, DELETE). |
| 209 | // |