-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
216 lines (192 loc) · 6.9 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
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
const avro = require('avsc')
const OAuth = require('oauth')
const Promise = require('promise')
const request = require('request')
const winston = require('winston')
const awsDecrypt = require('./helper/awsDecrypt.js')
const logger = require('./helper/logger.js')
// Initialize cache
var CACHE = {}
logger.info({'message': 'Loading MLN Bib Poster'})
// kinesis stream handler
exports.kinesisHandler = function (records, context, callback) {
logger.info({'message': 'Processing ' + records.length + ' records'})
// retrieve token and schema
Promise.all([token(), schema()])
.then(function (res) {
var accessToken = res[0]
var schema = res[1]
onReady(records, accessToken, schema)
})
// run when access token and schema are loaded
function onReady (payload, accessToken, schema) {
try {
// load avro schema
var avroType = avro.Type.forSchema(schema)
// parse payload
var records = payload
.map(function (record) {
return parseKinesis(record, avroType)
})
// post to API
updateCreateRecordsArray = []
deletedRecordsArray = []
records.forEach(function(record){
if(record.deleted){
deletedRecordsArray.push(record)
return;
}
if(record.materialType.value == "TEACHER SET"){
updateCreateRecordsArray.push(record)
} else {
logger.info({'message': 'Record has a value type of: ' + record.materialType.value + '. Therefore, will not send request to Rails API.'})
}
})
if (updateCreateRecordsArray.length != 0) postRecords(updateCreateRecordsArray, accessToken)
if (deletedRecordsArray.length != 0) deleteRecords(deletedRecordsArray, accessToken)
} catch (error) {
logger.error({'message': error.message, 'error': error})
CACHE['accessToken'] = null
callback(error)
}
}
// map to records objects as needed
function parseKinesis (payload, avroType) {
logger.info({'message': 'Parsing Kinesis'})
// decode base64
try{
var buf = new Buffer(payload.kinesis.data, 'base64')
// decode avro
var record = avroType.fromBuffer(buf)
logger.info({'message': 'Parsed Data' , 'data': record })
return record
}
catch (err) {
logger.error({'message': err.message, 'error': err})
callback(null)
}
}
// bulk posts records
//function postRecords (accessToken, records) {
function postRecords (records, accessToken) {
logger.info({'message': 'Posting records data:' + records})
var options = {
uri: process.env['MLN_API_URL'],
method: 'POST',
headers: { Authorization: `Bearer ${accessToken}` },
body: records,
json: true
}
request(options, function (error, response, body) {
logger.info({'message': 'Posting........'})
logger.info({'message': 'Response: ' + response.statusCode + " Records info:" + records})
if ([500, 401].includes(response.statusCode)) {
if (response.statusCode === 401) {
// Clear access token so new one will be requested on retried request
CACHE['accessToken'] = null
}
callback(new Error())
logger.error({'message': 'POST Error! ', 'response': response})
return
} else if ([400, 404].includes(response.statusCode)) {
logger.error({'message': 'Post api input validations failed!', 'response': response})
}
})
}
function deleteRecords(records, accessToken){
logger.info({'message': 'Deleting records'})
var options = {
uri: process.env['MLN_API_URL'],
method: 'DELETE',
headers: { Authorization: `Bearer ${accessToken}` },
body: records,
json: true
}
request(options, function (error, response, body) {
logger.info({'message': 'Deleting...'})
logger.info({'message': 'Response: ' + response.statusCode})
if ([500, 401].includes(response.statusCode)) {
if (response.statusCode === 401) {
// Clear access token so new one will be requested on retried request
CACHE['accessToken'] = null
}
callback(new Error())
logger.error({'message': 'DELETE Error! ', 'response': response})
return
}else if ([400, 404].includes(response.statusCode)) {
logger.error({'message': 'DELETE api input validations failed!', 'response': response})
}
})
}
function schema () {
// schema in cache; just return it as a instant promise
if (CACHE['schema']) {
logger.info({'message': 'Already have schema'})
return new Promise(function (resolve, reject) {
resolve(CACHE['schema'])
})
}
return new Promise(function (resolve, reject) {
var options = {
uri: process.env['NYPL_API_SCHEMA_URL'],
json: true
}
logger.info({'message': 'Loading schema...'})
request(options, function (error, resp, body) {
if (error) {
logger.info({'message': 'Error! ' + error})
reject(error)
}
if (body.data && body.data.schema) {
logger.info({'message': 'Sucessfully loaded schema'})
var schema = JSON.parse(body.data.schema)
CACHE['schema'] = schema
resolve(schema)
} else {
reject(new Error('Schema did not load'))
logger.error({'message': 'Schema did not load'})
}
})
})
}
// oauth token retriever
function token () {
// access token in cache; just return it as a instant promise
if (CACHE['accessToken']) {
logger.info({'message': 'Already authenticated'})
return new Promise(function (resolve, reject) {
resolve(CACHE['accessToken'])
})
}
// request a new token
logger.info({'message': 'Requesting new token...'})
return new Promise(function (resolve, reject) {
var OAuth2 = OAuth.OAuth2
var nyplOauthKey = awsDecrypt.decryptKMS(process.env['NYPL_OAUTH_KEY'])
var nyplOauthSecret = awsDecrypt.decryptKMS(process.env['NYPL_OAUTH_SECRET'])
Promise.all([nyplOauthKey, nyplOauthSecret])
.then((decryptedValues) => {
[nyplOauthKey, nyplOauthSecret] = decryptedValues;
var url = process.env['NYPL_OAUTH_URL']
var auth = new OAuth2(nyplOauthKey, nyplOauthSecret, url, null, 'oauth/token', null)
auth.getOAuthAccessToken('', { grant_type: 'client_credentials' }, function (error, accessToken, refreshToken, results) {
if (error) {
reject(error)
logger.error({'message': 'Not authenticated'})
} else {
logger.info({'message': 'Successfully authenticated'})
CACHE['accessToken'] = accessToken
resolve(accessToken)
}
})
})
})
}
}
// main function
exports.handler = function (event, context, callback) {
var record = event.Records[0]
if (record.kinesis) {
exports.kinesisHandler(event.Records, context, callback)
}
}