Skip to content

Commit

Permalink
Skohub: Simplify and adjust scheme loading (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefandesu committed Jun 22, 2022
1 parent d725ea1 commit 1402765
Showing 1 changed file with 23 additions and 34 deletions.
57 changes: 23 additions & 34 deletions src/providers/skohub-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,62 +44,62 @@ export default class SkohubProvider extends BaseProvider {
}

_setup() {
this._jskos.schemes = this.schemes || []
this._index = {}
this._conceptCache = {}
this._schemeCache = {}
}

async _loadScheme(scheme, config) {
const { uri, topConcepts } = scheme
async _loadScheme({ scheme, ...config }) {
const uris = jskos.getAllUris(scheme)
for (let uri of uris) {
if (this._schemeCache[uri]) {
return this._schemeCache[uri]
}
}
// Find main URI from this.schemes
const { uri } = this.schemes.find(s => jskos.compare(s, scheme)) || {}

if (!uri || topConcepts) {
return scheme
if (!uri) {
throw new errors.InvalidRequestError({ message: `Tried to load unsupported scheme (${scheme && scheme.uri})` })
}

let postfix = ".json"
if (uri.endsWith("/")) {
postfix = "index.json"
}
// Errors for this request will trickle upwards of the call chain
const data = await this.axios({ ...config, url: `${uri}${postfix}`, _skipAdditionalParameters: true })

// TODO: if not found

if (data.id !== uri) {
throw new errors.InvalidRequestError({ message: "Skohub URL did not return expected concept scheme" })
}

const { title, preferredNamespaceUri, hasTopConcept, description } = data //, issued, created, modified, creator, publisher } = data
const { title, preferredNamespaceUri, hasTopConcept, description } = data

scheme.prefLabel = title
Object.keys(scheme.prefLabel || {}).forEach(key => {
scheme.prefLabel[key] = decodeUnicode(scheme.prefLabel[key])
})
scheme.namespace = preferredNamespaceUri
scheme.topConcepts = (hasTopConcept || []).map(c => this._toJskosConcept(c))

// const hasNarrower = scheme.topConcepts.find(c => c.narrower && c.narrower.length)

scheme.concepts = [null]
// scheme.concepts = [...scheme.topConcepts]
// if (hasNarrower) {
// scheme.concepts.push(null)
// }

// TODO: map remaining fields

if (description) {
scheme.definition = description
// scopeNote values in JSKOS are arrays
Object.keys(scheme.definition).forEach(key => {
scheme.definition[key] = [decodeUnicode(scheme.definition[key])]
})
}

// remove fields without value
// Remove fields without value
for (let key of Object.keys(scheme).filter(key => !scheme[key])) {
delete scheme[key]
}

// Add to cache
for (let uri of uris) {
this._schemeCache[uri] = scheme
}

return scheme
}

Expand Down Expand Up @@ -150,27 +150,16 @@ export default class SkohubProvider extends BaseProvider {
}

async getSchemes({ ...config }) {
const { schemes } = this._jskos

for (let i=0; i<schemes.length; i++) {
schemes[i] = await this._loadScheme(schemes[i], config)
}

return schemes
return Promise.all(this.schemes.map(scheme => this._loadScheme({ ...config, scheme })))
}

async getTop({ scheme, ...config }) {
if (!scheme || !scheme.uri) {
throw new errors.InvalidOrMissingParameterError({ parameter: "scheme", message: "Missing scheme URI" })
}

scheme = this._jskos.schemes.find(s => s.uri === scheme.uri)
if (scheme) {
scheme = await this._loadScheme(scheme, config)
return scheme.topConcepts
} else {
return []
}
scheme = await this._loadScheme({ scheme, ...config })
return scheme.topConcepts || []
}

async getConcepts({ concepts, ...config }) {
Expand Down

0 comments on commit 1402765

Please sign in to comment.