Created
October 30, 2018 17:57
-
-
Save librarywebchic/23bffe62d40930e8f6683fc8eabc2697 to your computer and use it in GitHub Desktop.
Sample Code Change Location information
This file contains 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
"use strict"; | |
const nodeauth = require("nodeauth"); | |
const axios = require("axios"); | |
const xpath = require('xpath'); | |
const dom = require('xmldom').DOMParser; | |
const serializer = require('xmldom').XMLSerializer; | |
const builder = require('xmlbuilder'); | |
const serviceUrl = 'circ.sd00.worldcat.org/LHR'; | |
const options = { | |
services: ["WMS_COLLECTION_MANAGEMENT"] | |
}; | |
const user = new nodeauth.User('authenticating_institution_id', 'principalID', 'principalIDNS'); | |
const wskey = new nodeauth.Wskey('key', 'secret', options); | |
function findCopy(id, accessToken) { | |
let config = { | |
headers: { | |
'Authorization': 'Bearer ' + accessToken, | |
'User-Agent': 'node.js KAC client', | |
'Accept': 'application/atom+xml' | |
} | |
}; | |
let url = 'https://' + serviceUrl + '/' + id; | |
return new Promise(function (resolve, reject) { | |
axios.get(url, config) | |
.then(response => { | |
// parse out the User | |
resolve(response.data); | |
}) | |
.catch (error => { | |
reject(error); | |
}); | |
}); | |
} | |
function updateCopy(copy, id, accessToken){ | |
let config = { | |
headers: { | |
'Authorization': 'Bearer ' + accessToken, | |
'User-Agent': 'node.js KAC client', | |
'Content-Type': 'application/atom+xml;charset=utf-8', | |
'Accept': 'application/atom+xml' | |
} | |
}; | |
let url = 'https://' + serviceUrl + '/' + id; | |
return new Promise(function (resolve, reject) { | |
axios.put(url, copy, config) | |
.then(response => { | |
resolve(response.data); | |
}) | |
.catch (error => { | |
reject(error); | |
}); | |
}); | |
} | |
wskey.getAccessTokenWithClientCredentials('128807', '128807', user) | |
.then(function (accessToken) { | |
findCopy('266349549', accessToken.getAccessTokenString()) | |
.then(function (data){ | |
// change the copy info | |
let copy = new dom().parseFromString(data); | |
//copy.getElementsByTagNameNS('http://worldcat.org/xmlschemas/Copy-1.0', 'shelvingLocation')[0].childNodes[0].data = 'MAIN-STACKS'; | |
// Add temporary location to the holding with the pieceDesignation | |
let select = xpath.useNamespaces({"atom": "http://www.w3.org/2005/Atom", "copy": "http://worldcat.org/xmlschemas/Copy-1.0"}); | |
let holding_for_temporaryLocation = select('//copy:holding[copy:pieceDesignation = "testBarcode"]', copy); | |
let tempLocationNode = copy.createElementNS('http://worldcat.org/xmlschemas/Copy-1.0','temporaryLocation'); | |
tempLocationNode.appendChild(copy.createTextNode('MAIN-Reference')); | |
holding_for_temporaryLocation[0].appendChild(tempLocationNode); | |
//remove temporaryLocation | |
// let select = xpath.useNamespaces({"atom": "http://www.w3.org/2005/Atom", "copy": "http://worldcat.org/xmlschemas/Copy-1.0"}); | |
// let holding_for_temporaryLocation = select('//copy:holding[copy:pieceDesignation = "testBarcode"]', copy); | |
// holding_for_temporaryLocation[0].removeChild(copy.getElementsByTagNameNS('http://worldcat.org/xmlschemas/Copy-1.0', 'temporaryLocation')[0]); | |
let updatedCopy = new serializer().serializeToString(copy); | |
updateCopy(updatedCopy, '266349549', accessToken.getAccessTokenString()) | |
.then(function (updateResponse){ | |
console.log('Yes it worked') | |
}) | |
.catch(function (err){ | |
let error = err && err.response && err.response.body ? err.response.body : err; | |
console.log(error); | |
}) | |
}) | |
.catch(function (err) { | |
let error = err && err.response && err.response.body ? err.response.body : err; | |
console.log(error); | |
}) | |
}) | |
.catch(function (err) { | |
let error = err && err.response && err.response.body ? err.response.body : err; | |
console.log('no token'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment