github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/client/unversioned/restclient.go (about)

     1  /*
     2  Copyright 2014 The Kubernetes Authors All rights reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package unversioned
    18  
    19  import (
    20  	"net/http"
    21  	"net/url"
    22  	"strings"
    23  
    24  	"k8s.io/kubernetes/pkg/api"
    25  	"k8s.io/kubernetes/pkg/runtime"
    26  	"k8s.io/kubernetes/pkg/util"
    27  )
    28  
    29  // RESTClient imposes common Kubernetes API conventions on a set of resource paths.
    30  // The baseURL is expected to point to an HTTP or HTTPS path that is the parent
    31  // of one or more resources.  The server should return a decodable API resource
    32  // object, or an api.Status object which contains information about the reason for
    33  // any failure.
    34  //
    35  // Most consumers should use client.New() to get a Kubernetes API client.
    36  type RESTClient struct {
    37  	baseURL *url.URL
    38  	// A string identifying the version of the API this client is expected to use.
    39  	apiVersion string
    40  
    41  	// Codec is the encoding and decoding scheme that applies to a particular set of
    42  	// REST resources.
    43  	Codec runtime.Codec
    44  
    45  	// Set specific behavior of the client.  If not set http.DefaultClient will be
    46  	// used.
    47  	Client *http.Client
    48  
    49  	// TODO extract this into a wrapper interface via the RESTClient interface in kubectl.
    50  	Throttle util.RateLimiter
    51  }
    52  
    53  // NewRESTClient creates a new RESTClient. This client performs generic REST functions
    54  // such as Get, Put, Post, and Delete on specified paths.  Codec controls encoding and
    55  // decoding of responses from the server.
    56  func NewRESTClient(baseURL *url.URL, apiVersion string, c runtime.Codec, maxQPS float32, maxBurst int) *RESTClient {
    57  	base := *baseURL
    58  	if !strings.HasSuffix(base.Path, "/") {
    59  		base.Path += "/"
    60  	}
    61  	base.RawQuery = ""
    62  	base.Fragment = ""
    63  
    64  	var throttle util.RateLimiter
    65  	if maxQPS > 0 {
    66  		throttle = util.NewTokenBucketRateLimiter(maxQPS, maxBurst)
    67  	}
    68  	return &RESTClient{
    69  		baseURL:    &base,
    70  		apiVersion: apiVersion,
    71  		Codec:      c,
    72  		Throttle:   throttle,
    73  	}
    74  }
    75  
    76  // Verb begins a request with a verb (GET, POST, PUT, DELETE).
    77  //
    78  // Example usage of RESTClient's request building interface:
    79  // c := NewRESTClient(url, codec)
    80  // resp, err := c.Verb("GET").
    81  //  Path("pods").
    82  //  SelectorParam("labels", "area=staging").
    83  //  Timeout(10*time.Second).
    84  //  Do()
    85  // if err != nil { ... }
    86  // list, ok := resp.(*api.PodList)
    87  //
    88  func (c *RESTClient) Verb(verb string) *Request {
    89  	if c.Throttle != nil {
    90  		c.Throttle.Accept()
    91  	}
    92  	if c.Client == nil {
    93  		return NewRequest(nil, verb, c.baseURL, c.apiVersion, c.Codec)
    94  	}
    95  	return NewRequest(c.Client, verb, c.baseURL, c.apiVersion, c.Codec)
    96  }
    97  
    98  // Post begins a POST request. Short for c.Verb("POST").
    99  func (c *RESTClient) Post() *Request {
   100  	return c.Verb("POST")
   101  }
   102  
   103  // Put begins a PUT request. Short for c.Verb("PUT").
   104  func (c *RESTClient) Put() *Request {
   105  	return c.Verb("PUT")
   106  }
   107  
   108  // Patch begins a PATCH request. Short for c.Verb("Patch").
   109  func (c *RESTClient) Patch(pt api.PatchType) *Request {
   110  	return c.Verb("PATCH").SetHeader("Content-Type", string(pt))
   111  }
   112  
   113  // Get begins a GET request. Short for c.Verb("GET").
   114  func (c *RESTClient) Get() *Request {
   115  	return c.Verb("GET")
   116  }
   117  
   118  // Delete begins a DELETE request. Short for c.Verb("DELETE").
   119  func (c *RESTClient) Delete() *Request {
   120  	return c.Verb("DELETE")
   121  }
   122  
   123  // APIVersion returns the APIVersion this RESTClient is expected to use.
   124  func (c *RESTClient) APIVersion() string {
   125  	return c.apiVersion
   126  }