Skip to content

Commit

Permalink
Changed method receiver to pointer, removing the need for a mutex add…
Browse files Browse the repository at this point in the history
…ress
  • Loading branch information
natdm committed Oct 16, 2017
1 parent 58e3196 commit 453a5bb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 27 deletions.
5 changes: 2 additions & 3 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"sync"
"testing"

assert "github.com/stretchr/testify/require"
Expand All @@ -23,8 +22,8 @@ func TestErrorResponse(t *testing.T) {
}))
defer ts.Close()

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

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

// BackendConfiguration is the internal implementation for making HTTP calls to Stripe.
type BackendConfiguration struct {
Mutex *sync.Mutex
Type SupportedBackend
URL string
HTTPClient *http.Client
sync.Mutex
}

// SupportedBackend is an enumeration of supported Stripe endpoints.
Expand Down Expand Up @@ -159,10 +159,10 @@ func SetHTTPClient(client *http.Client) {
// should only need to use this for testing purposes or on App Engine.
func NewBackends(httpClient *http.Client) *Backends {
return &Backends{
API: BackendConfiguration{
Mutex: &sync.Mutex{}, Type: APIBackend, URL: APIURL, HTTPClient: httpClient},
Uploads: BackendConfiguration{
Mutex: &sync.Mutex{}, Type: UploadsBackend, URL: UploadsURL, HTTPClient: httpClient},
API: &BackendConfiguration{
Type: APIBackend, URL: APIURL, HTTPClient: httpClient},
Uploads: &BackendConfiguration{
Type: UploadsBackend, URL: UploadsURL, HTTPClient: httpClient},
}
}

Expand All @@ -172,15 +172,15 @@ func GetBackend(backend SupportedBackend) Backend {
switch backend {
case APIBackend:
if backends.API == nil {
backends.API = BackendConfiguration{
Mutex: &sync.Mutex{}, Type: backend, URL: apiURL, HTTPClient: httpClient}
backends.API = &BackendConfiguration{
Type: backend, URL: apiURL, HTTPClient: httpClient}
}

ret = backends.API
case UploadsBackend:
if backends.Uploads == nil {
backends.Uploads = BackendConfiguration{
Mutex: &sync.Mutex{}, Type: backend, URL: uploadsURL, HTTPClient: httpClient}
backends.Uploads = &BackendConfiguration{
Type: backend, URL: uploadsURL, HTTPClient: httpClient}
}
ret = backends.Uploads
}
Expand All @@ -199,7 +199,7 @@ func SetBackend(backend SupportedBackend, b Backend) {
}

// Call is the Backend.Call implementation for invoking Stripe APIs.
func (s BackendConfiguration) Call(method, path, key string, form *form.Values, params *Params, v interface{}) error {
func (s *BackendConfiguration) Call(method, path, key string, form *form.Values, params *Params, v interface{}) error {
var body io.Reader
if form != nil && !form.Empty() {
data := form.Encode()
Expand All @@ -223,7 +223,7 @@ func (s BackendConfiguration) Call(method, path, key string, form *form.Values,
}

// CallMultipart is the Backend.CallMultipart implementation for invoking Stripe APIs.
func (s BackendConfiguration) CallMultipart(method, path, key, boundary string, body io.Reader, params *Params, v interface{}) error {
func (s *BackendConfiguration) CallMultipart(method, path, key, boundary string, body io.Reader, params *Params, v interface{}) error {
contentType := "multipart/form-data; boundary=" + boundary

req, err := s.NewRequest(method, path, key, contentType, body, params)
Expand All @@ -241,8 +241,8 @@ 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.Mutex.Lock()
defer s.Mutex.Unlock()
s.Lock()
defer s.Unlock()
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
Expand Down Expand Up @@ -298,8 +298,8 @@ 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.Mutex.Lock()
defer s.Mutex.Unlock()
s.Lock()
defer s.Unlock()
if LogLevel > 1 {
Logger.Printf("Requesting %v %v%v\n", req.Method, req.URL.Host, req.URL.Path)
}
Expand Down
18 changes: 9 additions & 9 deletions stripe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func TestCheckinUseBearerAuth(t *testing.T) {
c := &stripe.BackendConfiguration{Mutex: &sync.Mutex{}, URL: stripe.APIURL}
c := &stripe.BackendConfiguration{URL: stripe.APIURL}
key := "apiKey"

req, err := c.NewRequest("", "", key, "", nil, nil)
Expand All @@ -30,7 +30,7 @@ func TestMultipleAPICalls(t *testing.T) {
wg.Add(1)
go func() {
defer wg.Done()
c := &stripe.BackendConfiguration{Mutex: &sync.Mutex{}, URL: stripe.APIURL}
c := &stripe.BackendConfiguration{URL: stripe.APIURL}
key := "apiKey"

req, err := c.NewRequest("", "", key, "", nil, nil)
Expand All @@ -43,7 +43,7 @@ func TestMultipleAPICalls(t *testing.T) {
}

func TestIdempotencyKey(t *testing.T) {
c := &stripe.BackendConfiguration{Mutex: &sync.Mutex{}, URL: stripe.APIURL}
c := &stripe.BackendConfiguration{URL: stripe.APIURL}
p := &stripe.Params{IdempotencyKey: "idempotency-key"}

req, err := c.NewRequest("", "", "", "", nil, p)
Expand All @@ -53,7 +53,7 @@ func TestIdempotencyKey(t *testing.T) {
}

func TestStripeAccount(t *testing.T) {
c := &stripe.BackendConfiguration{Mutex: &sync.Mutex{}, URL: stripe.APIURL}
c := &stripe.BackendConfiguration{URL: stripe.APIURL}
p := &stripe.Params{StripeAccount: TestMerchantID}

req, err := c.NewRequest("", "", "", "", nil, p)
Expand All @@ -72,7 +72,7 @@ func TestStripeAccount(t *testing.T) {
}

func TestUserAgent(t *testing.T) {
c := &stripe.BackendConfiguration{Mutex: &sync.Mutex{}, URL: stripe.APIURL}
c := &stripe.BackendConfiguration{URL: stripe.APIURL}

req, err := c.NewRequest("", "", "", "", nil, nil)
assert.NoError(t, err)
Expand All @@ -94,7 +94,7 @@ func TestUserAgentWithAppInfo(t *testing.T) {
stripe.SetAppInfo(appInfo)
defer stripe.SetAppInfo(nil)

c := &stripe.BackendConfiguration{Mutex: &sync.Mutex{}, URL: stripe.APIURL}
c := &stripe.BackendConfiguration{URL: stripe.APIURL}

req, err := c.NewRequest("", "", "", "", nil, nil)
assert.NoError(t, err)
Expand All @@ -108,7 +108,7 @@ func TestUserAgentWithAppInfo(t *testing.T) {
}

func TestStripeClientUserAgent(t *testing.T) {
c := &stripe.BackendConfiguration{Mutex: &sync.Mutex{}, URL: stripe.APIURL}
c := &stripe.BackendConfiguration{URL: stripe.APIURL}

req, err := c.NewRequest("", "", "", "", nil, nil)
assert.NoError(t, err)
Expand Down Expand Up @@ -142,7 +142,7 @@ func TestStripeClientUserAgentWithAppInfo(t *testing.T) {
stripe.SetAppInfo(appInfo)
defer stripe.SetAppInfo(nil)

c := &stripe.BackendConfiguration{Mutex: &sync.Mutex{}, URL: stripe.APIURL}
c := &stripe.BackendConfiguration{URL: stripe.APIURL}

req, err := c.NewRequest("", "", "", "", nil, nil)
assert.NoError(t, err)
Expand All @@ -161,7 +161,7 @@ func TestStripeClientUserAgentWithAppInfo(t *testing.T) {
}

func TestResponseToError(t *testing.T) {
c := &stripe.BackendConfiguration{Mutex: &sync.Mutex{}, URL: stripe.APIURL}
c := &stripe.BackendConfiguration{URL: stripe.APIURL}

// A test response that includes a status code and request ID.
res := &http.Response{
Expand Down

0 comments on commit 453a5bb

Please sign in to comment.