Skip to content

Commit

Permalink
Provide a helper on client/request.go for full URI
Browse files Browse the repository at this point in the history
Allows self links to be directly passed
  • Loading branch information
smarterclayton committed Feb 16, 2015
1 parent 3e2e471 commit 7e07d71
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
35 changes: 29 additions & 6 deletions pkg/client/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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()
Expand Down
14 changes: 13 additions & 1 deletion pkg/client/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 7e07d71

Please sign in to comment.