Skip to content

Commit

Permalink
Update NYPL packages
Browse files Browse the repository at this point in the history
  • Loading branch information
nonword committed Mar 12, 2024
1 parent dac48f1 commit 757189b
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 65 deletions.
47 changes: 13 additions & 34 deletions lib/scsb-client.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
const ScsbRestClient = require('@nypl/scsb-rest-client')
const request = require('request')
const scsbRestClient = require('@nypl/scsb-rest-client')
const NyplSourceMapper = require('discovery-store-models/lib/nypl-source-mapper')
const logger = require('./logger')
const { bNumberWithCheckDigit } = require('./util')

let _scsbClient
let _initialized = false

const scsbClient = () => {
if (!_scsbClient) _scsbClient = new ScsbRestClient({ url: process.env.SCSB_URL, apiKey: process.env.SCSB_API_KEY })
return _scsbClient
if (!_initialized) {
scsbRestClient.config({
url: process.env.SCSB_URL,
apiKey: process.env.SCSB_API_KEY
})

_initialized = true
}

return scsbRestClient
}

const clientWrapper = {}
Expand Down Expand Up @@ -102,33 +110,4 @@ clientWrapper.getBarcodesByStatusForBnum = (bnum) => {
})
}

// Shim scsbQuery as general-purpose function for performing arbitrary SCSB
// queries (this function is available in @nypl/scsb-rest-client#2.0, which
// requires Node12+, preventing this app from using it for now.)
ScsbRestClient.prototype.scsbQuery = function (path, body) {
return new Promise((resolve, reject) => {
const options = {
url: this.url + path,
headers: this._headers(),
body: JSON.stringify(body),
// Fail any SCSB query that exceeds 3s (at writing this function is only
// used by getItemsAvailabilityForBnum)
timeout: 3000
}
request.post(options, (error, response, body) => {
if (error) {
reject(error)
} else if (response && response.statusCode === 200) {
resolve(JSON.parse(body))
} else {
reject(new Error(`Error hitting SCSB API ${response.statusCode}: ${response.body}`))
}
})
})
}

clientWrapper._private = {
ScsbRestClient
}

module.exports = clientWrapper
56 changes: 38 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"author": "NYPL Digital",
"dependencies": {
"@nypl/nypl-core-objects": "2.3.2",
"@nypl/nypl-data-api-client": "^1.0.1",
"@nypl/scsb-rest-client": "1.0.6",
"@nypl/nypl-data-api-client": "^1.0.5",
"@nypl/scsb-rest-client": "2.0.0",
"config": "1.12.0",
"discovery-store-models": "git+https://github.com/NYPL-discovery/discovery-store-models.git#v1.3.2",
"dotenv": "^4.0.0",
Expand Down
4 changes: 2 additions & 2 deletions test/availability_resolver.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ const itemAvailabilityResponse = [

describe('Response with updated availability', function () {
beforeEach(() => {
sinon.stub(scsbClient._private.ScsbRestClient.prototype, 'getItemsAvailabilityForBarcodes')
sinon.stub(scsbClient, 'getItemsAvailabilityForBarcodes')
.callsFake(() => Promise.resolve(itemAvailabilityResponse))

sinon.stub(scsbClient, 'recapCustomerCodeByBarcode')
.callsFake(() => Promise.resolve('NC'))
})

afterEach(() => {
scsbClient._private.ScsbRestClient.prototype.getItemsAvailabilityForBarcodes.restore()
scsbClient.getItemsAvailabilityForBarcodes.restore()
scsbClient.recapCustomerCodeByBarcode.restore()
})

Expand Down
18 changes: 9 additions & 9 deletions test/scsb-client.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const sinon = require('sinon')
const ScsbRestClient = require('@nypl/scsb-rest-client')
const scsbRestClient = require('@nypl/scsb-rest-client')
const scsbClient = require('../lib/scsb-client')

describe('scsb-client', () => {
describe('getItemsAvailabilityForBnum', () => {
beforeEach(() => {
sinon.stub(ScsbRestClient.prototype, 'scsbQuery')
sinon.stub(scsbRestClient, 'scsbQuery')
.callsFake((path, body) => {
return Promise.resolve([
{
Expand All @@ -17,18 +17,18 @@ describe('scsb-client', () => {
})

afterEach(() => {
ScsbRestClient.prototype.scsbQuery.restore()
scsbRestClient.scsbQuery.restore()
})

it('returns barcode-status map for NYPL bnum', () => {
return scsbClient.getItemsAvailabilityForBnum('b123')
.then((resp) => {
// Verify SCSB API hit once:
expect(ScsbRestClient.prototype.scsbQuery.callCount).to.equal(1)
expect(scsbRestClient.scsbQuery.callCount).to.equal(1)
// Verify SCSB bibAvailabilityStatus query performed
expect(ScsbRestClient.prototype.scsbQuery.firstCall.args[0])
expect(scsbRestClient.scsbQuery.firstCall.args[0])
.to.equal('/sharedCollection/bibAvailabilityStatus')
expect(ScsbRestClient.prototype.scsbQuery.firstCall.args[1])
expect(scsbRestClient.scsbQuery.firstCall.args[1])
.to.deep.equal({
institutionId: 'NYPL',
// Verify payload has NYPL padded bnum:
Expand All @@ -49,11 +49,11 @@ describe('scsb-client', () => {
return scsbClient.getItemsAvailabilityForBnum('hb123')
.then((resp) => {
// Verify SCSB API hit once:
expect(ScsbRestClient.prototype.scsbQuery.callCount).to.equal(1)
expect(scsbRestClient.scsbQuery.callCount).to.equal(1)
// Verify SCSB bibAvailabilityStatus query performed
expect(ScsbRestClient.prototype.scsbQuery.firstCall.args[0])
expect(scsbRestClient.scsbQuery.firstCall.args[0])
.to.equal('/sharedCollection/bibAvailabilityStatus')
expect(ScsbRestClient.prototype.scsbQuery.firstCall.args[1])
expect(scsbRestClient.scsbQuery.firstCall.args[1])
.to.deep.equal({
institutionId: 'HL',
// Verify payload has original HL bnum:
Expand Down

0 comments on commit 757189b

Please sign in to comment.