-
Notifications
You must be signed in to change notification settings - Fork 79
/
intercom.go
128 lines (112 loc) · 4.35 KB
/
intercom.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
package intercom
import (
"gopkg.in/intercom/intercom-go.v2/interfaces"
)
// A Client manages interacting with the Intercom API.
type Client struct {
// Services for interacting with various resources in Intercom.
Admins AdminService
Companies CompanyService
Contacts ContactService
Conversations ConversationService
Events EventService
Jobs JobService
Messages MessageService
Segments SegmentService
Tags TagService
Users UserService
// Mappings for resources to API constructs
AdminRepository AdminRepository
CompanyRepository CompanyRepository
ContactRepository ContactRepository
ConversationRepository ConversationRepository
EventRepository EventRepository
JobRepository JobRepository
MessageRepository MessageRepository
SegmentRepository SegmentRepository
TagRepository TagRepository
UserRepository UserRepository
// AppID For Intercom.
AppID string
// APIKey for Intercom's API. See http://app.intercom.io/apps/api_keys.
APIKey string
// HTTP Client used to interact with the API.
HTTPClient interfaces.HTTPClient
baseURI string
clientVersion string
debug bool
}
const (
defaultBaseURI = "https://api.intercom.io"
clientVersion = "2.0.0"
)
type option func(c *Client) option
// Set Options on the Intercom Client, see TraceHTTP, BaseURI and SetHTTPClient.
func (c *Client) Option(opts ...option) (previous option) {
for _, opt := range opts {
previous = opt(c)
}
return previous
}
// NewClient returns a new Intercom API client, configured with the default HTTPClient.
func NewClient(appID, apiKey string) *Client {
intercom := Client{AppID: appID, APIKey: apiKey, baseURI: defaultBaseURI, debug: false, clientVersion: clientVersion}
intercom.HTTPClient = interfaces.NewIntercomHTTPClient(intercom.AppID, intercom.APIKey, &intercom.baseURI, &intercom.clientVersion, &intercom.debug)
intercom.setup()
return &intercom
}
// NewClientWithHTTPClient returns a new Intercom API client, configured with the supplied HTTPClient interface
func NewClientWithHTTPClient(appID, apiKey string, httpClient interfaces.HTTPClient) *Client {
intercom := Client{AppID: appID, APIKey: apiKey, baseURI: defaultBaseURI, debug: false, clientVersion: clientVersion, HTTPClient: httpClient}
intercom.setup()
return &intercom
}
// TraceHTTP turns on HTTP request/response tracing for debugging.
func TraceHTTP(trace bool) option {
return func(c *Client) option {
previous := c.debug
c.debug = trace
return TraceHTTP(previous)
}
}
// BaseURI sets a base URI for the HTTP Client to use. Defaults to "https://api.intercom.io".
// Typically this would be used during testing to point to a stubbed service.
func BaseURI(baseURI string) option {
return func(c *Client) option {
previous := c.baseURI
c.baseURI = baseURI
return BaseURI(previous)
}
}
// SetHTTPClient sets a HTTPClient for the Intercom Client to use.
// Useful for customising timeout behaviour etc.
func SetHTTPClient(httpClient interfaces.HTTPClient) option {
return func(c *Client) option {
previous := c.HTTPClient
c.HTTPClient = httpClient
c.setup()
return SetHTTPClient(previous)
}
}
func (c *Client) setup() {
c.AdminRepository = AdminAPI{httpClient: c.HTTPClient}
c.CompanyRepository = CompanyAPI{httpClient: c.HTTPClient}
c.ContactRepository = ContactAPI{httpClient: c.HTTPClient}
c.ConversationRepository = ConversationAPI{httpClient: c.HTTPClient}
c.EventRepository = EventAPI{httpClient: c.HTTPClient}
c.JobRepository = JobAPI{httpClient: c.HTTPClient}
c.MessageRepository = MessageAPI{httpClient: c.HTTPClient}
c.SegmentRepository = SegmentAPI{httpClient: c.HTTPClient}
c.TagRepository = TagAPI{httpClient: c.HTTPClient}
c.UserRepository = UserAPI{httpClient: c.HTTPClient}
c.Admins = AdminService{Repository: c.AdminRepository}
c.Companies = CompanyService{Repository: c.CompanyRepository}
c.Contacts = ContactService{Repository: c.ContactRepository}
c.Conversations = ConversationService{Repository: c.ConversationRepository}
c.Events = EventService{Repository: c.EventRepository}
c.Jobs = JobService{Repository: c.JobRepository}
c.Messages = MessageService{Repository: c.MessageRepository}
c.Segments = SegmentService{Repository: c.SegmentRepository}
c.Tags = TagService{Repository: c.TagRepository}
c.Users = UserService{Repository: c.UserRepository}
}