-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
117 lines (90 loc) · 2.5 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package gopaypal
import (
"bytes"
"encoding/json"
"github.com/kataras/go-errors"
"io/ioutil"
"net/http"
"time"
)
type PayPalError struct {
Name string `json:"name"`
Message string `json:"message"`
ErrorDescription string `json:"error_description"`
}
// Client gopaypal client for communicating with the PayPal REST API endpoints
type Client struct {
baseURL string
clientID string
secret string
AccessToken *oauthResponse
}
// NewClient creates and returns a new gopaypal client with the given credentials
func NewClient(clientID, secret, base string) Client {
return Client{
baseURL: base,
clientID: clientID,
secret: secret,
AccessToken: &oauthResponse{},
}
}
// Execute runs the given HTTP request
func (c Client) Execute(req *http.Request) ([]byte, error) {
// Create a new HTTP client
client := http.Client{}
// Execute request
res, err := client.Do(req)
if err != nil {
return nil, err
}
// Close response body
defer res.Body.Close()
// Read the whole response body
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
// If invalid request parse error
if res.StatusCode != 200 && res.StatusCode != 201 {
e := PayPalError{}
// Unmarshal error message
if err := json.Unmarshal(b, &e); err != nil {
return nil, err
}
return nil, errors.New(e.Message)
}
return b, nil
}
// BasicRequest creates a basic request to the PayPal endpoint without the Authorization header
func (c Client) BasicRequest(endpoint string, b []byte, method string) (*http.Request, error) {
// Wrap byte array on a io.Reader
buff := bytes.NewBuffer(b)
// Create HTTP request
req, err := http.NewRequest(method, c.baseURL+endpoint, buff)
if err != nil {
return nil, err
}
// Add accept header
req.Header.Add("Accept", "application/json")
// Add accept language header
req.Header.Add("Accept-Language", "en_US")
return req, nil
}
// AuthRequest creates a basic request to the PayPal endpoint with the Authorization header set
func (c *Client) AuthRequest(endpoint string, b []byte, method string) (*http.Request, error) {
// Create basic request
req, err := c.BasicRequest(endpoint, b, method)
if err != nil {
return nil, err
}
// Check if access token is expired
if time.Now().After(c.AccessToken.Expires) {
// Get new access token
if _, err := c.GetAccessToken(); err != nil {
return nil, err
}
}
// Set authorization header
req.Header.Set("Authorization", "Bearer "+c.AccessToken.AccessToken)
return req, nil
}