@@ -21,30 +21,30 @@ import (
2121// options pattern in our [README].
2222//
2323// [README]: https://pkg.go.dev/github.com/gitpod-io/gitpod-sdk-go#readme-requestoptions
24- type RequestOption = func ( * requestconfig.RequestConfig ) error
24+ type RequestOption = requestconfig.RequestOption
2525
2626// WithBaseURL returns a RequestOption that sets the BaseURL for the client.
2727func WithBaseURL (base string ) RequestOption {
2828 u , err := url .Parse (base )
2929 if err != nil {
3030 log .Fatalf ("failed to parse BaseURL: %s\n " , err )
3131 }
32- return func (r * requestconfig.RequestConfig ) error {
32+ return requestconfig . RequestOptionFunc ( func (r * requestconfig.RequestConfig ) error {
3333 if u .Path != "" && ! strings .HasSuffix (u .Path , "/" ) {
3434 u .Path += "/"
3535 }
3636 r .BaseURL = u
3737 return nil
38- }
38+ })
3939}
4040
4141// WithHTTPClient returns a RequestOption that changes the underlying [http.Client] used to make this
4242// request, which by default is [http.DefaultClient].
4343func WithHTTPClient (client * http.Client ) RequestOption {
44- return func (r * requestconfig.RequestConfig ) error {
44+ return requestconfig . RequestOptionFunc ( func (r * requestconfig.RequestConfig ) error {
4545 r .HTTPClient = client
4646 return nil
47- }
47+ })
4848}
4949
5050// MiddlewareNext is a function which is called by a middleware to pass an HTTP request
@@ -59,10 +59,10 @@ type Middleware = func(*http.Request, MiddlewareNext) (*http.Response, error)
5959// WithMiddleware returns a RequestOption that applies the given middleware
6060// to the requests made. Each middleware will execute in the order they were given.
6161func WithMiddleware (middlewares ... Middleware ) RequestOption {
62- return func (r * requestconfig.RequestConfig ) error {
62+ return requestconfig . RequestOptionFunc ( func (r * requestconfig.RequestConfig ) error {
6363 r .Middlewares = append (r .Middlewares , middlewares ... )
6464 return nil
65- }
65+ })
6666}
6767
6868// WithMaxRetries returns a RequestOption that sets the maximum number of retries that the client
@@ -74,76 +74,76 @@ func WithMaxRetries(retries int) RequestOption {
7474 if retries < 0 {
7575 panic ("option: cannot have fewer than 0 retries" )
7676 }
77- return func (r * requestconfig.RequestConfig ) error {
77+ return requestconfig . RequestOptionFunc ( func (r * requestconfig.RequestConfig ) error {
7878 r .MaxRetries = retries
7979 return nil
80- }
80+ })
8181}
8282
8383// WithHeader returns a RequestOption that sets the header value to the associated key. It overwrites
8484// any value if there was one already present.
8585func WithHeader (key , value string ) RequestOption {
86- return func (r * requestconfig.RequestConfig ) error {
86+ return requestconfig . RequestOptionFunc ( func (r * requestconfig.RequestConfig ) error {
8787 r .Request .Header .Set (key , value )
8888 return nil
89- }
89+ })
9090}
9191
9292// WithHeaderAdd returns a RequestOption that adds the header value to the associated key. It appends
9393// onto any existing values.
9494func WithHeaderAdd (key , value string ) RequestOption {
95- return func (r * requestconfig.RequestConfig ) error {
95+ return requestconfig . RequestOptionFunc ( func (r * requestconfig.RequestConfig ) error {
9696 r .Request .Header .Add (key , value )
9797 return nil
98- }
98+ })
9999}
100100
101101// WithHeaderDel returns a RequestOption that deletes the header value(s) associated with the given key.
102102func WithHeaderDel (key string ) RequestOption {
103- return func (r * requestconfig.RequestConfig ) error {
103+ return requestconfig . RequestOptionFunc ( func (r * requestconfig.RequestConfig ) error {
104104 r .Request .Header .Del (key )
105105 return nil
106- }
106+ })
107107}
108108
109109// WithQuery returns a RequestOption that sets the query value to the associated key. It overwrites
110110// any value if there was one already present.
111111func WithQuery (key , value string ) RequestOption {
112- return func (r * requestconfig.RequestConfig ) error {
112+ return requestconfig . RequestOptionFunc ( func (r * requestconfig.RequestConfig ) error {
113113 query := r .Request .URL .Query ()
114114 query .Set (key , value )
115115 r .Request .URL .RawQuery = query .Encode ()
116116 return nil
117- }
117+ })
118118}
119119
120120// WithQueryAdd returns a RequestOption that adds the query value to the associated key. It appends
121121// onto any existing values.
122122func WithQueryAdd (key , value string ) RequestOption {
123- return func (r * requestconfig.RequestConfig ) error {
123+ return requestconfig . RequestOptionFunc ( func (r * requestconfig.RequestConfig ) error {
124124 query := r .Request .URL .Query ()
125125 query .Add (key , value )
126126 r .Request .URL .RawQuery = query .Encode ()
127127 return nil
128- }
128+ })
129129}
130130
131131// WithQueryDel returns a RequestOption that deletes the query value(s) associated with the key.
132132func WithQueryDel (key string ) RequestOption {
133- return func (r * requestconfig.RequestConfig ) error {
133+ return requestconfig . RequestOptionFunc ( func (r * requestconfig.RequestConfig ) error {
134134 query := r .Request .URL .Query ()
135135 query .Del (key )
136136 r .Request .URL .RawQuery = query .Encode ()
137137 return nil
138- }
138+ })
139139}
140140
141141// WithJSONSet returns a RequestOption that sets the body's JSON value associated with the key.
142142// The key accepts a string as defined by the [sjson format].
143143//
144144// [sjson format]: https://github.com/tidwall/sjson
145145func WithJSONSet (key string , value interface {}) RequestOption {
146- return func (r * requestconfig.RequestConfig ) (err error ) {
146+ return requestconfig . RequestOptionFunc ( func (r * requestconfig.RequestConfig ) (err error ) {
147147 if buffer , ok := r .Body .(* bytes.Buffer ); ok {
148148 b := buffer .Bytes ()
149149 b , err = sjson .SetBytes (b , key , value )
@@ -155,15 +155,15 @@ func WithJSONSet(key string, value interface{}) RequestOption {
155155 }
156156
157157 return fmt .Errorf ("cannot use WithJSONSet on a body that is not serialized as *bytes.Buffer" )
158- }
158+ })
159159}
160160
161161// WithJSONDel returns a RequestOption that deletes the body's JSON value associated with the key.
162162// The key accepts a string as defined by the [sjson format].
163163//
164164// [sjson format]: https://github.com/tidwall/sjson
165165func WithJSONDel (key string ) RequestOption {
166- return func (r * requestconfig.RequestConfig ) (err error ) {
166+ return requestconfig . RequestOptionFunc ( func (r * requestconfig.RequestConfig ) (err error ) {
167167 if buffer , ok := r .Body .(* bytes.Buffer ); ok {
168168 b := buffer .Bytes ()
169169 b , err = sjson .DeleteBytes (b , key )
@@ -175,32 +175,32 @@ func WithJSONDel(key string) RequestOption {
175175 }
176176
177177 return fmt .Errorf ("cannot use WithJSONDel on a body that is not serialized as *bytes.Buffer" )
178- }
178+ })
179179}
180180
181181// WithResponseBodyInto returns a RequestOption that overwrites the deserialization target with
182182// the given destination. If provided, we don't deserialize into the default struct.
183183func WithResponseBodyInto (dst any ) RequestOption {
184- return func (r * requestconfig.RequestConfig ) error {
184+ return requestconfig . RequestOptionFunc ( func (r * requestconfig.RequestConfig ) error {
185185 r .ResponseBodyInto = dst
186186 return nil
187- }
187+ })
188188}
189189
190190// WithResponseInto returns a RequestOption that copies the [*http.Response] into the given address.
191191func WithResponseInto (dst * * http.Response ) RequestOption {
192- return func (r * requestconfig.RequestConfig ) error {
192+ return requestconfig . RequestOptionFunc ( func (r * requestconfig.RequestConfig ) error {
193193 r .ResponseInto = dst
194194 return nil
195- }
195+ })
196196}
197197
198198// WithRequestBody returns a RequestOption that provides a custom serialized body with the given
199199// content type.
200200//
201201// body accepts an io.Reader or raw []bytes.
202202func WithRequestBody (contentType string , body any ) RequestOption {
203- return func (r * requestconfig.RequestConfig ) error {
203+ return requestconfig . RequestOptionFunc ( func (r * requestconfig.RequestConfig ) error {
204204 if reader , ok := body .(io.Reader ); ok {
205205 r .Body = reader
206206 return r .Apply (WithHeader ("Content-Type" , contentType ))
@@ -212,17 +212,17 @@ func WithRequestBody(contentType string, body any) RequestOption {
212212 }
213213
214214 return fmt .Errorf ("body must be a byte slice or implement io.Reader" )
215- }
215+ })
216216}
217217
218218// WithRequestTimeout returns a RequestOption that sets the timeout for
219219// each request attempt. This should be smaller than the timeout defined in
220220// the context, which spans all retries.
221221func WithRequestTimeout (dur time.Duration ) RequestOption {
222- return func (r * requestconfig.RequestConfig ) error {
222+ return requestconfig . RequestOptionFunc ( func (r * requestconfig.RequestConfig ) error {
223223 r .RequestTimeout = dur
224224 return nil
225- }
225+ })
226226}
227227
228228// WithEnvironmentProduction returns a RequestOption that sets the current
@@ -234,8 +234,8 @@ func WithEnvironmentProduction() RequestOption {
234234
235235// WithBearerToken returns a RequestOption that sets the client setting "bearer_token".
236236func WithBearerToken (value string ) RequestOption {
237- return func (r * requestconfig.RequestConfig ) error {
237+ return requestconfig . RequestOptionFunc ( func (r * requestconfig.RequestConfig ) error {
238238 r .BearerToken = value
239239 return r .Apply (WithHeader ("authorization" , fmt .Sprintf ("Bearer %s" , r .BearerToken )))
240- }
240+ })
241241}
0 commit comments