SendWithAuth

This commit is contained in:
Aliaksandr Pliutau 2015-11-20 09:57:45 +07:00
parent 5043557577
commit ba0fb56eea
2 changed files with 11 additions and 2 deletions

View File

@ -87,6 +87,15 @@ func (c *Client) Send(req *http.Request, v interface{}) error {
return nil
}
// 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
// making the main request
func (c *Client) SendWithAuth(req *http.Request, v interface{}) error {
req.Header.Set("Authorization", "Bearer "+c.Token.Token)
return c.Send(req, v)
}
func (c *Client) log(request *http.Request, response *http.Response) {
if c.LogFile != "" {
os.OpenFile(c.LogFile, os.O_CREATE, 0755)

View File

@ -22,7 +22,7 @@ func (c *Client) CreateDirectPaypalPayment(amount Amount, redirectURI string) (*
req.Header.Set("Authorization", "Bearer "+c.Token.Token)
p := PaymentResponse{}
err = c.Send(req, &p)
err = c.SendWithAuth(req, &p)
if p.ID == "" {
return &p, errors.New("Unable to create payment with this access token")
@ -43,7 +43,7 @@ func (c *Client) ExecuteApprovedPayment(paymentID string, payerID string) (*Exec
req.Header.Set("Authorization", "Bearer "+c.Token.Token)
e := ExecuteResponse{}
err = c.Send(req, &e)
err = c.SendWithAuth(req, &e)
if e.ID == "" {
return &e, errors.New("Unable to execute payment with paymentID=" + paymentID)