Created
October 28, 2015 15:55
-
-
Save rococodogs/e663716f1d9159fc1589 to your computer and use it in GitHub Desktop.
code used to test the OCLC Notification API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var WSKey = require('oclc-wskey') | |
| var https = require('https') | |
| var fs = require('fs') | |
| var wskey = { | |
| public: 'give me a public key!', | |
| secret: 'give me a secret key!' | |
| } | |
| var key = new WSKey(wskey.public, wskey.secret); | |
| var host = 'circ.sd00.worldcat.org' | |
| var path = '/circ/notification' | |
| var sig = key.HMACSignature('GET', 'https://' + host + path) | |
| var reqOpts = { | |
| hostname: host, | |
| port: 443, | |
| path: path, | |
| headers: { | |
| 'Accept': 'application/json', | |
| 'Authorization': sig | |
| } | |
| } | |
| https.get(reqOpts, function (res) { | |
| var body = '' | |
| res.setEncoding('utf-8') | |
| res.on('data', function (d) { body += d }) | |
| res.on('end', function () { | |
| var parsed = JSON.parse(body) | |
| var results = parsed.entry[0].notificationMetaData | |
| for (var i = 0; i < results.length; i++) { | |
| requestNotification(results[i].fileId) | |
| } | |
| }) | |
| // res.pipe(process.stdout) | |
| }) | |
| function requestNotification (id) { | |
| var newPath = path + '/get?id=' + id | |
| var opts = { | |
| hostname: host, | |
| port: 443, | |
| path: newPath, | |
| headers: { | |
| 'Accept': 'application/json', | |
| 'Authorization': key.HMACSignature('GET', 'https://' + host + newPath) | |
| } | |
| } | |
| https.get(opts, function (res) { | |
| // res.pipe(process.stdout) | |
| var body = '' | |
| res.setEncoding('utf-8') | |
| res.on('data', function (d) { body += d }) | |
| res.on('end', function () { | |
| var parsed = JSON.parse(body) | |
| var b64 = new Buffer(parsed.entry[0].content, 'base64') | |
| fs.createWriteStream('./' + parsed.entry[0].metaData.fileName).write(b64) | |
| }) | |
| }) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment