@@ -21,30 +21,30 @@ import (
21
21
// options pattern in our [README].
22
22
//
23
23
// [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
25
25
26
26
// WithBaseURL returns a RequestOption that sets the BaseURL for the client.
27
27
func WithBaseURL (base string ) RequestOption {
28
28
u , err := url .Parse (base )
29
29
if err != nil {
30
30
log .Fatalf ("failed to parse BaseURL: %s\n " , err )
31
31
}
32
- return func (r * requestconfig.RequestConfig ) error {
32
+ return requestconfig . RequestOptionFunc ( func (r * requestconfig.RequestConfig ) error {
33
33
if u .Path != "" && ! strings .HasSuffix (u .Path , "/" ) {
34
34
u .Path += "/"
35
35
}
36
36
r .BaseURL = u
37
37
return nil
38
- }
38
+ })
39
39
}
40
40
41
41
// WithHTTPClient returns a RequestOption that changes the underlying [http.Client] used to make this
42
42
// request, which by default is [http.DefaultClient].
43
43
func WithHTTPClient (client * http.Client ) RequestOption {
44
- return func (r * requestconfig.RequestConfig ) error {
44
+ return requestconfig . RequestOptionFunc ( func (r * requestconfig.RequestConfig ) error {
45
45
r .HTTPClient = client
46
46
return nil
47
- }
47
+ })
48
48
}
49
49
50
50
// 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)
59
59
// WithMiddleware returns a RequestOption that applies the given middleware
60
60
// to the requests made. Each middleware will execute in the order they were given.
61
61
func WithMiddleware (middlewares ... Middleware ) RequestOption {
62
- return func (r * requestconfig.RequestConfig ) error {
62
+ return requestconfig . RequestOptionFunc ( func (r * requestconfig.RequestConfig ) error {
63
63
r .Middlewares = append (r .Middlewares , middlewares ... )
64
64
return nil
65
- }
65
+ })
66
66
}
67
67
68
68
// WithMaxRetries returns a RequestOption that sets the maximum number of retries that the client
@@ -74,76 +74,76 @@ func WithMaxRetries(retries int) RequestOption {
74
74
if retries < 0 {
75
75
panic ("option: cannot have fewer than 0 retries" )
76
76
}
77
- return func (r * requestconfig.RequestConfig ) error {
77
+ return requestconfig . RequestOptionFunc ( func (r * requestconfig.RequestConfig ) error {
78
78
r .MaxRetries = retries
79
79
return nil
80
- }
80
+ })
81
81
}
82
82
83
83
// WithHeader returns a RequestOption that sets the header value to the associated key. It overwrites
84
84
// any value if there was one already present.
85
85
func WithHeader (key , value string ) RequestOption {
86
- return func (r * requestconfig.RequestConfig ) error {
86
+ return requestconfig . RequestOptionFunc ( func (r * requestconfig.RequestConfig ) error {
87
87
r .Request .Header .Set (key , value )
88
88
return nil
89
- }
89
+ })
90
90
}
91
91
92
92
// WithHeaderAdd returns a RequestOption that adds the header value to the associated key. It appends
93
93
// onto any existing values.
94
94
func WithHeaderAdd (key , value string ) RequestOption {
95
- return func (r * requestconfig.RequestConfig ) error {
95
+ return requestconfig . RequestOptionFunc ( func (r * requestconfig.RequestConfig ) error {
96
96
r .Request .Header .Add (key , value )
97
97
return nil
98
- }
98
+ })
99
99
}
100
100
101
101
// WithHeaderDel returns a RequestOption that deletes the header value(s) associated with the given key.
102
102
func WithHeaderDel (key string ) RequestOption {
103
- return func (r * requestconfig.RequestConfig ) error {
103
+ return requestconfig . RequestOptionFunc ( func (r * requestconfig.RequestConfig ) error {
104
104
r .Request .Header .Del (key )
105
105
return nil
106
- }
106
+ })
107
107
}
108
108
109
109
// WithQuery returns a RequestOption that sets the query value to the associated key. It overwrites
110
110
// any value if there was one already present.
111
111
func WithQuery (key , value string ) RequestOption {
112
- return func (r * requestconfig.RequestConfig ) error {
112
+ return requestconfig . RequestOptionFunc ( func (r * requestconfig.RequestConfig ) error {
113
113
query := r .Request .URL .Query ()
114
114
query .Set (key , value )
115
115
r .Request .URL .RawQuery = query .Encode ()
116
116
return nil
117
- }
117
+ })
118
118
}
119
119
120
120
// WithQueryAdd returns a RequestOption that adds the query value to the associated key. It appends
121
121
// onto any existing values.
122
122
func WithQueryAdd (key , value string ) RequestOption {
123
- return func (r * requestconfig.RequestConfig ) error {
123
+ return requestconfig . RequestOptionFunc ( func (r * requestconfig.RequestConfig ) error {
124
124
query := r .Request .URL .Query ()
125
125
query .Add (key , value )
126
126
r .Request .URL .RawQuery = query .Encode ()
127
127
return nil
128
- }
128
+ })
129
129
}
130
130
131
131
// WithQueryDel returns a RequestOption that deletes the query value(s) associated with the key.
132
132
func WithQueryDel (key string ) RequestOption {
133
- return func (r * requestconfig.RequestConfig ) error {
133
+ return requestconfig . RequestOptionFunc ( func (r * requestconfig.RequestConfig ) error {
134
134
query := r .Request .URL .Query ()
135
135
query .Del (key )
136
136
r .Request .URL .RawQuery = query .Encode ()
137
137
return nil
138
- }
138
+ })
139
139
}
140
140
141
141
// WithJSONSet returns a RequestOption that sets the body's JSON value associated with the key.
142
142
// The key accepts a string as defined by the [sjson format].
143
143
//
144
144
// [sjson format]: https://github.com/tidwall/sjson
145
145
func WithJSONSet (key string , value interface {}) RequestOption {
146
- return func (r * requestconfig.RequestConfig ) (err error ) {
146
+ return requestconfig . RequestOptionFunc ( func (r * requestconfig.RequestConfig ) (err error ) {
147
147
if buffer , ok := r .Body .(* bytes.Buffer ); ok {
148
148
b := buffer .Bytes ()
149
149
b , err = sjson .SetBytes (b , key , value )
@@ -155,15 +155,15 @@ func WithJSONSet(key string, value interface{}) RequestOption {
155
155
}
156
156
157
157
return fmt .Errorf ("cannot use WithJSONSet on a body that is not serialized as *bytes.Buffer" )
158
- }
158
+ })
159
159
}
160
160
161
161
// WithJSONDel returns a RequestOption that deletes the body's JSON value associated with the key.
162
162
// The key accepts a string as defined by the [sjson format].
163
163
//
164
164
// [sjson format]: https://github.com/tidwall/sjson
165
165
func WithJSONDel (key string ) RequestOption {
166
- return func (r * requestconfig.RequestConfig ) (err error ) {
166
+ return requestconfig . RequestOptionFunc ( func (r * requestconfig.RequestConfig ) (err error ) {
167
167
if buffer , ok := r .Body .(* bytes.Buffer ); ok {
168
168
b := buffer .Bytes ()
169
169
b , err = sjson .DeleteBytes (b , key )
@@ -175,32 +175,32 @@ func WithJSONDel(key string) RequestOption {
175
175
}
176
176
177
177
return fmt .Errorf ("cannot use WithJSONDel on a body that is not serialized as *bytes.Buffer" )
178
- }
178
+ })
179
179
}
180
180
181
181
// WithResponseBodyInto returns a RequestOption that overwrites the deserialization target with
182
182
// the given destination. If provided, we don't deserialize into the default struct.
183
183
func WithResponseBodyInto (dst any ) RequestOption {
184
- return func (r * requestconfig.RequestConfig ) error {
184
+ return requestconfig . RequestOptionFunc ( func (r * requestconfig.RequestConfig ) error {
185
185
r .ResponseBodyInto = dst
186
186
return nil
187
- }
187
+ })
188
188
}
189
189
190
190
// WithResponseInto returns a RequestOption that copies the [*http.Response] into the given address.
191
191
func WithResponseInto (dst * * http.Response ) RequestOption {
192
- return func (r * requestconfig.RequestConfig ) error {
192
+ return requestconfig . RequestOptionFunc ( func (r * requestconfig.RequestConfig ) error {
193
193
r .ResponseInto = dst
194
194
return nil
195
- }
195
+ })
196
196
}
197
197
198
198
// WithRequestBody returns a RequestOption that provides a custom serialized body with the given
199
199
// content type.
200
200
//
201
201
// body accepts an io.Reader or raw []bytes.
202
202
func WithRequestBody (contentType string , body any ) RequestOption {
203
- return func (r * requestconfig.RequestConfig ) error {
203
+ return requestconfig . RequestOptionFunc ( func (r * requestconfig.RequestConfig ) error {
204
204
if reader , ok := body .(io.Reader ); ok {
205
205
r .Body = reader
206
206
return r .Apply (WithHeader ("Content-Type" , contentType ))
@@ -212,17 +212,17 @@ func WithRequestBody(contentType string, body any) RequestOption {
212
212
}
213
213
214
214
return fmt .Errorf ("body must be a byte slice or implement io.Reader" )
215
- }
215
+ })
216
216
}
217
217
218
218
// WithRequestTimeout returns a RequestOption that sets the timeout for
219
219
// each request attempt. This should be smaller than the timeout defined in
220
220
// the context, which spans all retries.
221
221
func WithRequestTimeout (dur time.Duration ) RequestOption {
222
- return func (r * requestconfig.RequestConfig ) error {
222
+ return requestconfig . RequestOptionFunc ( func (r * requestconfig.RequestConfig ) error {
223
223
r .RequestTimeout = dur
224
224
return nil
225
- }
225
+ })
226
226
}
227
227
228
228
// WithEnvironmentProduction returns a RequestOption that sets the current
@@ -234,8 +234,8 @@ func WithEnvironmentProduction() RequestOption {
234
234
235
235
// WithBearerToken returns a RequestOption that sets the client setting "bearer_token".
236
236
func WithBearerToken (value string ) RequestOption {
237
- return func (r * requestconfig.RequestConfig ) error {
237
+ return requestconfig . RequestOptionFunc ( func (r * requestconfig.RequestConfig ) error {
238
238
r .BearerToken = value
239
239
return r .Apply (WithHeader ("authorization" , fmt .Sprintf ("Bearer %s" , r .BearerToken )))
240
- }
240
+ })
241
241
}
0 commit comments