Skip to content

Commit 1402765

Browse files
committed
Skohub: Simplify and adjust scheme loading (#29)
1 parent d725ea1 commit 1402765

File tree

1 file changed

+23
-34
lines changed

1 file changed

+23
-34
lines changed

src/providers/skohub-provider.js

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -44,62 +44,62 @@ export default class SkohubProvider extends BaseProvider {
4444
}
4545

4646
_setup() {
47-
this._jskos.schemes = this.schemes || []
4847
this._index = {}
4948
this._conceptCache = {}
49+
this._schemeCache = {}
5050
}
5151

52-
async _loadScheme(scheme, config) {
53-
const { uri, topConcepts } = scheme
52+
async _loadScheme({ scheme, ...config }) {
53+
const uris = jskos.getAllUris(scheme)
54+
for (let uri of uris) {
55+
if (this._schemeCache[uri]) {
56+
return this._schemeCache[uri]
57+
}
58+
}
59+
// Find main URI from this.schemes
60+
const { uri } = this.schemes.find(s => jskos.compare(s, scheme)) || {}
5461

55-
if (!uri || topConcepts) {
56-
return scheme
62+
if (!uri) {
63+
throw new errors.InvalidRequestError({ message: `Tried to load unsupported scheme (${scheme && scheme.uri})` })
5764
}
5865

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

65-
// TODO: if not found
66-
6773
if (data.id !== uri) {
6874
throw new errors.InvalidRequestError({ message: "Skohub URL did not return expected concept scheme" })
6975
}
7076

71-
const { title, preferredNamespaceUri, hasTopConcept, description } = data //, issued, created, modified, creator, publisher } = data
77+
const { title, preferredNamespaceUri, hasTopConcept, description } = data
7278

7379
scheme.prefLabel = title
7480
Object.keys(scheme.prefLabel || {}).forEach(key => {
7581
scheme.prefLabel[key] = decodeUnicode(scheme.prefLabel[key])
7682
})
7783
scheme.namespace = preferredNamespaceUri
7884
scheme.topConcepts = (hasTopConcept || []).map(c => this._toJskosConcept(c))
79-
80-
// const hasNarrower = scheme.topConcepts.find(c => c.narrower && c.narrower.length)
81-
8285
scheme.concepts = [null]
83-
// scheme.concepts = [...scheme.topConcepts]
84-
// if (hasNarrower) {
85-
// scheme.concepts.push(null)
86-
// }
87-
88-
// TODO: map remaining fields
89-
9086
if (description) {
9187
scheme.definition = description
9288
// scopeNote values in JSKOS are arrays
9389
Object.keys(scheme.definition).forEach(key => {
9490
scheme.definition[key] = [decodeUnicode(scheme.definition[key])]
9591
})
9692
}
97-
98-
// remove fields without value
93+
// Remove fields without value
9994
for (let key of Object.keys(scheme).filter(key => !scheme[key])) {
10095
delete scheme[key]
10196
}
10297

98+
// Add to cache
99+
for (let uri of uris) {
100+
this._schemeCache[uri] = scheme
101+
}
102+
103103
return scheme
104104
}
105105

@@ -150,27 +150,16 @@ export default class SkohubProvider extends BaseProvider {
150150
}
151151

152152
async getSchemes({ ...config }) {
153-
const { schemes } = this._jskos
154-
155-
for (let i=0; i<schemes.length; i++) {
156-
schemes[i] = await this._loadScheme(schemes[i], config)
157-
}
158-
159-
return schemes
153+
return Promise.all(this.schemes.map(scheme => this._loadScheme({ ...config, scheme })))
160154
}
161155

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

167-
scheme = this._jskos.schemes.find(s => s.uri === scheme.uri)
168-
if (scheme) {
169-
scheme = await this._loadScheme(scheme, config)
170-
return scheme.topConcepts
171-
} else {
172-
return []
173-
}
161+
scheme = await this._loadScheme({ scheme, ...config })
162+
return scheme.topConcepts || []
174163
}
175164

176165
async getConcepts({ concepts, ...config }) {

0 commit comments

Comments
 (0)