...
1
16
17
18
19 package fake
20
21 import (
22 "net/http"
23 "net/url"
24
25 "k8s.io/apimachinery/pkg/runtime"
26 "k8s.io/apimachinery/pkg/runtime/schema"
27 "k8s.io/apimachinery/pkg/types"
28 restclient "k8s.io/client-go/rest"
29 "k8s.io/client-go/util/flowcontrol"
30 )
31
32
33
34 func CreateHTTPClient(roundTripper func(*http.Request) (*http.Response, error)) *http.Client {
35 return &http.Client{
36 Transport: roundTripperFunc(roundTripper),
37 }
38 }
39
40 type roundTripperFunc func(*http.Request) (*http.Response, error)
41
42 func (f roundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) {
43 return f(req)
44 }
45
46
47
48
49 type RESTClient struct {
50 NegotiatedSerializer runtime.NegotiatedSerializer
51 GroupVersion schema.GroupVersion
52 VersionedAPIPath string
53
54
55
56
57 Err error
58
59 Req *http.Request
60
61
62 Client *http.Client
63
64 Resp *http.Response
65 }
66
67 func (c *RESTClient) Get() *restclient.Request {
68 return c.Verb("GET")
69 }
70
71 func (c *RESTClient) Put() *restclient.Request {
72 return c.Verb("PUT")
73 }
74
75 func (c *RESTClient) Patch(pt types.PatchType) *restclient.Request {
76 return c.Verb("PATCH").SetHeader("Content-Type", string(pt))
77 }
78
79 func (c *RESTClient) Post() *restclient.Request {
80 return c.Verb("POST")
81 }
82
83 func (c *RESTClient) Delete() *restclient.Request {
84 return c.Verb("DELETE")
85 }
86
87 func (c *RESTClient) Verb(verb string) *restclient.Request {
88 return c.Request().Verb(verb)
89 }
90
91 func (c *RESTClient) APIVersion() schema.GroupVersion {
92 return c.GroupVersion
93 }
94
95 func (c *RESTClient) GetRateLimiter() flowcontrol.RateLimiter {
96 return nil
97 }
98
99 func (c *RESTClient) Request() *restclient.Request {
100 config := restclient.ClientContentConfig{
101 ContentType: runtime.ContentTypeJSON,
102 GroupVersion: c.GroupVersion,
103 Negotiator: runtime.NewClientNegotiator(c.NegotiatedSerializer, c.GroupVersion),
104 }
105 return restclient.NewRequestWithClient(&url.URL{Scheme: "https", Host: "localhost"}, c.VersionedAPIPath, config, CreateHTTPClient(c.do))
106 }
107
108
109 func (c *RESTClient) do(req *http.Request) (*http.Response, error) {
110 if c.Err != nil {
111 return nil, c.Err
112 }
113 c.Req = req
114 if c.Client != nil {
115 return c.Client.Do(req)
116 }
117 return c.Resp, nil
118 }
119
View as plain text