This repository has been archived by the owner on Jun 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmartyStreets.cfc
215 lines (141 loc) · 5.72 KB
/
SmartyStreets.cfc
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
component displayname="SmartyStreets" hint="LiveAddress API v2.3.1 wrapper" {
/*
* Version 1.0 — Dec 1, 2011
* Home page: https://github.com/sgalashyn/smartystreets
* API docs: http://wiki.smartystreets.com/liveaddress_api_users_guide
*/
variables.apikey = "";
variables.apiurl = "https://api.qualifiedaddress.com/street-address/";
variables.useragent = "";
variables.verbose = false;
// HTTP status codes map
variables.statuses = {
"200" = "Request completed successfully, inspect response body.",
"400" = "Malformed request – required fields missing from request.",
"401" = "Authentication failure – invalid credentials; check credentials and try again.",
"402" = "Unauthorized access; no active subscription can be found.",
"500" = "General service failure, retry request."
};
/*
* @apikey Valid API Key for your user account
* @useragent Custom useragent for HTTP requests
* @verbose Append extended info to the output
*/
public any function init(
required string apikey,
boolean verbose = false,
string useragent = server.ColdFusion.ProductName
)
hint="Component initialization" {
setApiKey(arguments.apikey);
setUserAgent(arguments.useragent);
setVerbose(arguments.verbose);
return this;
}
/*
* INTERACTION WITH API
*/
/*
* Required arguments are street AND either city/state OR zipcode OR city/zipcode OR lastline
*/
public any function invoke() hint="Perform request to the API and handle response" {
var local = {output = {}};
try {
// make simple validation of arguments
if (getApiKey() EQ "") {
throw(message="API key not set.");
}
param name="arguments.street" default="";
param name="arguments.city" default="";
param name="arguments.state" default="";
param name="arguments.zipcode" default="";
param name="arguments.lastline" default="";
if (arguments.street EQ "") {
throw(message="You must supply street address.");
}
if (arguments.city NEQ "" AND arguments.state NEQ "") {
// OK
}
else if (arguments.zipcode NEQ "") {
// OK
}
else if (arguments.lastline NEQ "") {
// OK
}
else {
throw(message="You must supply street AND city/state OR zipcode OR city/zipcode OR lastline.");
}
// send a request to the API (token must be in URL)
local.service = new http(
url = "#getApiUrl()#?auth-token=#URLEncodedFormat(getApiKey())#",
method = "post",
useragent = getUserAgent()
);
for (local.key in arguments) {
local.service.addParam(type="formfield", name=LCase(local.key), value=arguments[local.key]);
}
local.result = local.service.send().getPrefix();
if (getVerbose()) {
local.output.result = local.result;
}
// check if request is handled
if (local.result.responseheader.status_code NEQ 200) {
throw(message=getStatusDefinition(local.result.responseheader.status_code), detail=local.result.responseheader.explanation);
}
// try to parse the response
if (StructKeyExists(local.result, "filecontent") AND isJSON(local.result.filecontent)) {
local.output.fault = false;
local.output.data = DeserializeJSON(local.result.filecontent);
}
else if (StructKeyExists(local.result, "errordetail")) {
throw(message="API communication failure. #local.result.errordetail#");
}
else {
throw(message="API communication failure, or invalid (not JSON) response returned.");
}
}
catch (any exception) {
local.output.fault = true;
local.output.data = exception.Message;
if (exception.Detail NEQ "") {
local.output.data &= " " & exception.Detail;
}
if (getVerbose()) {
local.output.exception = exception;
}
}
return local.output;
}
/*
* HELPERS
*/
public void function setApiKey(required string apikey) hint="Set current API auth key setting" {
variables.apikey = arguments.apikey;
}
public string function getApiKey() hint="Get current API auth key setting" {
return variables.apikey;
}
public void function setUserAgent(required string useragent) hint="Set current useragent setting" {
variables.useragent = arguments.useragent;
}
public string function getUserAgent() hint="Get current useragent setting" {
return variables.useragent;
}
public string function getApiUrl() hint="Get current API URL" {
return variables.apiurl;
}
public void function setVerbose(required boolean verbose) hint="Set current verbose setting" {
variables.verbose = arguments.verbose;
}
public boolean function getVerbose() hint="Get current verbose setting" {
return variables.verbose;
}
private string function getStatusDefinition(required string code) hint="Get definition for status code" {
if (StructKeyExists(variables.statuses, arguments.code)) {
return variables.statuses[arguments.code];
}
else {
return "Unknown status code (#local.code#)";
}
}
}