-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
176 lines (147 loc) · 4.32 KB
/
index.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
var H = require('highland')
var request = require('request')
var getRequestOptions = function (path, token) {
var url = 'http://api.repo.nypl.org/api/v1/' + path
var auth = 'Token token="' + token + '"'
return {
url: url,
headers: {
Authorization: auth
}
}
}
var getCapturesRequestOptions = function (uuid, token, perPage, page) {
var path = 'items/' + uuid + '.json?withTitles=yes&per_page=' + perPage + '&page=' + page
return getRequestOptions(path, token)
}
var getMODSRequestOptions = function (uuid, token) {
return getRequestOptions('items/mods_captures/' + uuid, token)
}
var getUuidForLocalIdentifierOptions = function (fieldName, value, token) {
return getRequestOptions('items/' + fieldName + '/' + value, token)
}
var requestStream = function (options) {
return H(request(options))
.split()
.map(JSON.parse)
}
var getPageStreams = function (uuid, token, perPage, items) {
var count = Math.ceil(items / perPage)
var streams = []
for (var page = 1; page <= count; page++) {
streams.push(getCapturesRequestOptions(uuid, token, perPage, page))
}
return streams.map(function (options) {
return requestStream(options)
})
}
var getCaptures = function (body) {
return body.nyplAPI.response.capture
}
var checkUuid = function (options) {
if (!options.uuid) {
throw new Error('Please supply a UUID in options.uuid')
}
return options.uuid
}
var checkToken = function (options) {
var token = options.token || process.env.DIGITAL_COLLECTIONS_TOKEN
if (!token) {
throw new Error('Please supply an API token in options.token, or set the DIGITAL_COLLECTIONS_TOKEN environment variable')
}
return token
}
/**
* Returns a stream of capture objects
* @param {Object} options
* @param {String} options.uuid UUID of a Collection, Sub-container or Item
* @param {String} [options.token] Digital Collections API access token
* @param {number} [options.perPage=50] items per page, higher means less requests. Max. 500
*/
module.exports.captures = function (options) {
var uuid = checkUuid(options)
var token = checkToken(options)
if (options.perPage && options.perPage > 500 || options.perPage < 1) {
throw new Error('options.perPage should be between 1 and 500')
}
var perPage = options.perPage || 100
return requestStream(getCapturesRequestOptions(uuid, token, 1, 1))
.map(function (body) {
return body.nyplAPI.request.totalPages
})
.map(H.curry(getPageStreams, uuid, token, perPage))
.flatten()
.map(getCaptures)
.flatten()
}
/**
* Returns MODS records for capture
* @param {Object} options
* @param {String} options.uuid UUID of an Item
* @param {String} [options.token] Digital Collections API access token
*/
module.exports.mods = function (options, callback) {
let uuid
let token
try {
uuid = checkUuid(options)
token = checkToken(options)
} catch (err) {
callback(err)
return
}
request(getMODSRequestOptions(uuid, token), (error, response, body) => {
if (error) {
callback(error)
return
}
var parsedBody
try {
parsedBody = JSON.parse(body)
} catch (parseError) {
callback(parseError)
return
}
if (parsedBody && parsedBody.nyplAPI && parsedBody.nyplAPI.response && parsedBody.nyplAPI.response.mods) {
callback(null, parsedBody.nyplAPI.response.mods)
} else {
callback()
}
})
}
/**
* Returns UUID for local identifier
* @param {Object} options
* @param {String} options.fieldName a valid local identifier.
* @param {String} options.value value of local identifier
* @param {String} [options.token] Digital Collections API access token
*/
module.exports.uuidForLocalIdentifier = function (options, callback) {
let token
try {
token = checkToken(options)
} catch (err) {
callback(err)
return
}
const fieldName = options.fieldName
const value = options.value
request(getUuidForLocalIdentifierOptions(fieldName, value, token), (error, response, body) => {
if (error) {
callback(error)
return
}
let parsedBody
try {
parsedBody = JSON.parse(body)
} catch (parseError) {
callback(parseError)
return
}
if (parsedBody && parsedBody.nyplAPI && parsedBody.nyplAPI.response) {
callback(null, parsedBody.nyplAPI.response.uuid)
} else {
callback()
}
})
}