Skip to content

Commit

Permalink
fix modify script headers bug, ref #34
Browse files Browse the repository at this point in the history
also limit it to 5 concurrent connections to VAULT, this did not turn out to
be the source of the bug but it would probably eventually be a problem
  • Loading branch information
phette23 committed Aug 8, 2023
1 parent c2b4631 commit 4249bc0
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions utilities/metadata-csv/modify.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// usage:
// node modify --csv input.csv [--debug] [--dryrun]
import fs from 'node:fs'
import https from 'node:https'
import { pathToFileURL } from 'node:url'

import { default as fetch, Headers } from 'node-fetch'
import { default as fetch } from 'node-fetch'
import rc from 'rc'
import { DOMParser, XMLSerializer } from '@xmldom/xmldom'
import xpath from 'xpath'
Expand All @@ -24,9 +25,12 @@ if (options.h || options.help) {

if (options.dryrun) console.log('Dry run: no records will be modified.')

const headers = new Headers({
const headers = {
'X-Authorization': 'access_token=' + options.token,
'Accept': 'application/json',
}
const httpsAgent = new https.Agent({
maxSockets: 5
})

// log messages only when debug=true
Expand Down Expand Up @@ -57,19 +61,22 @@ function makeChangesHash(columns, row) {
// PUTs new item metadata to EQUELLA
function applyChanges(item, xml) {
const url = `${options.root}/item/${item.uuid}/${item.version}/`
let putHeaders = headers
putHeaders.append('Content-Type', 'application/json')
// ph = headers; ph.append('content-type'...) does not work, ends up with
// duplicates in putHeader & EQUELLA throws an error
let putHeaders = { 'Content-Type': 'application/json' }
putHeaders = Object.assign(headers, putHeaders)
item.metadata = XMLStringify(xml)

fetch(url, {
agent: httpsAgent,
method: 'PUT',
headers: putHeaders,
body: JSON.stringify(item), })
// EQUELLA responds with an empty body on success
.then(r => {
if (r.status == 200) return console.log(`Successfully edited item ${url}`)
if (r.ok) return console.log(`Successfully edited item ${url}`)
// if we're unsuccessful we might have error JSON
r.json()
return r.json()
})
.then(data => {
if (data && data.error) throw(data)
Expand Down

0 comments on commit 4249bc0

Please sign in to comment.