-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathebayapi.js
333 lines (314 loc) · 10.9 KB
/
ebayapi.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
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
/**
* The eBay Shopping/Finding API JavaScript Client
* The client works by performing JSONP requests. The client also supports AJAX if JQuery is installled.
* @author : Rajat Mittal
* @version : 1.0
* @param params[Object] - parameter object
* {
* appId (required): application id
* mode (optional) : request mode. One of the following two types ('AJAX', 'JSONP') [ Default : 'JSONP' ]
* url (optional) : base url . Only required when mode is AJAX. It substitutes the constant
* SHOPPING_API/FINDING_API with the provided url.
* }
*/
function ebay( params ){
if ( params.appId ){
this.appId = params.appId;
this._responseFormat = 'JSON';
this.callback = params.callback;
this.mode = params.mode || 'JSONP';
if ( this.mode === 'AJAX' && params.url && params.url !== '' ){
ebay.SHOPPING_API = params.url + '?callname=';
ebay.FINDING_API = params.url + '?&SERVICE-VERSION=1.0.0&REST-PAYLOAD&OPERATION-NAME=';
}
}
}
ebay.SHOPPING_API = 'http://open.api.ebay.com/shopping?callname=';
ebay.FINDING_API = 'http://svcs.ebay.com/services/search/FindingService/v1?&SERVICE-VERSION=1.0.0&REST-PAYLOAD&OPERATION-NAME=';
/**
* Utility function to generate linear unique numbers to supplement unique callbacknames, unique script sources
* @return [number] - counter
*/
ebay._getCallId = (function(){
var callId = 0;
return function(){
return callId++;
}
})();
/**
* Utility function to merge two objects
* @param source[Object]- Object to merge from
* @param target[Object]- Object to merge into
* @return [Object] - final object composed after merging source object in the target object
*/
ebay._merge = function(source,target){
for ( attrs in source) {
if ( source.hasOwnProperty ( attrs ) ){ //dont navigate the prototype chain.
target[attrs] = source[attrs];
}
}
return target;
};
/**
* Remove unneccesary fields from the response.
* @param response[Object] - response object to cleanup
*/
ebay._cleanResponse = function(response){
delete response.Ack;
delete response.Build;
delete response.Version;
delete response.Timestamp;
return response;
};
/**
* Executes a particular call
* @param contextObj - Object defining the call and its primary parameter
* @param props[Object] - object holding additional request parameters
* @param callback[Function] - Reference to callback fn to report the response of this call to. If not specified,
* the object's callback fn is called.
*/
ebay._execute = function(contextObj,props,callback){
var queryObj = ebay._merge(props,contextObj);
var query = this._buildQuery(queryObj);
this._performQuery(query,callback);
};
/**
* A modified curry function to allow partial functions inside the ebay client
*/
ebay._curry = function() {
var args = Array.prototype.slice.call(arguments);
var fn = args.shift();
return function() {
return fn.apply(this, args.concat(
Array.prototype.slice.call(arguments)));
};
};
/**
* Creates a JSONP call
* @param url- url to do the request at. The url should be ending with the query string param for JSONP
* as defined by the provider of the JSONP service.
* @param callback[Function] - Reference to callback fn to report the response of this call to.
*/
ebay._getJSON = function(url,callback){
var script = document.createElement('SCRIPT');
var tempCallbackName = 'JSONP' + ebay._getCallId();
script.src = url + tempCallbackName;
window[tempCallbackName] = function(response){
callback(response);
delete window[tempCallbackName];
script.parentNode.removeChild(script);
};
document.getElementsByTagName('HEAD')[0].appendChild(script);
};
/**
* Constructs a complete URL for making a GET request
* props[Object] - property object
* {
* callName - name of the actual ebay api call to make [REQUIRED]
* All call specific parameters
* }
*/
ebay.prototype._buildQuery = function(props){
function adder(){
delete props.callName;
var urlSuffix = '';
for( var key in props ){
if ( props.hasOwnProperty(key) ){
urlSuffix += '&' + key + '=' + props[key];
}
}
return urlSuffix;
}
if ( props.callName ){
if ( props.type === 'FINDING' ){
var url = ebay.FINDING_API + props.callName + '&SECURITY-APPNAME=' + this.appId + '&RESPONSE-DATA-FORMAT=' + this._responseFormat;
url = url + adder() + '&callback=';
}
else{
var url = ebay.SHOPPING_API + props.callName + '&appid=' + this.appId + '&version=525' + '&responseencoding=' + this._responseFormat;
url = url + adder() + '&callbackname=';
}
return url;
}
return false;
};
/**
* Do the actual query
* @param query - URL
* @callback - call specific callback fn. If not provided, than the callback function attached to the ebay object is used
*/
ebay.prototype._performQuery = function(query,callback){
var self = this;
if ( self.mode === 'AJAX' ){
jQuery.ajax({
url : query,
success: function(response){
callback = callback || self.callback;
callback( ebay._cleanResponse(response) );
}
});
}
else{
ebay._getJSON(query, function(response){
if ( callback ) {
callback( ebay._cleanResponse(response) );
}
else if ( self.callback ){
self.callback( ebay._cleanResponse(response) );
}
});
}
};
/**
* Get User Profiles
* @param userIds[Array] - Array of ids of the users
* @param props[Object] - object holding additional request parameters ( so called IncludeSelectors )
* @param callback[Function] - Reference to callback fn to report this response to. If not specified,
* the object's callback fn is called.
*/
ebay.prototype.getUsers = function(userIds,props,callback){
var noOfUsers = userIds.length;
if ( noOfUsers > 1 ){
var finalResponse = [];
var callbackCount = 0;
var self = this;
var callbackIdentifier = '_tempCallback' + ebay._getCallId();
this[callbackIdentifier] = function(insertAt,response){
finalResponse[insertAt] = response;
callbackCount++;
if( callbackCount === noOfUsers ){
var callback = callback || self.callback;
delete self[callbackIdentifier];
callback(finalResponse);
}
};
var position = 0;
while( userIds.length !== 0 ){
this.getUsers([userIds[0]],props,ebay._curry(this[callbackIdentifier],position));
userIds.shift();
position++;
}
}
else{
ebay._execute.call(this,{
callName : 'GetUserProfile',
'UserID' : userIds[0]
},props,callback);
}
};
/**
* Get Categories
* @param categoryIds[Array] - Array of ids of the categories
* @param props[Object] - object holding additional request parameters ( so called IncludeSelectors )
* @param callback[Function] - Reference to callback fn to report this response to. If not specified,
* the object's callback fn is called.
*/
ebay.prototype.getCategory = function(categoryIds,props,callback){
var noOfCategories = categoryIds.length;
if ( noOfCategories > 1 ){
var finalResponse = [];
var callbackCount = 0;
var self = this;
var callbackIdentifier = '_tempCallback' + ebay._getCallId();
this[callbackIdentifier] = function(insertAt,response){
finalResponse[insertAt] = response;
callbackCount++;
if( callbackCount === noOfCategories ){
var callback = callback || self.callback;
delete self[callbackIdentifier];
callback(finalResponse);
}
};
var position = 0;
while( categoryIds.length !== 0 ){
this.getCategory([categoryIds[0]],props,ebay._curry(this[callbackIdentifier],position));
categoryIds.shift();
position++;
}
}
else{
ebay._execute.call(this,{
callName : 'GetCategoryInfo',
CategoryID : categoryIds[0]
},props,callback);
}
};
/**
* Get Items
* @param items[Array] - Array of item ids
* @param props[Object] - object holding additional request parameters ( so called IncludeSelectors )
* @param callback[Function] - Reference to callback fn to report this response to. If not specified,
* the object's callback fn is called.
*/
ebay.prototype.getItems = function(items,props,callback){
var multipleItems = (items.length === 1)? false : true;
if( multipleItems ){
//make getMultipleItems call
var contextObj = {
callName : 'GetMultipleItems',
'ItemID' : items.join()
};
}
else{
//make getSingleItem call
var contextObj = {
callName : 'GetSingleItem',
'ItemID' : items[0]
};
}
ebay._execute.call(this,contextObj,props,callback);
};
/**
* Get Popular Items
* @param keywords[String] - list of keywords separated by space
* @param props[Object] - object holding additional request parameters
* @param callback[Function] - Reference to callback fn to report this response to. If not specified,
* the object's callback fn is called.
*/
ebay.prototype.getPopularItems = function(keywords,props,callback){
ebay._execute.call(this,{
callName : 'FindPopularItems',
'QueryKeywords' : keywords
},props,callback);
};
/**
* Get Search Items from the FINDING API based upon Keywords
* @param keywords[String] - list of keywords separated by space
* @param props[Object] - object holding additional request parameters
* @param callback[Function] - Reference to callback fn to report this response to. If not specified,
* the object's callback fn is called.
*/
ebay.prototype.searchByKeywords = function(keywords,props,callback){
ebay._execute.call(this,{
callName : 'findItemsByKeywords',
'keywords' : keywords,
type : 'FINDING'
},props,callback);
};
/**
* Get Items Listings in a particular category from the FINDING API
* @param categoryId[String] - categoryId
* @param props[Object] - object holding additional request parameters
* @param callback[Function] - Reference to callback fn to report this response to. If not specified,
* the object's callback fn is called.
*/
ebay.prototype.getItemsInCategory = function(categoryId,props,callback){
ebay._execute.call(this,{
callName : 'findItemsByCategory',
'categoryId' : categoryId,
type : 'FINDING'
},props,callback);
};
/**
* Get Product info
* @param keywords[String] - keywords for the item
* @param props[Object] - object holding additional request parameters
* @param callback[Function] - Reference to callback fn to report this response to. If not specified,
* the object's callback fn is called.
*/
ebay.prototype.getProductInfo = function(keywords,props,callback){
ebay._execute.call(this,{
callName : 'FindProducts',
'QueryKeywords' : keywords
},props,callback);
};