This repository has been archived by the owner on Nov 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathEmailValidationClient.js
177 lines (145 loc) · 4.76 KB
/
EmailValidationClient.js
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
var request = require('request');
var WaitForCompletionOptions = require('./WaitForCompletionOptions');
var EmailValidationClient = function(restClient) {
this.restClient = restClient;
};
EmailValidationClient.prototype.VALIDATION_STATUS_PENDING = 'pending';
EmailValidationClient.prototype.VALIDATION_STATUS_COMPLETED = 'completed';
EmailValidationClient.prototype.submit = function(emailAddresses, options) {
var callback = options instanceof Function ? options : (options && options.callback) || (()=>{});
var completionOptions = (options && options.waitForCompletion);
if (typeof emailAddresses === 'string' || emailAddresses instanceof String) {
emailAddresses = [ emailAddresses ];
}
var entries = [];
for (var idx = 0; idx < emailAddresses.length; idx++) {
entries.push({
inputData: emailAddresses[idx]
});
}
var $this = this;
request({
url: this.restClient.baseUrl + this.restClient.apiVersion + '/email-validations',
method: 'POST',
body: {
entries: entries
},
json: true,
auth: {
user: this.restClient.accountSid,
pass: this.restClient.authToken
},
header: {
Accept: 'application/json'
}
},
function onResponse(error, response, body) {
if (error) {
return callback(error, null);
}
switch (response.statusCode) {
case 202: { // Accepted
response.status = $this.VALIDATION_STATUS_PENDING;
if (completionOptions) {
return setTimeout(function onTimeout() {
queryUntilDone($this, body.uniqueID, completionOptions, callback);
},
completionOptions.pollingInterval || WaitForCompletionOptions.DEFAULT_QUERY_POLLING_INTERVAL);
}
return callback(null, body);
}
case 200: { // OK
response.status = $this.VALIDATION_STATUS_COMPLETED;
return callback(null, body);
}
case 404: // Not found
case 410: // Gone
return callback(null, null);
}
return callback('Unexpected HTTP status code: ' + response.statusCode + '. ' + response.statusMessage, null);
});
};
var queryOnce = function($this, uniqueID, requestTimeout, callback) {
request({
url: $this.restClient.baseUrl + $this.restClient.apiVersion + '/email-validations/' + uniqueID,
json: true,
auth: {
user: $this.restClient.accountSid,
pass: $this.restClient.authToken
},
header: {
Accept: 'application/json'
},
timeout: requestTimeout
},
function onResponse(error, response, body) {
if (error) {
return callback(error, null);
}
switch (response.statusCode) {
case 200: { // OK
body.status = $this.VALIDATION_STATUS_COMPLETED;
return callback(null, body);
}
case 202: { // Accepted
body.status = $this.VALIDATION_STATUS_PENDING;
return callback(null, body);
}
case 404: // Not found
case 410: // Gone
return callback(null, null);
}
return callback('Unexpected HTTP status code: ' + response.statusCode + '. ' + response.statusMessage, null);
});
}
var queryUntilDone = function($this, uniqueID, completionOptions, callback) {
queryOnce($this, uniqueID, completionOptions.requestTimeout || WaitForCompletionOptions.DEFAULT_REQUEST_TIMEOUT, function onResponse(error, data) {
if (error) {
return callback(error, null);
}
if (data != null) {
if (data.status === $this.VALIDATION_STATUS_PENDING) {
return setTimeout(function onTimeout() {
queryUntilDone($this, uniqueID, completionOptions, callback);
}, completionOptions.pollingInterval || WaitForCompletionOptions.DEFAULT_QUERY_POLLING_INTERVAL);
}
}
callback(null, data);
});
};
EmailValidationClient.prototype.query = function(uniqueID, options) {
var callback = options instanceof Function
? options
: (options && options.callback) || (()=>{});
var completionOptions = (options && options.waitForCompletion);
if (!completionOptions) {
return queryOnce(this, uniqueID, WaitForCompletionOptions.DEFAULT_REQUEST_TIMEOUT, callback);
}
var $this = this;
queryUntilDone(this, uniqueID, completionOptions, callback);
};
EmailValidationClient.prototype.delete = function(uniqueID, options) {
var callback = options instanceof Function
? options
: (options && options.callback) || (()=>{});
request({
url: this.restClient.baseUrl + this.restClient.apiVersion + '/email-validations/' + uniqueID,
method: 'DELETE',
auth: {
user: this.restClient.accountSid,
pass: this.restClient.authToken
}
},
function onResponse(error, response, body) {
if (error) {
return callback(error, null);
}
switch (response.statusCode) {
case 200: { // OK
return callback(null, null);
}
}
return callback('Unexpected HTTP status code: ' + response.statusCode + '. ' + response.statusMessage, null);
});
};
module.exports = EmailValidationClient;