-
Notifications
You must be signed in to change notification settings - Fork 14
/
hubspot.go
203 lines (168 loc) · 4.59 KB
/
hubspot.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package gohubspot
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
const (
defaultBaseURL = "https://api.hubapi.com"
)
type HubspotClient struct {
authenticator Authenticator
common service
client *http.Client
BaseURL *url.URL
UserAgent string
// Services used for talking to different parts of the API
ContactLists *ContactListsService
Contacts *ContactsService
ContactProperties *ContactPropertiesService
CompanyProperties *CompanyPropertiesService
Companies *CompaniesService
Forms *FormService
}
type service struct {
client *HubspotClient
}
func NewHubspotOAuthClient(token string) *HubspotClient {
return NewHubspotClient(NewOAuth2(token))
}
func NewHubspotApiClient(apikey string) *HubspotClient {
return NewHubspotClient(NewAPIKeyAuth(apikey))
}
func NewHubspotClient(auth Authenticator) *HubspotClient {
r := &HubspotClient{authenticator: auth}
url, e := url.Parse(defaultBaseURL)
if e != nil {
panic(e)
}
r.BaseURL = url
r.client = http.DefaultClient
r.common.client = r
r.ContactLists = (*ContactListsService)(&r.common)
r.Contacts = (*ContactsService)(&r.common)
r.ContactProperties = (*ContactPropertiesService)(&r.common)
r.CompanyProperties = (*CompanyPropertiesService)(&r.common)
r.Companies = (*CompaniesService)(&r.common)
//r.Forms = (*FormService)(&r.common)
r.Forms = &FormService{service: r.common}
return r
}
// Post creates a new POST request
func (c *HubspotClient) Post(url string, body interface{}) (*http.Request, error) {
return c.NewRequest(http.MethodPost, url, body)
}
// Get creates a new GET API request
func (c *HubspotClient) Get(url string) (*http.Request, error) {
return c.NewRequest(http.MethodGet, url, nil)
}
func (c *HubspotClient) RunGet(url string, res interface{}) error {
if req, err := c.Get(url); err != nil {
return err
} else {
return c.Do(req, res)
}
}
func (c *HubspotClient) RunPost(url string, body, res interface{}) error {
if req, err := c.Post(url, body); err != nil {
return err
} else {
return c.Do(req, res)
}
}
func (c *HubspotClient) RunPut(url string, body, res interface{}) error {
if req, err := c.NewRequest(http.MethodPut, url, body); err != nil {
return err
} else {
return c.Do(req, res)
}
}
func (c *HubspotClient) RunDelete(url string, res interface{}) error {
if req, err := c.NewRequest(http.MethodDelete, url, nil); err != nil {
return err
} else {
return c.Do(req, res)
}
}
// NewRequest creates a new API request.
func (c *HubspotClient) NewRequest(method, urlStr string, body interface{}) (*http.Request, error) {
if strings.HasSuffix(c.BaseURL.Path, "/") {
return nil, fmt.Errorf("BaseURL must NOT have a trailing slash, but %q does not", c.BaseURL)
}
if !strings.HasPrefix(urlStr, "/") {
return nil, fmt.Errorf("urlStr must have begin with a slash, but %q does not", urlStr)
}
u, err := c.BaseURL.Parse(c.BaseURL.Path + urlStr)
if err != nil {
return nil, err
}
var buf io.ReadWriter
if body != nil {
buf = new(bytes.Buffer)
if e := json.NewEncoder(buf).Encode(body); e != nil {
return nil, e
}
}
req, err := http.NewRequest(method, u.String(), buf)
if err != nil {
return nil, err
}
// body, err = ioutil.ReadAll(req.Body)
// if err != nil {
// log.Fatalf("ERROR: %s", err)
// }
// fmt.Printf("%s", body)
if body != nil {
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
}
if c.UserAgent != "" {
req.Header.Set("User-Agent", c.UserAgent)
}
if e := c.authenticator.Authenticate(req); e != nil {
return nil, e
}
return req, nil
}
// Do sends an API request and returns the API response
func (c *HubspotClient) Do(req *http.Request, v interface{}) error {
resp, err := c.client.Do(req)
if err != nil {
return err
}
defer func() {
// Drain up to 512 bytes and close the body to let the Transport reuse the connection
io.CopyN(ioutil.Discard, resp.Body, 512)
resp.Body.Close()
}()
err = CheckResponse(resp)
if err != nil {
return err
}
if v != nil {
if w, ok := v.(io.Writer); ok {
io.Copy(w, resp.Body)
} else {
err = json.NewDecoder(resp.Body).Decode(v)
if err == io.EOF {
err = nil //ignore EOF erros caused by empty response body
}
}
}
return nil
}
func CheckResponse(r *http.Response) error {
if c := r.StatusCode; 200 <= c && c <= 299 {
return nil
}
data, err := ioutil.ReadAll(r.Body)
if err != nil {
return fmt.Errorf("Server error %d \n can not read body: %v", r.StatusCode, err)
}
return fmt.Errorf("Server error %d \n Body: %v", r.StatusCode, string(data[:]))
}