diff --git a/auth_test.go b/auth_test.go index 8b58e6f..37e9067 100644 --- a/auth_test.go +++ b/auth_test.go @@ -19,12 +19,12 @@ func TestGetAuthorization(t *testing.T) { a, err := c.GetAuthorization(testAuthID) if err != nil || a.ID != testAuthID { - t.Errorf("GetAuthorization failed for ID=" + testAuthID) + t.Errorf("GetAuthorization failed for ID=%s", testAuthID) } a, err = c.GetAuthorization(testFakeAuthID) if err == nil { - t.Errorf("GetAuthorization must return error for ID=" + testFakeAuthID) + t.Errorf("GetAuthorization must return error for ID=%s", testFakeAuthID) } } diff --git a/client.go b/client.go index 442ab5c..9beee97 100644 --- a/client.go +++ b/client.go @@ -47,6 +47,12 @@ func (c *Client) SetAccessToken(token string) error { // unmarshaled into v, or if v is an io.Writer, the response will // be written to it without decoding func (c *Client) Send(req *http.Request, v interface{}) error { + var ( + err error + resp *http.Response + data []byte + ) + // Set default headers req.Header.Set("Accept", "application/json") req.Header.Set("Accept-Language", "en_US") @@ -56,7 +62,7 @@ func (c *Client) Send(req *http.Request, v interface{}) error { req.Header.Set("Content-type", "application/json") } - resp, err := c.client.Do(req) + resp, err = c.client.Do(req) c.log(req, resp) if err != nil { @@ -66,7 +72,7 @@ func (c *Client) Send(req *http.Request, v interface{}) error { if resp.StatusCode < 200 || resp.StatusCode > 299 { errResp := &ErrorResponse{Response: resp} - data, err := ioutil.ReadAll(resp.Body) + data, err = ioutil.ReadAll(resp.Body) if err == nil && len(data) > 0 { json.Unmarshal(data, errResp) @@ -90,10 +96,19 @@ func (c *Client) Send(req *http.Request, v interface{}) error { } // SendWithAuth makes a request to the API and apply OAuth2 header automatically. -// If the access token soon to be expired, it will try to get a new one before +// If the access token soon to be expired or already expired, it will try to get a new one before // making the main request +// client.Token will be updated when changed func (c *Client) SendWithAuth(req *http.Request, v interface{}) error { if c.Token != nil { + if c.Token.ExpiresIn < RequestNewTokenBeforeExpiresIn { + // c.Token willbe updated in GetAccessToken call + _, err := c.GetAccessToken() + if err != nil { + return err + } + } + req.Header.Set("Authorization", "Bearer "+c.Token.Token) } diff --git a/client_test.go b/client_test.go index eb37983..04a70b6 100644 --- a/client_test.go +++ b/client_test.go @@ -27,6 +27,6 @@ func TestNewClient(t *testing.T) { _, err = NewClient(testClientID, testSecret, APIBaseSandBox) if err != nil { - t.Errorf("NewClient() must not return error for valid creds: " + err.Error()) + t.Errorf("NewClient() must not return error for valid creds: %s", err.Error()) } } diff --git a/identity_test.go b/identity_test.go index 6336e9b..9593b6d 100644 --- a/identity_test.go +++ b/identity_test.go @@ -26,6 +26,6 @@ func TestGetUserInfo(t *testing.T) { u, err := c.GetUserInfo("openid") if u.ID != testUserID || err != nil { - t.Errorf("GetUserInfo must return valid test ID=" + testUserID) + t.Errorf("GetUserInfo must return valid test ID=%s", testUserID) } } diff --git a/order_test.go b/order_test.go index 14e9870..31829b4 100644 --- a/order_test.go +++ b/order_test.go @@ -11,12 +11,12 @@ func TestGetOrder(t *testing.T) { o, err := c.GetOrder(testOrderID) if err != nil || o.ID != testOrderID { - t.Errorf("GetOrder failed for ID=" + testOrderID) + t.Errorf("GetOrder failed for ID=%s", testOrderID) } o, err = c.GetOrder(testFakeOrderID) if err == nil { - t.Errorf("GetOrder must return error for ID=" + testFakeOrderID) + t.Errorf("GetOrder must return error for ID=%s", testFakeOrderID) } } diff --git a/payment_test.go b/payment_test.go index 5fd8f8c..ee3e2ac 100644 --- a/payment_test.go +++ b/payment_test.go @@ -2,7 +2,6 @@ package paypalsdk import ( "fmt" - "strconv" "testing" ) @@ -42,7 +41,7 @@ func TestGetPayments(t *testing.T) { payments, _ := c.GetPayments() if len(payments) != 2 { - t.Errorf("2 payments must be returned for GetPayments. Returned: " + strconv.Itoa(len(payments))) + t.Errorf("2 payments must be returned for GetPayments. Returned: %d", len(payments)) } } diff --git a/sale_test.go b/sale_test.go index 0f6093e..544e3a6 100644 --- a/sale_test.go +++ b/sale_test.go @@ -11,7 +11,7 @@ func TestGetSale(t *testing.T) { _, err := c.GetSale(testSaleID) if err == nil { - t.Errorf("404 must be returned for ID=" + testSaleID) + t.Errorf("404 must be returned for ID=%s", testSaleID) } else { fmt.Println(err.Error()) } @@ -23,14 +23,14 @@ func TestRefundSale(t *testing.T) { _, err := c.RefundSale(testSaleID, nil) if err == nil { - t.Errorf("404 must be returned for ID=" + testSaleID) + t.Errorf("404 must be returned for ID=%s", testSaleID) } else { fmt.Println(err.Error()) } _, err = c.RefundSale(testSaleID, &Amount{Total: "7.00", Currency: "USD"}) if err == nil { - t.Errorf("404 must be returned for ID=" + testSaleID) + t.Errorf("404 must be returned for ID=%s", testSaleID) } else { fmt.Println(err.Error()) } @@ -42,7 +42,7 @@ func TestGetRefund(t *testing.T) { _, err := c.GetRefund("1") if err == nil { - t.Errorf("404 must be returned for ID=" + testSaleID) + t.Errorf("404 must be returned for ID=%s", testSaleID) } else { fmt.Println(err.Error()) } diff --git a/types.go b/types.go index f4c8096..00b53c7 100644 --- a/types.go +++ b/types.go @@ -13,6 +13,9 @@ const ( // APIBaseLive points to the live version of the API APIBaseLive = "https://api.paypal.com" + + // RequestNewTokenBeforeExpiresIn is used by SendWithAuth and try to get new Token when it's about to expire + RequestNewTokenBeforeExpiresIn = 60 ) type (