This repository has been archived by the owner on Jun 21, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
client.go
148 lines (119 loc) · 3.33 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
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
package clearbit
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
// These are the valid services and resources of the Clearbit API.
const (
ProspectURL = "https://prospector.clearbit.com/v1/people/search"
EnrichCombinedStreamingURL = "https://person.clearbit.com/v2/combined/find"
EnrichCompanyStreamingURL = "https://company-stream.clearbit.com/v2/companies/find"
EnrichPersonStreamingURL = "https://person-stream.clearbit.com/v2/people/find"
)
// Client provides access to the Clearbit API.
type Client struct {
apiKey string
httpClient *http.Client
}
// NewClient initializes a Clearbit API client with the provided apiKey.
//
// If httpClient is nil, http.DefaultClient will be used.
func NewClient(apiKey string, httpClient *http.Client) *Client {
if httpClient == nil {
httpClient = http.DefaultClient
}
return &Client{
apiKey: apiKey,
httpClient: httpClient,
}
}
// Enrich finds a person by their email address
// and returns detailed information about both them
// as well as their company
func (c *Client) Enrich(email string) (*CombinedResponse, error) {
var combined *CombinedResponse
err := c.get(
EnrichCombinedStreamingURL,
url.Values{"email": []string{email}},
&combined,
)
return combined, err
}
// EnrichPerson finds a person by their email address
// and returns detailed information about them.
func (c *Client) EnrichPerson(email string) (*Person, error) {
var person *Person
err := c.get(
EnrichPersonStreamingURL,
url.Values{"email": []string{email}},
&person,
)
return person, err
}
// EnrichCompany finds a company by its domain
// and returns detailed information about it.
func (c *Client) EnrichCompany(domain string) (*Company, error) {
var company *Company
err := c.get(
EnrichCompanyStreamingURL,
url.Values{"domain": []string{domain}},
&company,
)
return company, err
}
// ProspectQuery defines the available options for querying
// the Prospector API.
type ProspectQuery struct {
// Company's domain name to look up (required).
Domain string
// Filters results by first or last name (case-insensitive).
Name string
// Filters results by job role (case-sensitive).
Role string
// Filters results by job seniority (case-sensitive).
Seniority string
// Filters results by one or more titles.
Titles []string
}
// Prospect finds a company by its domain name and returns basic
// information about the people working there.
func (c *Client) Prospect(q ProspectQuery) ([]*Prospect, error) {
var prospects []*Prospect
err := c.get(
ProspectURL,
url.Values{
"domain": []string{q.Domain},
"email": []string{"true"},
"name": []string{q.Name},
"role": []string{q.Role},
"seniority": []string{q.Seniority},
"titles[]": q.Titles,
},
&prospects,
)
return prospects, err
}
func (c *Client) get(endpoint string, params url.Values, v interface{}) error {
req, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
return err
}
req.SetBasicAuth(c.apiKey, "")
req.URL.RawQuery = params.Encode()
resp, err := c.httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return fmt.Errorf("clearbit HTTP error %d: %s", resp.StatusCode, data)
}
return json.Unmarshal(data, v)
}