-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
70 lines (58 loc) · 1.99 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
package intuit
import (
"encoding/json"
"encoding/xml"
"fmt"
"github.com/MattNewberry/oauth"
"net/http"
)
func post(endpoint string, body interface{}, params map[string]string, headers map[string][]string) (interface{}, error) {
return request(POST, endpoint, body, params, headers)
}
func get(endpoint string, params map[string]string) (interface{}, error) {
return request(GET, endpoint, "", params, nil)
}
func put(endpoint string, body interface{}, params map[string]string, headers map[string][]string) (interface{}, error) {
return request(PUT, endpoint, body, params, headers)
}
func request(method string, endpoint string, body interface{}, params map[string]string, headers map[string][]string) (data interface{}, err error) {
if SessionConfiguration.oAuthToken == nil {
SessionConfiguration.oAuthToken, err = MakeSamlAssertion()
if err != nil {
return
}
}
c := oauth.NewConsumer(
SessionConfiguration.OAuthConsumerKey,
SessionConfiguration.OAuthConsumerSecret,
oauth.ServiceProvider{})
c.AdditionalHeaders = map[string][]string{
"Accept": []string{"application/json"},
"Content-Type": []string{"application/xml"},
}
for k, v := range headers {
c.AdditionalHeaders[k] = v
}
url := fmt.Sprintf("%s%s", BaseURL, endpoint)
var res *http.Response
if method == GET {
res, err = c.Get(url, params, SessionConfiguration.oAuthToken)
} else if method == POST {
payload, _ := xml.MarshalIndent(body, " ", " ")
res, err = c.Post(url, string(payload), params, SessionConfiguration.oAuthToken)
} else if method == PUT {
payload, _ := xml.MarshalIndent(body, " ", " ")
res, err = c.Put(url, string(payload), params, SessionConfiguration.oAuthToken)
} else if method == DELETE {
res, err = c.Delete(url, params, SessionConfiguration.oAuthToken)
}
if err == nil {
d := json.NewDecoder(res.Body)
d.UseNumber()
err = d.Decode(&data)
} else {
httpError := err.(oauth.HTTPExecuteError)
json.Unmarshal(httpError.ResponseBodyBytes, &data)
}
return data, err
}