Use codec to encode/decode api objects in client and kubecfg parser

This commit is contained in:
csrwng
2014-08-29 13:53:14 -04:00
parent d7f84aef82
commit 6551f4e0f0
6 changed files with 19 additions and 16 deletions

View File

@@ -99,7 +99,7 @@ type Client struct {
// to a URL will prepend the server path. Returns an error if host cannot be converted to a
// valid URL.
func New(host string, auth *AuthInfo) (*Client, error) {
restClient, err := NewRESTClient(host, auth, "/api/v1beta1/")
restClient, err := NewRESTClient(host, auth, "/api/v1beta1/", runtime.DefaultCodec)
if err != nil {
return nil, err
}
@@ -143,11 +143,12 @@ type RESTClient struct {
Sync bool
PollPeriod time.Duration
Timeout time.Duration
Codec runtime.Codec
}
// NewRESTClient creates a new RESTClient. This client performs generic REST functions
// such as Get, Put, Post, and Delete on specified paths.
func NewRESTClient(host string, auth *AuthInfo, path string) (*RESTClient, error) {
func NewRESTClient(host string, auth *AuthInfo, path string, c runtime.Codec) (*RESTClient, error) {
prefix, err := normalizePrefix(host, path)
if err != nil {
return nil, err
@@ -171,6 +172,7 @@ func NewRESTClient(host string, auth *AuthInfo, path string) (*RESTClient, error
Sync: false,
PollPeriod: time.Second * 2,
Timeout: time.Second * 20,
Codec: c,
}, nil
}

View File

@@ -48,7 +48,7 @@ func TestValidatesHostParameter(t *testing.T) {
"host/server": {"", "", true},
}
for k, expected := range testCases {
c, err := NewRESTClient(k, nil, "/api/v1beta1/")
c, err := NewRESTClient(k, nil, "/api/v1beta1/", runtime.DefaultCodec)
switch {
case err == nil && expected.Err:
t.Errorf("expected error but was nil")

View File

@@ -206,7 +206,7 @@ func (r *Request) Body(obj interface{}) *Request {
case io.Reader:
r.body = t
case runtime.Object:
data, err := runtime.DefaultCodec.Encode(t)
data, err := r.c.Codec.Encode(t)
if err != nil {
r.err = err
return r
@@ -301,14 +301,15 @@ func (r *Request) Do() Result {
}
}
}
return Result{respBody, err}
return Result{respBody, err, r.c.Codec}
}
}
// Result contains the result of calling Request.Do().
type Result struct {
body []byte
err error
body []byte
err error
codec runtime.Codec
}
// Raw returns the raw result.
@@ -321,7 +322,7 @@ func (r Result) Get() (runtime.Object, error) {
if r.err != nil {
return nil, r.err
}
return runtime.DefaultCodec.Decode(r.body)
return r.codec.Decode(r.body)
}
// Into stores the result into obj, if possible.
@@ -329,7 +330,7 @@ func (r Result) Into(obj runtime.Object) error {
if r.err != nil {
return r.err
}
return runtime.DefaultCodec.DecodeInto(r.body, obj)
return r.codec.DecodeInto(r.body, obj)
}
// Error returns the error executing the request, nil if no error occurred.