From ba0fb56eea9b881ba03f0189000ae3569815f594 Mon Sep 17 00:00:00 2001 From: Aliaksandr Pliutau Date: Fri, 20 Nov 2015 09:57:45 +0700 Subject: [PATCH] SendWithAuth --- client.go | 9 +++++++++ payment.go | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/client.go b/client.go index 499d341..a838018 100644 --- a/client.go +++ b/client.go @@ -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) diff --git a/payment.go b/payment.go index 66718d2..9c4333d 100644 --- a/payment.go +++ b/payment.go @@ -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)