forked from socketlabs/socketlabs-go
/
socketlabsclient.go
191 lines (151 loc) · 5.66 KB
/
socketlabsclient.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
package injectionapi
import (
"bytes"
"encoding/json"
"net/http"
"net/url"
"runtime"
"github.com/socketlabs/socketlabs-go/injectionapi/core"
"github.com/socketlabs/socketlabs-go/injectionapi/core/serialization"
"github.com/socketlabs/socketlabs-go/injectionapi/message"
)
const endpointURL = "https://inject.socketlabs.com/api/v1/email"
// ISocketlabsClient is used to easily send messages through the Socketlabs Injection API
type ISocketlabsClient interface {
// SendBasic sends a basic email message and returns the response from the Injection API.
SendBasic(message *message.BasicMessage) (SendResponse, error)
// SendBulk sends a bulk email message and returns the response from the Injection API.
SendBulk(message *message.BulkMessage) (SendResponse, error)
// SetEndpointURL sets the API endpoint. Under normal circumstances this should not be used.
SetEndpointURL(endpointURL string)
// SetProxyUrl sets the proxy url.
SetProxyURL(proxyURL string)
// GetEndpointURL retreives the API endpoint.
GetEndpointURL() string
}
// socketlabsClient is the default ISocketlabsClient implementation
type socketlabsClient struct {
ServerID int
APIKey string
EndpointURL string
ProxyURL string
}
// CreateClient instatiates new client using the specified credentials
func CreateClient(serverID int, apiKey string) ISocketlabsClient {
return &socketlabsClient{
ServerID: serverID,
APIKey: apiKey,
EndpointURL: endpointURL,
}
}
// CreateClientWithProxy instatiates new client using the specified credentials
func CreateClientWithProxy(serverID int, apiKey string, proxyURL string) ISocketlabsClient {
return &socketlabsClient{
ServerID: serverID,
APIKey: apiKey,
EndpointURL: endpointURL,
ProxyURL: proxyURL,
}
}
// SetEndpointURL sets the API endpoint. Under normal circumstances this should not be used.
func (socketlabsClient *socketlabsClient) SetEndpointURL(endpointURL string) {
socketlabsClient.EndpointURL = endpointURL
}
// SetProxyUrl sets the proxy url.
func (socketlabsClient *socketlabsClient) SetProxyURL(proxyURL string) {
socketlabsClient.ProxyURL = proxyURL
}
// GetEndpointURL retreives the API endpoint.
func (socketlabsClient *socketlabsClient) GetEndpointURL() string {
return socketlabsClient.EndpointURL
}
// SendBasic sends a basic email message and returns the response from the Injection API.
func (socketlabsClient *socketlabsClient) SendBasic(message *message.BasicMessage) (SendResponse, error) {
sendResponse := SendResponse{
Result: SendResultUNKNOWNERROR,
}
validator := sendValidator{}
sendResponse = validator.ValidateCredentials(socketlabsClient.ServerID, socketlabsClient.APIKey)
if sendResponse.Result != SendResultSUCCESS {
sendResponse.ResponseMessage = sendResponse.Result.ToResponseMessage()
return sendResponse, nil
}
sendResponse = validator.ValidateBasicMessage(*message)
if sendResponse.Result != SendResultSUCCESS {
sendResponse.ResponseMessage = sendResponse.Result.ToResponseMessage()
return sendResponse, nil
}
//create injection request from factory
factory := core.InjectionRequestFactory{
ServerID: socketlabsClient.ServerID,
APIKey: socketlabsClient.APIKey,
}
request := factory.GenerateBasicRequest(message)
return socketlabsClient.sendInjectionRequest(&request)
}
// SendBulk sends a bulk email message and returns the response from the Injection API.
func (socketlabsClient *socketlabsClient) SendBulk(message *message.BulkMessage) (SendResponse, error) {
sendResponse := SendResponse{
Result: SendResultUNKNOWNERROR,
}
validator := sendValidator{}
sendResponse = validator.ValidateCredentials(socketlabsClient.ServerID, socketlabsClient.APIKey)
if sendResponse.Result != SendResultSUCCESS {
sendResponse.ResponseMessage = sendResponse.Result.ToResponseMessage()
return sendResponse, nil
}
sendResponse = validator.ValidateBulkMessage(*message)
if sendResponse.Result != SendResultSUCCESS {
sendResponse.ResponseMessage = sendResponse.Result.ToResponseMessage()
return sendResponse, nil
}
//create injection request from factory
factory := core.InjectionRequestFactory{
ServerID: socketlabsClient.ServerID,
APIKey: socketlabsClient.APIKey,
}
request := factory.GenerateBulkRequest(message)
return socketlabsClient.sendInjectionRequest(&request)
}
func (socketlabsClient socketlabsClient) sendInjectionRequest(injectionRequest *serialization.InjectionRequestJson) (sendResponse SendResponse, err error) {
//attempt to serialize injection request
serializedRequest, err := json.Marshal(injectionRequest)
if err != nil {
return SendResponse{}, err
}
//create request
req, err := http.NewRequest("POST", socketlabsClient.GetEndpointURL(), bytes.NewBuffer(serializedRequest))
if err != nil {
return SendResponse{}, err
}
//add headers
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", "socketlabs-go/1.0.1 ("+runtime.Version()+")")
//create http client
client, err := socketlabsClient.createHttpClient(socketlabsClient.ProxyURL)
if err != nil {
return SendResponse{}, err
}
//issue http request
resp, err := client.Do(req)
if err != nil {
return SendResponse{}, err
}
//map to response and return
return injectionResponseParser{}.Parse(resp)
}
func (socketlabsClient *socketlabsClient) createHttpClient(proxyUrl string) (*http.Client, error) {
//create http client
if socketlabsClient.ProxyURL == "" {
return &http.Client{}, nil
}
//attempt to parse proxy url
var proxyURL *url.URL
proxyURL, err := url.Parse(socketlabsClient.ProxyURL)
if err != nil {
return nil, err
}
//create client with proxy url
client := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)}}
return client, nil
}