Skip to content

Commit

Permalink
Client renegotiation
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtek-t committed May 12, 2016
1 parent b79a1bb commit e48116b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
8 changes: 8 additions & 0 deletions pkg/client/restclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type Serializers struct {
Decoder runtime.Decoder
StreamingSerializer runtime.Serializer
Framer runtime.Framer
RenegotiatedDecoder func(contentType string, params map[string]string) (runtime.Decoder, error)
}

// NewRESTClient creates a new RESTClient. This client performs generic REST functions
Expand Down Expand Up @@ -156,6 +157,13 @@ func createSerializers(config ContentConfig) (*Serializers, error) {
Decoder: negotiated.DecoderToVersion(info.Serializer, internalGV),
StreamingSerializer: streamInfo.Serializer,
Framer: streamInfo.Framer,
RenegotiatedDecoder: func(contentType string, params map[string]string) (runtime.Decoder, error) {
renegotiated, ok := negotiated.SerializerForMediaType(contentType, params)
if !ok {
return nil, fmt.Errorf("serializer for %s not registered", contentType)
}
return negotiated.DecoderToVersion(renegotiated.Serializer, internalGV), nil
},
}, nil
}

Expand Down
30 changes: 27 additions & 3 deletions pkg/client/restclient/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -910,12 +910,30 @@ func (r *Request) transformResponse(resp *http.Response, req *http.Request) Resu
return Result{err: errors.FromObject(status)}
}

// TODO: Check ContentType.
contentType := resp.Header.Get("Content-Type")
var decoder runtime.Decoder
if contentType == r.content.ContentType {
decoder = r.serializers.Decoder
} else {
mediaType, params, err := mime.ParseMediaType(contentType)
if err != nil {
return Result{err: errors.NewInternalError(err)}
}
decoder, err = r.serializers.RenegotiatedDecoder(mediaType, params)
if err != nil {
return Result{
body: body,
contentType: contentType,
statusCode: resp.StatusCode,
}
}
}

return Result{
body: body,
contentType: resp.Header.Get("Content-Type"),
contentType: contentType,
statusCode: resp.StatusCode,
decoder: r.serializers.Decoder,
decoder: decoder,
}
}

Expand Down Expand Up @@ -1021,6 +1039,9 @@ func (r Result) Get() (runtime.Object, error) {
if r.err != nil {
return nil, r.err
}
if r.decoder == nil {
return nil, fmt.Errorf("serializer for %s doesn't exist", r.contentType)
}
return runtime.Decode(r.decoder, r.body)
}

Expand All @@ -1036,6 +1057,9 @@ func (r Result) Into(obj runtime.Object) error {
if r.err != nil {
return r.err
}
if r.decoder == nil {
return fmt.Errorf("serializer for %s doesn't exist", r.contentType)
}
return runtime.DecodeInto(r.decoder, r.body, obj)
}

Expand Down

0 comments on commit e48116b

Please sign in to comment.