-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathedmunds.api.sdk.js
More file actions
257 lines (229 loc) · 6.53 KB
/
edmunds.api.sdk.js
File metadata and controls
257 lines (229 loc) · 6.53 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
/*
* This module is a collection of classes designed to make working with
* the Appigee App Services API as easy as possible.
* Learn more at http://apigee.com/docs/usergrid
*
* Copyright 2013 Edmunds.com, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author Ismail Elshareef (ielshareef@edmunds.com)
* @author Michael Bock (mbock@edmunds.com)
*/
/**
* The browser console
*
* @property console
* @private
* @type object
*/
window.console = window.console || {};
window.console.log = this.console.log || function() {};
/**
* Core functionality for the Edmunds API JavaScript SDK
*
* @class EDMUNDSAPI
*/
function EDMUNDSAPI(key) {
/**
* The SDK version
*
* @config _version
* @private
* @type string
*/
var _version = "0.1.7";
/**
* Assigned API Key. Register for an API Key <a href="http://developer.edmunds.com/apps/register">here</a>
*
* @config _api_key
* @private
* @type string
*/
var _api_key = key;
/**
* The base URL for the API
* http or https depending on the site, defaulting to http: if other.
*
* @property _base_url
* @private
* @type string
*/
if (window.location.protocol == "http:" || window.location.protocol == "https:") {
var _base_url = window.location.protocol + "//api.edmunds.com";
}
else {
var _base_url = "http://api.edmunds.com";
}
/**
* The base URL for photos
*
* @property _base_url
* @private
* @type string
*/
var _base_media = window.location.protocol + "//media.ed.edmunds-media.com";
/**
* The API response format
*
* @property _response_format
* @private
* @type string
*/
var _response_format = 'json';
/**
* Set the response format (json or xml)
*
* @method setOutput
* @param void
* @return {string} API version
*/
this.setOutput = function(format) {
_response_format = format;
return _response_format;
};
/**
* Serialize a JSON object into a key=value pairs
*
* @method _serializeParams
* @private
* @param object JSON object of parameters and their values
* @return {string} Serialized parameters in the form of a query string
*/
function _serializeParams(params) {
var str = '';
for(var key in params) {
if(params.hasOwnProperty(key)) {
if (str !== '') str += "&";
str += key + "=" + params[key];
}
}
return str;
}
/**
* The base URL for the API
*
* @method getBaseUrl
* @param void
* @return {string} API URL stub
*/
this.getBaseUrl = function() {
return _base_url;
};
/**
* The base URL for photos
*
* @method getVersion
* @param void
* @return {string} API version
*/
this.getBaseMediaUrl = function() {
return _base_media;
};
/**
* Make the API REST call
*
* @method api
* @param string method The API resource to be invoked
* @param object params JSON object of method parameters and their values
* @param function callback The JavaScript function to be invoked when the results are returned (JSONP implementation)
* @return {string} API REST call URL
*/
this.api = function(method, params, successCallback, errorCallback) {
var queryString = _serializeParams(params),
baseUrl = this.getBaseUrl();
queryString = (queryString) ? '?' + queryString + '&api_key=' + _api_key + "&fmt=" + _response_format : '?api_key=' + _api_key + "&fmt=" + _response_format;
return this.jsonp({
url: baseUrl + method + queryString,
timeout: 7000,
success: successCallback,
error: errorCallback,
cache: true
});
};
/**
* Make the JSONP call
*
* @method jsonp
* @param object options
* {
* "url" : [the API call],
* "timeout": [time in milliseconds of how long to wait for the script to execute],
* "success": [the function to be invoked upon success],
* "error": [the function to be invoked upon error],
* "cache": [let the Mashery cache be or bust it. Boolean value with default is FALSE (keep the cache)],
* }
*
* @return {string} API REST call URL
*/
this.jsonp = (function(global) {
'use strict';
var callbackId = 0,
documentHead = document.head || document.getElementsByTagName('head')[0];
function createScript(url) {
var script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('async', true);
script.setAttribute('src', url);
return script;
}
return function(options) {
options = options || {};
var callbackName = 'callback' + (new Date().getTime())+ (++callbackId),
url = options.url + '&callback=' + callbackName + (options.cache ? '' : '&_dc=' + new Date().getTime()),
script = createScript(url),
abortTimeout;
function cleanup() {
if (script) {
script.parentNode.removeChild(script);
}
clearTimeout(abortTimeout);
delete global[callbackName];
}
function success(data) {
if (typeof options.success === 'function') {
options.success(data);
}
}
function error(errorType) {
if (typeof options.error === 'function') {
options.error(errorType);
}
}
function abort(errorType) {
cleanup();
if (errorType === 'timeout') {
global[callbackName] = function() {};
}
error(errorType);
window.console.log(errorType+": One of the scripts failed to load ("+callbackName+")");
}
global[callbackName] = function(data) {
cleanup();
success(data);
};
script.onerror = function(e) {
abort(e);
};
documentHead.appendChild(script);
if (options.timeout > 0) {
abortTimeout = setTimeout(function() {
abort('timeout');
}, options.timeout);
}
};
}(window));
}
if (typeof window.sdkAsyncInit === "function") {
sdkAsyncInit();
}