-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClient.cfc
More file actions
executable file
·648 lines (537 loc) · 20.5 KB
/
Client.cfc
File metadata and controls
executable file
·648 lines (537 loc) · 20.5 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/**
* Client interface for the WolfNet API.
*
* This class is a ColdFusion implementation of the WolfNet API Client. It is used to
* make requests to the API and receive responses from the API. The scope of this class should not
* extend beyond basic HTTP communication. Any other logic such as caching should be accomplished
* by decorating or advising this class.
*
* In order for the API client to perform requests to the API it must first prove to the API that it
* has valid credentials to do so, namely a valid and active *API key*. With the API key the client
* can retrieve an API token (see API documentation) which is then used to make any subsequent
* requests.
*/
component
{
/* PROPERTIES ******************************************************************************* */
/**
* The hostname for the API where requests will be sent.
* @type string
*/
property name="host";
/**
* The API version that will be interacted with.
* @type numeric
*/
property name="version";
/**
* The number of seconds before HTTP requests should timeout.
* @type numeric
*/
property name="timeout";
/**
* An instance of a logger object which has the same interface as a LogBox logger.
*/
property name="logger";
/* CONSTRUCTOR ****************************************************************************** */
/**
* Constructor Method
*
* This constructor method instantiates the ApiClient class and allows the consumer to specify
* details about what API should be interacted with.
*
* @param string host The hostname for the API where requests will be sent.
* @param integer version The API version that will be interacted with.
*
* @return Client
*
*/
public Client function init(
string host="api.wolfnet.com",
numeric version=1,
numeric timeout=5
) {
variables.host = arguments.host;
variables.version = arguments.version;
variables.timeout = arguments.timeout;
return this;
}
/* PUBLIC METHODS *************************************************************************** */
/**
* This method is used to authenticate with the API and retrieve an API token which is needed
* to perform any other requests to the API.
*
* @param string key API key to be used for authentication.
* @param struct headers The HTTP headers to be sent with the request.
* @param struct options Extra options that may be passed into the request. This parameter
* mostly exists to facilitate the decorators.
*
* @return struct The API response structure.
*
*/
public struct function authenticate(
required string key,
struct headers={},
struct options={}
) {
var data = {
'key' = arguments.key,
'v' = variables.version,
};
return performRequest(
'/core/auth',
'POST',
data,
arguments.headers ?: {}
);
}
/**
* This method makes pre-authenticated requests to the WolfNet API and returns the response.
*
* @param string token The API token that should be used to the API requests.
* @param string resource The API endpoint the request should be made to.
* @param string method The HTTP verb that should be used to make the request.
* @param struct data Any data that should be passed along with the request.
* @param struct headers The HTTP headers to be sent with the request.
* @param struct options Extra options that may be passed into the request. This parameter
* mostly exists to facilitate the decorators.
*
* @return struct An array containing the HTTP response.
*
*/
public struct function sendRequest(
required string token,
required string resource,
required string method = "GET",
struct data = {},
struct headers = {},
struct options = {}
) {
arguments.headers['api_token'] = arguments.token;
return performRequest(
arguments.resource,
arguments.method,
arguments.data,
arguments.headers
);
}
/* PRIVATE METHODS ************************************************************************** */
/**
* This method takes in request parameters and performs HTTP requests to the WolfNet API.
*
* @param string resource The API endpoint the request should be made to.
* @param string method The HTTP verb that should be used to make the request.
* @param array data Any data that should be passed along with the request.
* @param array headers The HTTP headers to be sent with the request.
*
* @throws wolfnet.api.client This exception is thrown any time there is an issue
* with the request. This exception should then be caught
* later and displayed as a user friendly message.
*
* @return struct An array containing the HTTP response.
*/
private struct function performRequest(
required string resource,
required string method = "GET",
struct data = {},
struct headers = {}
) {
this.log('info', 'Starting API request.', arguments);
// Make sure the method is valid.
validateMethod(arguments.method);
try {
arguments.uri = uriFromResource(arguments.resource);
validateRequestData(arguments.data);
}
catch (wolfnet.api.client exception) {
throw(type=exception.type,
message=exception.message,
detail=exception.detail,
extendedInfo=serializeJson(arguments) & ' ' & exception.extendedInfo
);
}
// TODO: Implement CF HTTP request.
var httpObj = new http();
httpObj.setTimeOut(variables.timeout);
httpObj.setUrl(arguments.uri);
httpObj.setMethod(arguments.method);
// For now the client will only accept JSON responses.
arguments.headers['Accept'] = 'application/json';
// Adding HTTP Encoding header.
arguments.headers['Accept-Encoding'] = 'gzip, deflate';
// Apply data to the requests
// Depending on the method we will pass data in the request differently.
switch (arguments.method) {
case "GET":
for (var dataKey in arguments.data) {
httpObj.addParam(type="url", name=dataKey, value=arguments.data[dataKey]);
}
break;
case "POST":
for (var dataKey in arguments.data) {
httpObj.addParam(type="formField", name=dataKey, value=arguments.data[dataKey]);
}
break;
case "PUT":
httpObj.addParam(type="header", name="Content-Type", value="application/json");
httpObj.addParam(type="body", value=serializeJSON(arguments.data));
break;
}
// Append any header data to the HTTP object.
for (var headerKey in arguments.headers) {
httpObj.addParam(type="header", name=headerKey, value=arguments.headers[headerKey]);
}
// Perform the actual HTTP request.
var httpResponse = httpObj.send();
var httpPrefix = httpResponse.getPrefix();
/* Since the response validator doesn't know anything about the request we want to catch
any exceptions generated by it and add some extra information. */
try {
validateResponse(httpPrefix);
}
catch (wolfnet.api.client exception) {
throw(type=exception.type,
message=exception.message,
detail=exception.detail,
extendedInfo=serializeJson(arguments) & ' ' & exception.extendedInfo
);
}
return parseResponse(httpPrefix, arguments);
}
/**
* This method turns a resource string into a fully qualified URL using the API host and port
* that were passed into the constructor of API Client class.
*
* @param string resource The API resource (endpoint) be be converted to a URL.
*
* @return string A fully qualified URL to the API.
*
*/
private string function uriFromResource(required string resource)
{
// TODO: Add more validation criteria.
// If the resource does not start with a leading slash it is not valid.
if (left(arguments.resource, 1) != "/") {
throw(type="wolfnet.api.client.InvalidResource",
message="API Resource Invalid",
detail="Resources being requested from the API must start with '/'. ",
extendedInfo=arguments.resource
);
}
return variables.host & arguments.resource;
}
/**
* This method validates the data to be sent with the HTTP request.
*
* Specifically we are checking to make sure that the data which is being sent to the API can be
* easily converted into a format which works with basic HTTP requests. This means we only want
* Scalar values such as numbers, strings, and booleans.
*
* @param array data The data to be validated.
*
* @throws Wolfnet_Api_ApiException This exception is thrown if any of the data that was
* given does not meet the validation criteria.
*
* @return null
*/
private void function validateRequestData(required struct data)
{
// Loop over each key in the data and check if they are scalar (simple) values.
for (var dataKey in arguments.data) {
if (!isSimpleValue(arguments.data[dataKey])) {
throw(type='wolfnet.api.client.InvalidData',
message='API Invalid Request Data',
detail='All API arguments must be scalar values. [#dataKey#] is not a valid API argument. ',
extendedInfo=serializeJson([dataKey, arguments.data[dataKey]])
);
}
}
}
/**
* This method validates that a given method string matches one that is supported by the API.
*
* @param {string} method The HTTP method the request should be submitted as.
*
* @return {Boolean} Is the method valid? true/false
*
*/
private void function validateMethod(required string method)
{
var validMethods = 'GET,POST,PUT,DELETE';
if (listFindNoCase(validMethods, arguments.method) == 0) {
throw(type="wolfnet.api.client.InvalidMethod",
message="API Method Invalid",
detail="The method used for the API request is not valid. Valid methods include #validMethods#.",
extendedInfo=arguments.method
);
}
}
/**
* This method validates the HTTP response from the API. If the response does not pass validation
* an exception is thrown which can be caught and acted upon later.
*
* @param mixed response A response from the CF HTTP Request
*
* @return null
*
*/
private void function validateResponse(required struct response)
{
if (!structKeyExists(arguments.response, 'status_code')) {
throw(type="wolfnet.api.client.ConnectionFailure",
message="API Connection Failure",
detail="The application server was unable to make an HTTP connection with the API server. ",
extendedInfo=serializeJSON(arguments.response)
);
}
var responseText = arguments.response.status_text ?: '';
var responseCode = arguments.response.status_code;
if (responseCode == 408) {
throw(type="wolfnet.api.client.RequestTimeout",
message="API Request Timeout",
detail="The API request did not complete within the #variables.timeout# second time limit. ",
extendedInfo=serializeJSON(arguments.response)
);
}
var mimeType = arguments.response.mimetype ?: '';
var responseBody = arguments.response.filecontent ?: '{}';
if (mimeType == 'application/json') {
try {
var responseData = deserializeJson(responseBody);
} catch (Any exception) {
throw(type="wolfnet.api.client.InvalidJsonResponse",
message="API JSON Invalid",
detail="An error occurred while attempting to deserialize the API response JSON.",
extendedInfo=serializeJSON(arguments.response)
);
}
} else {
// The response was not JSON so we don't have anything to parse.
// Create an empty struct to prevent errors later.
var responseData = {};
}
var metadata = responseData.metadata ?: {};
var status = metadata.status ?: {};
var errorCode = status.errorCode ?: '';
var statusCode = status.statusCode ?: '';
var errorID = status.error_id ?: '';
var message = status.message ?: '';
// TODO: These two variables are used repeatedly below. Could be done better.
var errorIDMessage = (errorID != '') ? 'API Error ID: #errorID#' : '';
var errorMessage = (message != '') ? 'The API says: [#message#]' : '';
// Here we will handle special API error responses.
/**
* The API has indicated that the request was made with an invalid key.
*/
if (errorCode == 'Auth1001' || statusCode == 'Auth1001') {
throw(type='wolfnet.api.client.Unauthorized.InvalidKey',
message='API Key Invalid',
detail='The WolfNet API has responded that provided API key is not valid. '
& errorIDMessage & ' ' & errorMessage,
extendedInfo=serializeJSON(arguments.response)
);
}
/**
* The API has indicated that the request was made to a site that is disabled.
*/
if (errorCode == 'Auth1002' || statusCode == 'Auth1002' || errorCode == 'S3001' || statusCode == 'S3001') {
throw(type='wolfnet.api.client.Unauthorized.SiteDisabled',
message='API Site Disabled',
detail='The WolfNet API has responded that the site is disabled. '
& errorIDMessage & ' ' & errorMessage,
extendedInfo=serializeJSON(arguments.response)
);
}
/**
* The API has indicated that the request was made to a site that is disabled.
*/
if (errorCode == 'Auth1002P' || statusCode == 'Auth1002P') {
throw(type='wolfnet.api.client.Unauthorized.SitePending',
message='API Site Pending',
detail='The WolfNet API has responded that the site is pending. '
& errorIDMessage & ' ' & errorMessage,
extendedInfo=serializeJSON(arguments.response)
);
}
/**
* The API has indicated that the request was made with a disabled API key.
*/
if (errorCode == 'Auth1003' || statusCode == 'Auth1003' || errorCode == 'S2001' || statusCode == 'S2001') {
throw(type='wolfnet.api.client.Unauthorized.KeyDisabled',
message='API Key Disabled',
detail='The WolfNet API has responded that it did not receive a enabled API key. '
& errorIDMessage & ' ' & errorMessage,
extendedInfo=serializeJSON(arguments.response)
);
}
/**
* The API has indicated that the request was made but the data can only be accessed by
* a user who has authenticated (double opt-in) their account.
*/
if (errorCode == 'Auth1004' || statusCode == 'Auth1004') {
throw(type='wolfnet.api.client.Unauthorized.User',
message='User Not Authenticated',
detail='The WolfNet API has responded the data requested can only be viewed by '
& 'a user that has authenticated their account. '
& errorIDMessage & ' ' & errorMessage,
extendedInfo=serializeJSON(arguments.response)
);
}
/**
* The API has indicated that the request was made without a valid API token so we will
* throw a special exception that we can can catch and attempt to re-authenticate.
*/
if (errorCode == 'Auth1005' || statusCode == 'Auth1005') {
throw(type='wolfnet.api.client.Unauthorized.API',
message='API Client Not Authenticated',
detail='The WolfNet API has responded that it did not receive a valid API token. '
& errorIDMessage & ' ' & errorMessage,
extendedInfo=serializeJSON(arguments.response)
);
}
/**
* The API has indicated that the email address used to attempt an account creation already exists.
*/
if (errorCode == 'Res1001' || statusCode == 'Res1001') {
throw(type='wolfnet.api.client.Resource.Exists',
message='API Resource Already Exists',
detail='The WolfNet API has responded that the resource that was attempting to '
& 'be created already exists.'
& errorIDMessage & ' ' & errorMessage,
extendedInfo=serializeJSON(arguments.response)
);
}
// The API returned a 400 Bad Response
// There are several reasons why this might have happened so we should check for those
if (responseCode == 400) {
throw(type='wolfnet.api.client.BadResponse',
message='API 400 Bad Request',
detail='The WolfNet API has indicated that the request was "bad" for an unknown reason. '
& errorIDMessage & ' ' & errorMessage,
extendedInfo=serializeJSON(arguments.response)
);
}
// The API returned a 401 Unauthorized
if (responseCode == 401) {
throw(type='wolfnet.api.client.Unauthorized',
message='API 401 Unauthorized',
detail='The WolfNet API has indicated that the request was made '
& 'without properly authentication. '
& errorIDMessage & ' ' & errorMessage,
extendedInfo=serializeJSON(arguments.response)
);
}
// The API returned a 403 Forbidden
if (responseCode == 403) {
throw(type='wolfnet.api.client.Forbidden',
message='API 403 Forbidden',
detail='An attempt was made to request data that is not available to the key that '
& 'was used to authenticate the request. '
& errorIDMessage & ' ' & errorMessage,
extendedInfo=serializeJSON(arguments.response)
);
}
// The API returned a 404 Not Found
if (responseCode == 404) {
throw(type='wolfnet.api.client.NotFound',
message='API 404 Not Found',
detail='The WolfNet API has indicated that the requested resource could not be '
& 'found. '
& errorIDMessage & ' ' & errorMessage,
extendedInfo=serializeJSON(arguments.response)
);
}
// The API returned a 500 Internal Server Error
if (responseCode == 500) {
throw(type='wolfnet.api.client.InternalServerError',
message='API 500 Internal Server Error',
detail='The WolfNet API appears to be unresponsive at this time. '
& errorIDMessage & ' ' & errorMessage,
extendedInfo=serializeJSON(arguments.response)
);
}
// The API returned a 503 Service Unavailable
if (responseCode == 503) {
throw(type='wolfnet.api.client.ServerUnavailable',
message='API 503 Server Unavailable',
detail='The WolfNet API appears to be unresponsive at this time but should be back soon. '
& errorIDMessage & ' ' & errorMessage,
extendedInfo=serializeJSON(arguments.response)
);
}
/**
* This response code we received is not code 200. We don't know how to deal with this
* response at this time so we will need to throw an exception.
*/
if (responseCode != 200) {
throw(type='wolfnet.api.client.UnknownResponse',
message='API Unknown Response',
detail='The WolfNet API responded that the this client is not prepared to handle. '
& 'Status: #responseCode# #responseText#; '
& errorIDMessage & ' ' & errorMessage,
extendedInfo=serializeJSON(arguments.response)
);
}
// We got this far so add the response data to the response so we don't have to parse it again.
arguments.response.responseData = responseData;
}
/**
* This method is used to abstract a raw CF HTTP response from the API into a format that
* we control, in this case an array. This way if the WP response changes we only have one place
* in our code to change.
*
* Our structure currently contains for request and response data to make debugging easier.
*
* NOTE: This method expects that the response is an array at this point. If the response is not
* and array it should have been caught by the validation method (validateResponse) and then
* resulted in an exception.
*
* @param array response The CF HTTP API response.
* @param string uri The request URI.
* @param string method The request HTTP verb.
* @param array requestData The request data.
*
* @return array A uniform array of request and response data.
*
*/
private struct function parseResponse(
required struct res,
required struct req
) {
var formattedResponse = {
'requestUrl' = arguments.req.uri,
'requestMethod' = arguments.req.method,
'requestData' = arguments.req.data,
'requestHeaders' = arguments.req.headers,
'responseStatusCode' = arguments.res.status_code,
'responseData' = arguments.res.filecontent,
'timestamp' = Now(),
'fromCache' = false,
};
var mimeType = arguments.res.mimetype ?: '';
if (arguments.res.mimetype == 'application/json') {
// If possible use the parsed data from the response validation process.
if (arguments.res.keyExists('responseData') && isStruct(arguments.res.responseData)) {
formattedResponse['responseData'] = arguments.res.responseData;
} else {
try {
formattedResponse['responseData'] = deserializeJson(arguments.res.filecontent);
} catch (Any exception) {
throw(type="wolfnet.api.client.InvalidJsonResponse",
message="API JSON Invalid",
detail="An error occurred while attempting to deserialize the API response JSON.",
extendedInfo=serializeJSON(arguments)
);
}
}
}
return formattedResponse;
}
private void function log(required string type, required string message, any data)
{
var logger = variables.logger ?: {};
if (structCount(logger) > 0 && structKeyExists(logger, arguments.type)) {
logger[arguments.type](arguments.message, arguments.data ?: '');
}
}
}