Skip to content

Commit

Permalink
Updated with PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
natdm committed Oct 16, 2017
1 parent ab5be53 commit a8ae951
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
all: test bench vet check-gofmt

bench:
go test --race -bench . -run "Benchmark" ./form
go test -race -bench . -run "Benchmark" ./form

build:
go build ./...
Expand All @@ -10,7 +10,7 @@ check-gofmt:
scripts/check_gofmt.sh

test:
go test --race ./...
go test -race ./...

vet:
go vet ./...
4 changes: 3 additions & 1 deletion error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ func TestErrorResponse(t *testing.T) {
defer ts.Close()

SetBackend("api", &BackendConfiguration{
Type: APIBackend, URL: ts.URL, HTTPClient: &http.Client{},
APIBackend,
ts.URL,
&http.Client{},
})

err := GetBackend(APIBackend).Call("GET", "/v1/account", "sk_test_badKey", nil, nil, nil)
Expand Down
40 changes: 22 additions & 18 deletions stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ type Backend interface {

// BackendConfiguration is the internal implementation for making HTTP calls to Stripe.
type BackendConfiguration struct {
mu sync.Mutex
Type SupportedBackend
URL string
HTTPClient *http.Client
Expand All @@ -103,6 +102,7 @@ const (
// Backends are the currently supported endpoints.
type Backends struct {
API, Uploads Backend
mu sync.RWMutex
}

// stripeClientUserAgent contains information about the current runtime which
Expand Down Expand Up @@ -160,32 +160,40 @@ func SetHTTPClient(client *http.Client) {
func NewBackends(httpClient *http.Client) *Backends {
return &Backends{
API: &BackendConfiguration{
Type: APIBackend, URL: APIURL, HTTPClient: httpClient},
APIBackend, APIURL, httpClient},
Uploads: &BackendConfiguration{
Type: UploadsBackend, URL: UploadsURL, HTTPClient: httpClient},
UploadsBackend, UploadsURL, httpClient},
}
}

// GetBackend returns the currently used backend in the binding.
func GetBackend(backend SupportedBackend) Backend {
var ret Backend
switch backend {
case APIBackend:
if backends.API == nil {
backends.API = &BackendConfiguration{
Type: backend, URL: apiURL, HTTPClient: httpClient}
backends.mu.RLock()
ret := backends.API
backends.mu.RUnlock()
if ret != nil {
return ret
}

ret = backends.API
backends.mu.Lock()
defer backends.mu.Unlock()
backends.API = &BackendConfiguration{backend, apiURL, httpClient}
return backends.API
case UploadsBackend:
if backends.Uploads == nil {
backends.Uploads = &BackendConfiguration{
Type: backend, URL: uploadsURL, HTTPClient: httpClient}
backends.mu.RLock()
ret := backends.Uploads
backends.mu.RUnlock()
if ret != nil {
return ret
}
ret = backends.Uploads
backends.mu.Lock()
defer backends.mu.Unlock()
backends.Uploads = &BackendConfiguration{backend, uploadsURL, httpClient}
return backends.Uploads
}

return ret
return nil
}

// SetBackend sets the backend used in the binding.
Expand Down Expand Up @@ -241,8 +249,6 @@ func (s *BackendConfiguration) CallMultipart(method, path, key, boundary string,
// NewRequest is used by Call to generate an http.Request. It handles encoding
// parameters and attaching the appropriate headers.
func (s *BackendConfiguration) NewRequest(method, path, key, contentType string, body io.Reader, params *Params) (*http.Request, error) {
s.mu.Lock()
defer s.mu.Unlock()
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
Expand Down Expand Up @@ -298,8 +304,6 @@ func (s *BackendConfiguration) NewRequest(method, path, key, contentType string,
// the backend's HTTP client to execute the request and unmarshals the response
// into v. It also handles unmarshaling errors returned by the API.
func (s *BackendConfiguration) Do(req *http.Request, v interface{}) error {
s.mu.Lock()
defer s.mu.Unlock()
if LogLevel > 1 {
Logger.Printf("Requesting %v %v%v\n", req.Method, req.URL.Host, req.URL.Path)
}
Expand Down

0 comments on commit a8ae951

Please sign in to comment.