forked from jinmatt/go-quickbooks.v2
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquickbooks.go
185 lines (152 loc) · 3.82 KB
/
quickbooks.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package quickbooks
import (
"bytes"
"encoding/json"
"log"
"net/http"
"net/http/httputil"
"net/url"
"strconv"
"github.com/omniboost/go-quickbooks/sdk"
"github.com/omniboost/go-quickbooks/sdk/consts"
)
// Quickbooks client type
type Quickbooks struct {
RealmID string
AccessToken string
baseURL string
minorVersion int
// http client to use
http *http.Client
// Debugging flag
debug bool
}
// NewClient creates a new client to work with Quickbooks
func NewClient(httpClient *http.Client, realmID string) *Quickbooks {
if httpClient == nil {
httpClient = http.DefaultClient
}
q := Quickbooks{}
q.http = httpClient
q.RealmID = realmID
q.SetBaseURL(sdk.ProductionURL)
return &q
}
// makeGetRequest makes a GET request to Quickbooks API.
// endpoint should start with a leading '/'
func (q *Quickbooks) makeGetRequest(endpoint string) (*http.Response, error) {
urlStr := q.baseURL + endpoint
if q.MinorVersion() != 0 {
u, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
v, err := url.ParseQuery(u.RawQuery)
if err != nil {
return nil, err
}
v.Add("minorversion", strconv.Itoa(q.MinorVersion()))
u.RawQuery = v.Encode()
urlStr = u.String()
}
request, err := http.NewRequest("GET", urlStr, nil)
if err != nil {
return nil, err
}
// headers
request.Header.Set("accept", "application/json")
request.Header.Set("Authorization", "Bearer "+q.AccessToken)
if q.debug == true {
dump, _ := httputil.DumpRequestOut(request, true)
log.Println(string(dump))
}
response, err := q.http.Do(request)
if q.debug == true {
dump, _ := httputil.DumpResponse(response, true)
log.Println(string(dump))
}
if err != nil {
return nil, err
}
if response.StatusCode != 200 {
return nil, handleError(response)
}
return response, nil
}
// PostRequest makes a POST request to Quickbooks API.
// endpoint should start with a leading '/'
func (q *Quickbooks) makePostRequest(endpoint string, body interface{}) (*http.Response, error) {
urlStr := q.baseURL + endpoint
if q.MinorVersion() != 0 {
u, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
v, err := url.ParseQuery(u.RawQuery)
if err != nil {
return nil, err
}
v.Add("minorversion", strconv.Itoa(q.MinorVersion()))
u.RawQuery = v.Encode()
urlStr = u.String()
}
b, err := json.Marshal(body)
if err != nil {
return nil, err
}
request, err := http.NewRequest("POST", urlStr, bytes.NewReader(b))
if err != nil {
return nil, err
}
// headers
request.Header.Set("accept", "application/json")
request.Header.Set("Content-Type", "application/json")
request.Header.Set("Authorization", "Bearer "+q.AccessToken)
if q.debug == true {
dump, _ := httputil.DumpRequestOut(request, true)
log.Println(string(dump))
}
response, err := q.http.Do(request)
if q.debug == true && response != nil {
dump, _ := httputil.DumpResponse(response, true)
log.Println(string(dump))
}
if err != nil {
return nil, err
}
if response.StatusCode != 200 {
return nil, handleError(response)
}
return response, nil
}
func (q *Quickbooks) SetBaseURL(URL string) {
q.baseURL = URL
}
func (q *Quickbooks) SetDebug(debug bool) {
q.debug = debug
}
func (q *Quickbooks) SetMinorVersion(minorVersion int) {
q.minorVersion = minorVersion
}
func (q *Quickbooks) MinorVersion() int {
return q.minorVersion
}
func handleError(response *http.Response) error {
switch response.StatusCode {
case 400:
// b, errz := ioutil.ReadAll(response.Body)
// log.Println(string(b))
// log.Println(errz)
// os.Exit(2)
qbError := ErrorObject{}
err := json.NewDecoder(response.Body).Decode(&qbError)
if err != nil {
return err
}
return qbError
case 401:
sdkError := SDKError{}
return sdkError.New(consts.QBAuthorizationFault, consts.QBAuthenticationFaultCode, consts.QBAuthorizationFaultMessage)
}
return nil
}