Skip to content
This repository was archived by the owner on May 25, 2021. It is now read-only.

Commit 4d6f5b3

Browse files
committed
Update readme, further error handling
1 parent f33c7cb commit 4d6f5b3

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

README.md

+22
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,25 @@ There are several additional functions available to simplify frequent calls:
4747
'/Meetings?startDate=05-01-2015&endDate=05-03-2015';
4848

4949
api.call(options, callback);
50+
51+
## Caching
52+
53+
Caching is turned on by default. Queries are cached for a period of 12 hours.
54+
The cache can be turned off by invoking the following function:
55+
56+
api.useCache(false);
57+
58+
or individually, for each query by specifying an optional parameter in the
59+
options object (defaults to the global useCache, which is set above, or true by
60+
default):
61+
62+
options.useCache = false;
63+
64+
Additionally, we fall back on old cached data (even if it's older than 12 hours)
65+
if the API is unavailable or returns an error for any reason. Otherwise, you
66+
will get back from the server a response code 500, with an error object and
67+
message like so:
68+
69+
{
70+
"error": "Error getting access token: UNABLE_TO_VERIFY_LEAF_SIGNATURE"
71+
}

api.js

+16
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,22 @@ module.exports.call = function(options, callback) {
7474
}
7575
},
7676
getCallback = function(response) {
77+
// non-standard response
78+
if(response.statusCode !== 200) {
79+
if(options.useCache && cache.isCached(options) >= 0) {
80+
// the object is cached, even if it's old,
81+
// let's return that object.
82+
cache.getCachedResponse(options, callback);
83+
return;
84+
} else {
85+
// the server is unavailable, and the object isn't
86+
// cached at all, or the cache is off.
87+
// we return an error:
88+
callback({ error: 'UM API returned non-200 status code' });
89+
return;
90+
}
91+
}
92+
7793
var responseData = '';
7894

7995
response.on('data', function (chunk) {

cache.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ module.exports.isCached = function(options) {
1919
var timeDiff = Date.now() - cache[searchQuery].timestamp;
2020

2121
// (now - time saved) < 12 hours
22-
if(timeDiff < 15000) {
23-
// if(timeDiff < 43200000) {
22+
if(timeDiff < 43200000) {
2423
return 1;
2524
} else {
2625
return 0;

token.js

+7
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ var getAccessToken = function(callback) {
5252
}
5353
},
5454
postCallback = function(response) {
55+
// non-standard response
56+
if(response.statusCode !== 200) {
57+
callback({ error: 'UM access token request returned non-200' +
58+
'status code' });
59+
return;
60+
}
61+
5562
var tokenData = '';
5663
response.on('data', function (chunk) {
5764
tokenData += chunk;

0 commit comments

Comments
 (0)