@@ -165,9 +165,9 @@ func WithClientMiddleware(middleware ...ClientMiddleware) ClientOption {
165
165
166
166
func (c * clientCreator ) NewAppClient () (* github.Client , error ) {
167
167
base := & http.Client {Transport : http .DefaultTransport }
168
-
169
168
installation , transportError := newAppInstallation (c .integrationID , c .privKeyBytes , c .v3BaseURL )
170
- middleware := append (c .middleware , installation )
169
+
170
+ middleware := []ClientMiddleware {installation }
171
171
if c .cacheFunc != nil {
172
172
middleware = append (middleware , cache (c .cacheFunc ), cacheControl (c .alwaysValidate ))
173
173
}
@@ -184,14 +184,13 @@ func (c *clientCreator) NewAppClient() (*github.Client, error) {
184
184
185
185
func (c * clientCreator ) NewAppV4Client () (* githubv4.Client , error ) {
186
186
base := & http.Client {Transport : http .DefaultTransport }
187
-
188
187
installation , transportError := newAppInstallation (c .integrationID , c .privKeyBytes , c .v3BaseURL )
189
188
190
189
// The v4 API primarily uses POST requests (except for introspection queries)
191
- // which we cannot cache, so don't construct the middleware
192
- middleware := append ( c . middleware , installation )
190
+ // which we cannot cache, so don't add the cache middleware
191
+ middleware := [] ClientMiddleware { installation }
193
192
194
- client , err := c .newV4Client (base , middleware , "application" , 0 )
193
+ client , err := c .newV4Client (base , middleware , "application" )
195
194
if err != nil {
196
195
return nil , err
197
196
}
@@ -203,9 +202,9 @@ func (c *clientCreator) NewAppV4Client() (*githubv4.Client, error) {
203
202
204
203
func (c * clientCreator ) NewInstallationClient (installationID int64 ) (* github.Client , error ) {
205
204
base := & http.Client {Transport : http .DefaultTransport }
206
-
207
205
installation , transportError := newInstallation (c .integrationID , int (installationID ), c .privKeyBytes , c .v3BaseURL )
208
- middleware := append (c .middleware , installation )
206
+
207
+ middleware := []ClientMiddleware {installation }
209
208
if c .cacheFunc != nil {
210
209
middleware = append (middleware , cache (c .cacheFunc ), cacheControl (c .alwaysValidate ))
211
210
}
@@ -222,14 +221,13 @@ func (c *clientCreator) NewInstallationClient(installationID int64) (*github.Cli
222
221
223
222
func (c * clientCreator ) NewInstallationV4Client (installationID int64 ) (* githubv4.Client , error ) {
224
223
base := & http.Client {Transport : http .DefaultTransport }
225
-
226
224
installation , transportError := newInstallation (c .integrationID , int (installationID ), c .privKeyBytes , c .v3BaseURL )
227
225
228
226
// The v4 API primarily uses POST requests (except for introspection queries)
229
227
// which we cannot cache, so don't construct the middleware
230
- middleware := append ( c . middleware , installation )
228
+ middleware := [] ClientMiddleware { installation }
231
229
232
- client , err := c .newV4Client (base , middleware , fmt .Sprintf ("installation: %d" , installationID ), installationID )
230
+ client , err := c .newV4Client (base , middleware , fmt .Sprintf ("installation: %d" , installationID ))
233
231
if err != nil {
234
232
return nil , err
235
233
}
@@ -242,18 +240,21 @@ func (c *clientCreator) NewInstallationV4Client(installationID int64) (*githubv4
242
240
func (c * clientCreator ) NewTokenClient (token string ) (* github.Client , error ) {
243
241
ts := oauth2 .StaticTokenSource (& oauth2.Token {AccessToken : token })
244
242
tc := oauth2 .NewClient (context .Background (), ts )
245
- return c .newClient (tc , c . middleware , "oauth token" , 0 )
243
+ return c .newClient (tc , nil , "oauth token" , 0 )
246
244
}
247
245
248
246
func (c * clientCreator ) NewTokenV4Client (token string ) (* githubv4.Client , error ) {
249
247
ts := oauth2 .StaticTokenSource (& oauth2.Token {AccessToken : token })
250
248
tc := oauth2 .NewClient (context .Background (), ts )
251
- return c .newV4Client (tc , c . middleware , "oauth token" , 0 )
249
+ return c .newV4Client (tc , nil , "oauth token" )
252
250
}
253
251
254
252
func (c * clientCreator ) newClient (base * http.Client , middleware []ClientMiddleware , details string , installID int64 ) (* github.Client , error ) {
255
- middleware = append ([]ClientMiddleware {setInstallationID (installID )}, middleware ... )
256
- applyMiddleware (base , middleware )
253
+ applyMiddleware (base , [][]ClientMiddleware {
254
+ {setInstallationID (installID )},
255
+ c .middleware ,
256
+ middleware ,
257
+ })
257
258
258
259
baseURL , err := url .Parse (c .v3BaseURL )
259
260
if err != nil {
@@ -267,11 +268,12 @@ func (c *clientCreator) newClient(base *http.Client, middleware []ClientMiddlewa
267
268
return client , nil
268
269
}
269
270
270
- func (c * clientCreator ) newV4Client (base * http.Client , middleware []ClientMiddleware , details string , installID int64 ) (* githubv4.Client , error ) {
271
- ua := makeUserAgent (c .userAgent , details )
272
-
273
- middleware = append ([]ClientMiddleware {setUserAgentHeader (ua )}, middleware ... )
274
- applyMiddleware (base , middleware )
271
+ func (c * clientCreator ) newV4Client (base * http.Client , middleware []ClientMiddleware , details string ) (* githubv4.Client , error ) {
272
+ applyMiddleware (base , [][]ClientMiddleware {
273
+ {setUserAgentHeader (makeUserAgent (c .userAgent , details ))},
274
+ c .middleware ,
275
+ middleware ,
276
+ })
275
277
276
278
v4BaseURL , err := url .Parse (c .v4BaseURL )
277
279
if err != nil {
@@ -282,9 +284,14 @@ func (c *clientCreator) newV4Client(base *http.Client, middleware []ClientMiddle
282
284
return client , nil
283
285
}
284
286
285
- func applyMiddleware (base * http.Client , middleware []ClientMiddleware ) {
287
+ // applyMiddleware behaves as if it concatenates all middleware slices in the
288
+ // order given and then composes the middleware so that the first element is
289
+ // the outermost function and the last element is the innermost function.
290
+ func applyMiddleware (base * http.Client , middleware [][]ClientMiddleware ) {
286
291
for i := len (middleware ) - 1 ; i >= 0 ; i -- {
287
- base .Transport = middleware [i ](base .Transport )
292
+ for j := len (middleware [i ]) - 1 ; j >= 0 ; j -- {
293
+ base .Transport = middleware [i ][j ](base .Transport )
294
+ }
288
295
}
289
296
}
290
297
0 commit comments