Skip to content

Commit

Permalink
internal: remove RegisterContextClientFunc
Browse files Browse the repository at this point in the history
This function added a totally unused error path, since the only call
site is for App Engine, which cannot produce an error.

Change-Id: I86277ab4ff96e7bd140c53c5a114a338716668e3
Reviewed-on: https://go-review.googlesource.com/85935
Reviewed-by: Brad Fitzpatrick <[email protected]>
  • Loading branch information
zombiezen committed Jan 3, 2018
1 parent ee2bad9 commit 876b1c6
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 114 deletions.
25 changes: 0 additions & 25 deletions client_appengine.go

This file was deleted.

13 changes: 13 additions & 0 deletions internal/client_appengine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build appengine

package internal

import "google.golang.org/appengine/urlfetch"

func init() {
appengineClientHook = urlfetch.Client
}
6 changes: 1 addition & 5 deletions internal/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,6 @@ func providerAuthHeaderWorks(tokenURL string) bool {
}

func RetrieveToken(ctx context.Context, clientID, clientSecret, tokenURL string, v url.Values) (*Token, error) {
hc, err := ContextClient(ctx)
if err != nil {
return nil, err
}
bustedAuth := !providerAuthHeaderWorks(tokenURL)
if bustedAuth {
if clientID != "" {
Expand All @@ -191,7 +187,7 @@ func RetrieveToken(ctx context.Context, clientID, clientSecret, tokenURL string,
if !bustedAuth {
req.SetBasicAuth(url.QueryEscape(clientID), url.QueryEscape(clientSecret))
}
r, err := ctxhttp.Do(ctx, hc, req)
r, err := ctxhttp.Do(ctx, ContextClient(ctx), req)
if err != nil {
return nil, err
}
Expand Down
46 changes: 6 additions & 40 deletions internal/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,50 +19,16 @@ var HTTPClient ContextKey
// because nobody else can create a ContextKey, being unexported.
type ContextKey struct{}

// ContextClientFunc is a func which tries to return an *http.Client
// given a Context value. If it returns an error, the search stops
// with that error. If it returns (nil, nil), the search continues
// down the list of registered funcs.
type ContextClientFunc func(context.Context) (*http.Client, error)
var appengineClientHook func(context.Context) *http.Client

var contextClientFuncs []ContextClientFunc

func RegisterContextClientFunc(fn ContextClientFunc) {
contextClientFuncs = append(contextClientFuncs, fn)
}

func ContextClient(ctx context.Context) (*http.Client, error) {
func ContextClient(ctx context.Context) *http.Client {
if ctx != nil {
if hc, ok := ctx.Value(HTTPClient).(*http.Client); ok {
return hc, nil
return hc
}
}
for _, fn := range contextClientFuncs {
c, err := fn(ctx)
if err != nil {
return nil, err
}
if c != nil {
return c, nil
}
if appengineClientHook != nil {
return appengineClientHook(ctx)
}
return http.DefaultClient, nil
}

func ContextTransport(ctx context.Context) http.RoundTripper {
hc, err := ContextClient(ctx)
// This is a rare error case (somebody using nil on App Engine).
if err != nil {
return ErrorTransport{err}
}
return hc.Transport
}

// ErrorTransport returns the specified error on RoundTrip.
// This RoundTripper should be used in rare error cases where
// error handling can be postponed to response handling time.
type ErrorTransport struct{ Err error }

func (t ErrorTransport) RoundTrip(*http.Request) (*http.Response, error) {
return nil, t.Err
return http.DefaultClient
}
38 changes: 0 additions & 38 deletions internal/transport_test.go

This file was deleted.

8 changes: 2 additions & 6 deletions oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,15 +313,11 @@ var HTTPClient internal.ContextKey
// packages.
func NewClient(ctx context.Context, src TokenSource) *http.Client {
if src == nil {
c, err := internal.ContextClient(ctx)
if err != nil {
return &http.Client{Transport: internal.ErrorTransport{Err: err}}
}
return c
return internal.ContextClient(ctx)
}
return &http.Client{
Transport: &Transport{
Base: internal.ContextTransport(ctx),
Base: internal.ContextClient(ctx).Transport,
Source: ReuseTokenSource(nil, src),
},
}
Expand Down

0 comments on commit 876b1c6

Please sign in to comment.