Skip to content

Commit

Permalink
fix(import): L'API Search d'OCLC ne permet désormais pas plus de 10 b…
Browse files Browse the repository at this point in the history
…ooléens
  • Loading branch information
remillc committed Apr 16, 2024
1 parent bcf01bc commit e22ac8e
Showing 1 changed file with 27 additions and 27 deletions.
54 changes: 27 additions & 27 deletions app/lib/oclc-search-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,34 @@ import axios from 'axios'
import { AccessToken } from './access-token.js'
import console from './console.js'

const wskey = config.get('oclcApi.search');
const wskey = config.get('oclcApi.search')

export class OclcSearchService {
constructor({
serviceUrl = 'https://americas.discovery.api.oclc.org/worldcat/search/v2',
key,
secret,
} = {}) {
this.url = new URL(serviceUrl);
this.url = new URL(serviceUrl)

this.key = key;
this.secret = secret;
this.key = key
this.secret = secret
this.accessToken = new AccessToken({
scope: ['SCIM', 'wcapi'],
wskey: wskey.key,
wskeySecret: wskey.secret
});
})
}

async getBibRecordByOclcNumber(oclcNumber) {

return new Promise(async (resolve, reject) => {

const url = new URL(this.url) // clone this.url
url.pathname += `/bibs/${oclcNumber}`;
url.pathname += `/bibs/${oclcNumber}`

try {
const bearerToken = (await this.accessToken.requestAccessToken()).accessToken;
const bearerToken = (await this.accessToken.requestAccessToken()).accessToken

let result = await axios(
{
Expand All @@ -40,37 +40,37 @@ export class OclcSearchService {
Authorization: `Bearer ${bearerToken}`
}
}
);
)

resolve(result);
resolve(result)

} catch (e) {
console.error(e)
reject(e)
}

});
})
}

async getBibRecordsByOclcNumber(oclcNumbers) {

return new Promise(async (resolve, reject) => {

const limit = 50;
const result = [];
const limit = 10
const result = []

for (let i = 0; i < oclcNumbers.length; i += limit) {
const url = new URL(this.url.href) // clone this.url
const oclcNumbersSlice = oclcNumbers.slice(i, i + limit);
const oclcNumbersSlice = oclcNumbers.slice(i, i + limit)

let q = oclcNumbersSlice.map(oclcNumber => `no:(${oclcNumber})`)
q = q.join(' OR ');
url.pathname += `/bibs/`;
url.searchParams.set('q', q);
q = q.join(' OR ')
url.pathname += `/bibs/`
url.searchParams.set('q', q)
url.searchParams.set('limit', limit)

try {
const bearerToken = (await this.accessToken.requestAccessToken()).accessToken;
const bearerToken = (await this.accessToken.requestAccessToken()).accessToken

console.info(`Requesting oclc numbers ${i} to ${i + limit}...`)

Expand All @@ -82,14 +82,14 @@ export class OclcSearchService {
},
proxy: false
}
);
)

if (response.status === 200) {

const partialResult = response.data

if (partialResult.numberOfRecords > 0) {
result.push(...partialResult.bibRecords);
result.push(...partialResult.bibRecords)
}
} else {
console.error(`Failed to retrieve oclc numbers ${i} to ${i + limit}.`)
Expand All @@ -101,19 +101,19 @@ export class OclcSearchService {

}

resolve(result);
resolve(result)

});
})
}

async getHoldingByOclcNumber(oclcNumber) {

const url = new URL(this.url.href) // clone this.url
url.pathname += `/my-holdings/`;
url.pathname += `/my-holdings/`
url.searchParams.set('oclcNumber', oclcNumber)

try {
const bearerToken = (await this.accessToken.requestAccessToken()).accessToken;
const bearerToken = (await this.accessToken.requestAccessToken()).accessToken

const result = await axios(
{
Expand All @@ -123,15 +123,15 @@ export class OclcSearchService {
},
proxy: false
}
);
)

// console.debug(result)

return result

} catch (e) {
console.error(e)
return e;
return e
}

// });
Expand All @@ -148,10 +148,10 @@ import isMain from 'is-main'
if (isMain(import.meta)) {
(async () => {

const searchService = new OclcSearchService(wskey);
const searchService = new OclcSearchService(wskey)

try {
const result = await searchService.getBibRecordByOclcNumber('1224186661');
const result = await searchService.getBibRecordByOclcNumber('1224186661')
console.log(JSON.stringify(result, null, 2))
} catch (e) {
try {
Expand Down

0 comments on commit e22ac8e

Please sign in to comment.