-
Notifications
You must be signed in to change notification settings - Fork 1
/
apiRequest.js
336 lines (235 loc) · 7.97 KB
/
apiRequest.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
334
335
336
//talks to the server
"use strict"
var request = require('request'),
apiConfig = require('./apiConfig'),
querystring = require('querystring'),
apiCache = require('./apiCache'),
apiProcess = require('./apiProcess'),
Q = require('q')
var exports = module.exports = {}
//setup the constants
var numberOfRecords = apiConfig.search.numberOfRecords
//the user can reference these search operator constants
exports.ONE_OR_MORE = apiConfig.search.operatorOneOrMore
exports.EXACT = apiConfig.search.operatorExact
exports.ANY = apiConfig.search.operatorAny
exports.ALL = apiConfig.search.operatorAll
exports.LESS = apiConfig.search.operatorLess
exports.LESS_EQUAL = apiConfig.search.operatorLessEqual
exports.GRATER = apiConfig.search.operatorGrater
exports.GRATER_EQUAL = apiConfig.search.operatorGraterEqual
exports.NOT = apiConfig.search.operatorNot
//and sources for limit
exports.VIAF = apiConfig.search.limitSourceVIAF
exports.AUSTRALIA = apiConfig.search.limitSourceAustralia
exports.BELGIUM = apiConfig.search.limitSourceBelgium
exports.CANADA = apiConfig.search.limitSourceCanada
exports.CZECH = apiConfig.search.limitSourceCzech
exports.EGYPT = apiConfig.search.limitSourceEgypt
exports.FRANCE_BNF = apiConfig.search.limitSourceFranceBnF
exports.FRANCE_SUDOC = apiConfig.search.limitSourceFranceSudoc
exports.GERMANY = apiConfig.search.limitSourceGermany
exports.GETTY = apiConfig.search.limitSourceGetty
exports.HUNGARY = apiConfig.search.limitSourceHungary
exports.ISRAEL = apiConfig.search.limitSourceIsrael
exports.ITALY = apiConfig.search.limitSourceItaly
exports.LOC = apiConfig.search.limitSourceLOC
exports.NORWAY = apiConfig.search.limitSourceNorway
exports.POLAND = apiConfig.search.limitSourcePoland
exports.PORTUGAL = apiConfig.search.limitSourcePortugal
exports.SPAIN = apiConfig.search.limitSourceSpain
exports.SWEDEN = apiConfig.search.limitSourceSweden
exports.SWISS_NL = apiConfig.search.limitSourceSwissNL
exports.SWISS_RERO = apiConfig.search.limitSourceSwissRERO
exports.RUSSIA = apiConfig.search.limitSourceRussia
exports.VATICAN = apiConfig.search.limitSourceVatican
exports.XA = apiConfig.search.limitSourcexA
//bindings to the search function
exports.searchAny = function(query,options,cb){
return exports.search('searchAny',query,options,cb)
}
exports.searchServerChoice = function(query,options,cb){
return exports.search('searchServerChoice',query,options,cb)
}
exports.searchCorporate = function(query,options,cb){
return exports.search('searchCorporate',query,options,cb)
}
exports.searchGeographic = function(query,options,cb){
return exports.search('searchGeographic',query,options,cb)
}
exports.searchLCCN = function(query,options,cb){
return exports.search('searchLCCN',query,options,cb)
}
exports.searchPreferredName = function(query,options,cb){
return exports.search('searchPreferredName',query,options,cb)
}
exports.searchNames = function(query,options,cb){
return exports.search('searchNames',query,options,cb)
}
exports.searchPersonalNames = function(query,options,cb){
return exports.search('searchPersonalNames',query,options,cb)
}
exports.searchSourceRecord = function(query,options,cb){
return exports.search('searchSourceRecord',query,options,cb)
}
exports.searchTitle = function(query,options,cb){
return exports.search('searchTitle',query,options,cb)
}
exports.searchExpression = function(query,options,cb){
return exports.search('searchExpression',query,options,cb)
}
exports.searchWorks = function(query,options,cb){
return exports.search('searchWorks',query,options,cb)
}
//used for testing
exports.searchError = function(query,options,cb){
return exports.search('searchError',query,options,cb)
}
exports.search = function(type,query,options,cb){
var deferred = Q.defer();
var hasQuery = (typeof query === 'string') ? true : false,
hasOptions = (typeof(options) !== "function" && typeof(options) === "object" ) ? true : false,
hasCallback = (typeof(cb) === "function") ? true : false,
qs = ""
//just query and callback no options
if (!hasOptions){
if (typeof(options) === "function"){
var cb = options
hasCallback = true
}
}
//it should be empty if not passed
if (!hasOptions) var options = {}
if (!hasQuery) deferred.reject("No Query")
qs = exports.buildSearchUrl(type,query,options)
//see if this query is already in the cache if so return that instead
var cacheResults = apiCache.return(qs)
if (cacheResults){
//yes
if (options.raw){
deferred.resolve(cacheResults.raw)
}else{
//console.log("Using Cache")
//we have the cached response return it
deferred.resolve(cacheResults.data)
}
}else{
//make the request
request(qs, function (error, response, body) {
if (!body) var body = ""
if (error){
if (error.errno === 'ENOTFOUND'){
console.error("Cannot connect to the internet")
}
deferred.reject(error)
}else if (response.statusCode!=200){
deferred.reject(response.statusCode)
}else{
if (options.raw){
deferred.resolve(body)
}else{
//okay we want to process the record
var records = apiProcess.splitSearchResults(body, function(results){
var recordArray = []
//send each one to the processor
for (var x in results.records){
recordArray.push( apiProcess.combineResults(results.records[x]))
}
//cache it
apiCache.add(qs,recordArray,body)
//return it
deferred.resolve(recordArray)
})
}
}
})
}
deferred.promise.nodeify(cb)
return deferred.promise
}
//build up the query string based on the options, exposed for testing
exports.buildSearchUrl = function(type,query,options){
//defaults
var operator = exports.ALL,
ts = "",
maxRecord = apiConfig.config.numberOfRecords,
startRecord = 1,
acceptHeader = apiConfig.search.acceptXML,
limit = false
for (var x in options){
if (x === 'operator'){
operator = options.operator
}
if (x === 'limit'){
limit = options.limit
}
if (x === 'start'){
startRecord = options.start
}
}
//loop though the types of searchs and grab that string if the type matches
for (var x in apiConfig.search){
if (x === type){
ts = apiConfig.search[x]
}
}
//build query string, viaf uses "+" to seperate their operations we leave the base off until later to escape it all
var qs = apiConfig.search.base
//replace the query string and operator
qs += ts.replace('{X}',querystring.escape(query)).replace('{OP}',querystring.escape(operator))
if (limit){
qs += apiConfig.search.limitSourceSyntax.replace('{X}',limit)
}
//sort key
qs += "&" + apiConfig.search.sortKey
//how many records
qs += "&" + apiConfig.search.recordsMax.replace("{X}",maxRecord)
//start record
qs += "&" + apiConfig.search.recordsStart.replace("{X}",startRecord)
//return type
qs += "&" + acceptHeader
if (type==='searchError') qs = qs.replace('search?','searcherror?')
return qs
}
//get the viaf id and return a processed record
exports.getLccn = function(id,cb){
return exports.getViaf(id,cb)
}
//get the viaf id and return a processed record
exports.getViaf = function(id,cb){
var deferred = Q.defer();
if (!id) deferred.reject("No ID")
id=id.toString()
if (id.charAt(0)=='n'){
var qs = apiConfig.get.lccnURL.replace("{X}",id)
}else{
var qs = apiConfig.get.viafURL.replace("{X}",id)
}
//make the request
request(qs, function (error, response, body) {
if (!body) var body = ""
if (error){
if (error.errno === 'ENOTFOUND'){
console.error("Cannot connect to the internet")
}
deferred.reject(error)
}else if (response.statusCode!=200){
deferred.reject(response.statusCode)
}else{
//okay we want to process the record
var records = apiProcess.splitSearchResults(body, function(results){
var record = apiProcess.combineResults(results.records[0])
//return it
deferred.resolve(record)
})
}
})
deferred.promise.nodeify(cb)
return deferred.promise
}
exports.setNumberOfRecords = function(intVal){
apiConfig.numberOfRecords = intVal
}
exports.setCacheSize= function(intVal){
apiConfig.cacheSize = intVal
}