Provide a helper on client/request.go for full URI

Allows self links to be directly passed
This commit is contained in:
Clayton Coleman 2015-02-16 16:29:40 -05:00
parent 3e2e4714a2
commit 7e07d711b8
2 changed files with 42 additions and 7 deletions

View File

@ -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()

View File

@ -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)
}
}