/
contacts.go
109 lines (93 loc) · 2.74 KB
/
contacts.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
package hubspotclient
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
)
type Contact struct {
ID string `json:"id"`
Properties Properties `json:"properties"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
type Properties struct {
FirstName string `json:"firstname"`
LastName string `json:"lastname"`
Email string `json:"email"`
Company string `json:"company"`
Owner string `json:"hubspot_owner_id"`
Phone string `json:"phone"`
DeveloperSetup string `json:"developer_setup"`
Domain string `json:"hs_email_domain"`
Title string `json:"jobtitle"`
OriginalSource string `json:"hs_analytics_source"`
OriginalSourceData2 string `json:"hs_analytics_source_data_2"`
CreatedAccount string `json:"created_account"`
CreatedOrganization string `json:"created_organization"`
InvitedToOrganization string `json:"invited_to"`
FirstConversion string `json:"first_conversion_event_name"`
RecentConversion string `json:"recent_conversion_event_name"`
}
// Event source- if apply- hs_analytics_source_data_2
type Paging struct {
Next struct {
After string `json:"after"`
} `json:"next"`
}
type ContactResponse struct {
Results []Contact `json:"results"`
Paging Paging `json:"paging"`
}
func (c *HubspotClient) ListContacts() ([]Contact, error) {
contacts := make([]Contact, 0)
properties := []string{"firstname",
"lastname",
"email",
"company",
"hubspot_owner_id",
"phone",
"developer_setup",
"hs_email_domain",
"jobtitle",
"hs_analytics_source",
"hs_analytics_source_data_2",
"created_account",
"created_organization",
"invited_to",
"first_conversion_event_name",
"recent_conversion_event_name"}
params := strings.Join(properties, "&properties=")
after := ""
for {
req, err := http.NewRequest("GET", fmt.Sprintf("https://api.hubapi.com/crm/v3/objects/contacts?limit=100&properties=%s%s", params, after), nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.token))
req.Header.Set("Accept", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var contactResponse ContactResponse
err = json.Unmarshal(body, &contactResponse)
if err != nil {
return nil, err
}
contacts = append(contacts, contactResponse.Results...)
if contactResponse.Paging.Next.After == "" {
break
}
after = fmt.Sprintf("&after=%s", contactResponse.Paging.Next.After)
}
return contacts, nil
}