From 7e07d711b815e0fff60cf749abc487807d882071 Mon Sep 17 00:00:00 2001 From: Clayton Coleman Date: Mon, 16 Feb 2015 16:29:40 -0500 Subject: [PATCH] Provide a helper on client/request.go for full URI Allows self links to be directly passed --- pkg/client/request.go | 35 +++++++++++++++++++++++++++++------ pkg/client/request_test.go | 14 +++++++++++++- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/pkg/client/request.go b/pkg/client/request.go index ae172e439e692..051f94cb06e64 100644 --- a/pkg/client/request.go +++ b/pkg/client/request.go @@ -90,7 +90,7 @@ type Request struct { // generic components accessible via method setters path string subpath string - params map[string]string + params url.Values // structural elements of the request that are part of the Kubernetes API conventions namespace string @@ -204,6 +204,29 @@ func (r *Request) AbsPath(segments ...string) *Request { return r } +// RequestURI overwrites existing path and parameters with the value of the provided server relative +// URI. Some parameters (those in specialParameters) cannot be overwritten. +func (r *Request) RequestURI(uri string) *Request { + if r.err != nil { + return r + } + locator, err := url.Parse(uri) + if err != nil { + r.err = err + return r + } + r.path = locator.Path + if len(locator.Query()) > 0 { + if r.params == nil { + r.params = make(url.Values) + } + for k, v := range locator.Query() { + r.params[k] = v + } + } + return r +} + // ParseSelectorParam parses the given string as a resource label selector. // This is a convenience function so you don't have to first check that it's a // validly formatted selector. @@ -252,9 +275,9 @@ func (r *Request) setParam(paramName, value string) *Request { return r } if r.params == nil { - r.params = make(map[string]string) + r.params = make(url.Values) } - r.params[paramName] = value + r.params[paramName] = []string{value} return r } @@ -325,16 +348,16 @@ func (r *Request) finalURL() string { query := url.Values{} for key, value := range r.params { - query.Add(key, value) + query[key] = value } if r.namespaceSet && r.namespaceInQuery { - query.Add("namespace", r.namespace) + query.Set("namespace", r.namespace) } // timeout is handled specially here. if r.timeout != 0 { - query.Add("timeout", r.timeout.String()) + query.Set("timeout", r.timeout.String()) } finalURL.RawQuery = query.Encode() return finalURL.String() diff --git a/pkg/client/request_test.go b/pkg/client/request_test.go index f4bdafe9538e0..f23cbef97a7f5 100644 --- a/pkg/client/request_test.go +++ b/pkg/client/request_test.go @@ -148,7 +148,19 @@ func TestRequestParseSelectorParam(t *testing.T) { func TestRequestParam(t *testing.T) { r := (&Request{}).Param("foo", "a") - if !api.Semantic.DeepDerivative(r.params, map[string]string{"foo": "a"}) { + if !api.Semantic.DeepDerivative(r.params, url.Values{"foo": []string{"a"}}) { + t.Errorf("should have set a param: %#v", r) + } +} + +func TestRequestURI(t *testing.T) { + r := (&Request{}).Param("foo", "a") + r.Prefix("other") + r.RequestURI("/test?foo=b&a=b") + if r.path != "/test" { + t.Errorf("path is wrong: %#v", r) + } + if !api.Semantic.DeepDerivative(r.params, url.Values{"a": []string{"b"}, "foo": []string{"b"}}) { t.Errorf("should have set a param: %#v", r) } }