Skip to content

Commit

Permalink
Adjust how provider capabiltities are set by adding static variable "…
Browse files Browse the repository at this point in the history
…supports"

Makes it a bit simpler and also allows checking a provider's capabilities before initializing a registry.
  • Loading branch information
stefandesu committed Nov 24, 2023
1 parent 772ad9b commit 5256f35
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 151 deletions.
13 changes: 13 additions & 0 deletions src/providers/base-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import * as errors from "../errors/index.js"
* BaseProvider to be subclassed to implement specific providers. Do not initialize a registry directly with this!
*
* Prefix all internal method and properties with underscore (e.g. `this._cache`)!
*
* Static members that should be set:
* - providerName (This is how a provider is identified in a "registry" object in field `provider`.)
* - providerType (Optional BARTOC API type URI. Supported types: https://github.com/gbv/bartoc.org/blob/main/data/bartoc-api-types.concepts.csv, the URI prefix is "http://bartoc.org/api-type/".)
* - supports (Optional object of supported capabilities. The keys should be values from this list: https://github.com/gbv/cocoda-sdk/blob/9145952398d6828004beb395c1d392a4d24e9288/src/utils/index.js#L159-L174; values should be a boolean. `false` values can be left out. They will be used to initialize `this.has` (see below). Alternatively, `this.has` can be filled in `_prepare` or `_setup`.)
*
* Methods that can be overridden:
* - Do not override the constructor! Use _prepare or _setup instead.
Expand Down Expand Up @@ -91,6 +96,14 @@ export default class BaseProvider {
* @readonly
*/
this.has = {}
// Use values from static "supports" value
if (this.constructor?.supports) {
this.has = Object.assign({}, this.constructor?.supports)
}
// Explicitly set other capabilities to false
utils.listOfCapabilities.filter(c => !this.has[c]).forEach(c => {
this.has[c] = false
})
// Set default language priority list
this._defaultLanguages = "en,de,fr,es,nl,it,fi,pl,ru,cs,jp".split(",")
/**
Expand Down
27 changes: 12 additions & 15 deletions src/providers/concept-api-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ import jskos from "jskos-tools"
* @category Providers
*/
export default class ConceptApiProvider extends BaseProvider {
static supports = {
schemes: true,
top: true,
data: true,
concepts: true,
narrower: true,
ancestors: true,
types: true,
suggest: true,
search: true,
auth: true,
}

/**
* @private
Expand All @@ -39,21 +51,6 @@ export default class ConceptApiProvider extends BaseProvider {
if (this._api.api && this._api.status === undefined) {
this._api.status = utils.concatUrl(this._api.api, "/status")
}
// Set capabilities to true for now; will be overridden by _setup() later
this.has.schemes = true
this.has.top = true
this.has.data = true
this.has.concepts = true
this.has.narrower = true
this.has.ancestors = true
this.has.types = true
this.has.suggest = true
this.has.search = true
this.has.auth = true
// Explicitly set other capabilities to false
utils.listOfCapabilities.filter(c => !this.has[c]).forEach(c => {
this.has[c] = false
})
}

/**
Expand Down
9 changes: 3 additions & 6 deletions src/providers/label-search-suggestion-provider.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import BaseProvider from "./base-provider.js"
import jskos from "jskos-tools"
import * as _ from "../utils/lodash.js"
import { listOfCapabilities } from "../utils/index.js"
import * as errors from "../errors/index.js"

// TODO: Only keep the last 20 results in cache.
Expand Down Expand Up @@ -32,17 +31,15 @@ import * as errors from "../errors/index.js"
* @category Providers
*/
export default class LabelSearchSuggestionProvider extends BaseProvider {
static supports = {
mappings: true,
}

/**
* @private
*/
_prepare() {
this._cache = []
this.has.mappings = true
// Explicitly set other capabilities to false
listOfCapabilities.filter(c => !this.has[c]).forEach(c => {
this.has[c] = false
})
}

/**
Expand Down
22 changes: 8 additions & 14 deletions src/providers/lobid-api-provider.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import BaseProvider from "./base-provider.js"
import * as errors from "../errors/index.js"
import * as utils from "../utils/index.js"
import jskos from "jskos-tools"
import axios from "axios"

Expand Down Expand Up @@ -145,19 +144,14 @@ function fixURI(uri) {
}

export default class LobidApiProvider extends BaseProvider {

_prepare() {
this.has.schemes = true
this.has.data = true
this.has.concepts = true
this.has.narrower = true
this.has.suggest = true
this.has.search = true
this.has.types = true
// Explicitly set other capabilities to false
utils.listOfCapabilities.filter(c => !this.has[c]).forEach(c => {
this.has[c] = false
})
static supports = {
schemes: true,
data: true,
concepts: true,
narrower: true,
suggest: true,
search: true,
types: true,
}

_setup() {
Expand Down
28 changes: 10 additions & 18 deletions src/providers/loc-api-provider.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import BaseProvider from "./base-provider.js"
import * as errors from "../errors/index.js"
import { listOfCapabilities } from "../utils/index.js"
import jskos from "jskos-tools"
import axios from "axios"

Expand Down Expand Up @@ -116,23 +115,16 @@ function madsToJskosConcept(data, { scheme }) {
* @category Providers
*/
export default class LocApiProvider extends BaseProvider {

/**
* @private
*/
_prepare() {
this.has.schemes = true
this.has.top = false
this.has.data = true
this.has.concepts = true
this.has.narrower = false
this.has.ancestors = false
this.has.suggest = true
this.has.search = true
// Explicitly set other capabilities to false
listOfCapabilities.filter(c => !this.has[c]).forEach(c => {
this.has[c] = false
})
// TODO: Can unsupported types be supported later? If not, just remove them here.
static supports = {
schemes: true,
top: false,
data: true,
concepts: true,
narrower: false,
ancestors: false,
suggest: true,
search: true,
}

/**
Expand Down
25 changes: 9 additions & 16 deletions src/providers/local-mappings-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import * as _ from "../utils/lodash.js"
import localforage from "localforage"
import { v4 as uuid } from "uuid"
import * as errors from "../errors/index.js"
import { listOfCapabilities } from "../utils/index.js"
const uriPrefix = "urn:uuid:"

/**
Expand All @@ -26,21 +25,15 @@ const uriPrefix = "urn:uuid:"
* @category Providers
*/
export default class LocalMappingsProvider extends BaseProvider {

/**
* @private
*/
_prepare() {
this.has.mappings = {
read: true,
create: true,
update: true,
delete: true,
}
// Explicitly set other capabilities to false
listOfCapabilities.filter(c => !this.has[c]).forEach(c => {
this.has[c] = false
})
static get supports() {
return {
mappings: {
read: true,
create: true,
update: true,
delete: true,
},
}
}

/**
Expand Down
13 changes: 5 additions & 8 deletions src/providers/mappings-api-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ import * as utils from "../utils/index.js"
* @category Providers
*/
export default class MappingsApiProvider extends BaseProvider {
static supports = {
mappings: true,
concordances: true,
annotations: true,
}

/**
* @private
Expand All @@ -40,14 +45,6 @@ export default class MappingsApiProvider extends BaseProvider {
if (this._api.api && this._api.status === undefined) {
this._api.status = utils.concatUrl(this._api.api, "/status")
}
// Preliminarily set capabilties
this.has.mappings = true
this.has.concordances = true
this.has.annotations = true
// Explicitly set other capabilities to false
utils.listOfCapabilities.filter(c => !this.has[c]).forEach(c => {
this.has[c] = false
})
}

/**
Expand Down
24 changes: 9 additions & 15 deletions src/providers/mycore-provider.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import BaseProvider from "./base-provider.js"
import * as _ from "../utils/lodash.js"
import * as errors from "../errors/index.js"
import { listOfCapabilities } from "../utils/index.js"
import jskos from "jskos-tools"
import FlexSearch from "flexsearch"

Expand All @@ -26,20 +25,15 @@ const data = {}
*
*/
export default class MyCoReProvider extends BaseProvider {

_prepare() {
this.has.schemes = true
this.has.top = true
this.has.data = true
this.has.concepts = true
this.has.narrower = true
this.has.ancestors = true
this.has.suggest = true
this.has.search = true
// Explicitly set other capabilities to false
listOfCapabilities.filter(c => !this.has[c]).forEach(c => {
this.has[c] = false
})
static supports = {
schemes: true,
top: true,
data: true,
concepts: true,
narrower: true,
ancestors: true,
suggest: true,
search: true,
}

_setup() {
Expand Down
25 changes: 10 additions & 15 deletions src/providers/not-api-provider.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import BaseProvider from "./base-provider.js"
import * as errors from "../errors/index.js"
import { listOfCapabilities } from "../utils/index.js"
import axios from "axios"
import jskos from "jskos-tools"

Expand Down Expand Up @@ -33,20 +32,16 @@ const cache = {
}

export default class NoTApiProvider extends BaseProvider {

_prepare() {
this.has.schemes = true
this.has.top = false
this.has.data = true
this.has.concepts = true
this.has.narrower = false
this.has.ancestors = false
this.has.suggest = true
this.has.search = true
// Explicitly set other capabilities to false
listOfCapabilities.filter(c => !this.has[c]).forEach(c => {
this.has[c] = false
})
// TODO: Can unsupported types be supported later? If not, just remove them here.
static supports = {
schemes: true,
top: false,
data: true,
concepts: true,
narrower: false,
ancestors: false,
suggest: true,
search: true,
}

/**
Expand Down
10 changes: 4 additions & 6 deletions src/providers/occurrences-api-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ const cache = {}
* @category Providers
*/
export default class OccurrencesApiProvider extends BaseProvider {
static supports = {
occurrences: true,
mappings: true,
}

get _cache() {
return cache[this.uri]
Expand All @@ -40,12 +44,6 @@ export default class OccurrencesApiProvider extends BaseProvider {
_prepare() {
cache[this.uri] = []
this._occurrencesSupportedSchemes = []
this.has.occurrences = true
this.has.mappings = true
// Explicitly set other capabilities to false
utils.listOfCapabilities.filter(c => !this.has[c]).forEach(c => {
this.has[c] = false
})
}

/**
Expand Down
9 changes: 3 additions & 6 deletions src/providers/reconciliation-api-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import BaseProvider from "./base-provider.js"
import jskos from "jskos-tools"
import * as _ from "../utils/lodash.js"
import * as errors from "../errors/index.js"
import { listOfCapabilities } from "../utils/index.js"

// Cache by registry URI
const cache = {}
Expand Down Expand Up @@ -36,6 +35,9 @@ const cache = {}
* @category Providers
*/
export default class ReconciliationApiProvider extends BaseProvider {
static supports = {
mappings: true,
}

get _cache() {
return cache[this.uri]
Expand All @@ -46,11 +48,6 @@ export default class ReconciliationApiProvider extends BaseProvider {
*/
_prepare() {
cache[this.uri] = []
this.has.mappings = true
// Explicitly set other capabilities to false
listOfCapabilities.filter(c => !this.has[c]).forEach(c => {
this.has[c] = false
})
}

/**
Expand Down
Loading

0 comments on commit 5256f35

Please sign in to comment.