-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
202 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
const Capabilities = require("./capabilities"); | ||
const Utils = require('@openeo/js-commons/src/utils'); | ||
const StacMigrate = require('@radiantearth/stac-migrate'); | ||
|
||
class GdcCapabilities extends Capabilities { | ||
|
||
constructor(data) { | ||
super(data); | ||
this.checkConformance(); | ||
} | ||
|
||
hasConformance(uri) { | ||
if(!Array.isArray(this.data.conformsTo)) { | ||
return false; | ||
} | ||
return this.data.conformsTo.includes(uri); | ||
} | ||
|
||
checkConformance() { | ||
if (!Array.isArray(this.data.endpoints)) { | ||
this.data.endpoints = []; | ||
} | ||
if (this.hasConformance('http://www.opengis.net/spec/ogcapi-coverages-1/0.0/conf/geodata-coverage')) { | ||
this.data.endpoints.push({ | ||
"path": "/collections", | ||
"methods": [ | ||
"GET" | ||
] | ||
}); | ||
this.data.endpoints.push({ | ||
"path": "/collections/{collection_id}", | ||
"methods": [ | ||
"GET" | ||
] | ||
}); | ||
} | ||
if (this.hasConformance('http://www.opengis.net/spec/ogcapi-processes-1/1.0/conf/core')) { | ||
this.data.endpoints.push({ | ||
"path": "/processes", | ||
"methods": [ | ||
"GET" | ||
] | ||
}); | ||
} | ||
this.init(); | ||
} | ||
|
||
/** | ||
* Initializes the class. | ||
* | ||
* @protected | ||
*/ | ||
init() { | ||
if (Array.isArray(this.data.endpoints)) { | ||
super.init(); | ||
} | ||
} | ||
|
||
/** | ||
* Validates the capabilities. | ||
* | ||
* Throws an error in case of an issue, otherwise just passes. | ||
* | ||
* @protected | ||
* @throws {Error} | ||
*/ | ||
validate() { | ||
if(!Utils.isObject(this.data)) { | ||
throw new Error("No capabilities retrieved."); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the GDC API version implemented by the back-end. | ||
* | ||
* @returns {string} openEO API version number. | ||
*/ | ||
apiVersion() { | ||
return "1.0.0"; // this.data.gdc_version; | ||
} | ||
|
||
isEndpoint(response, method, endpoint) { | ||
if (response.config.method !== method) { | ||
return false; | ||
} | ||
if (endpoint.includes('{}')) { | ||
let pattern = '^' + endpoint.replace('{}', '[^/]+') + '$'; | ||
let regex = new RegExp(pattern); | ||
return regex.test(response.config.url); | ||
} | ||
return endpoint === response.config.url; | ||
} | ||
|
||
/** | ||
* Migrates a response, if required. | ||
* | ||
* @param {AxiosResponse} response | ||
* @protected | ||
* @returns {AxiosResponse} | ||
*/ | ||
migrate(response) { | ||
if (this.isEndpoint(response, 'get', '/collections')) { | ||
response.data.collections = response.data.collections.map(collection => Migrate.collection(collection, response)); | ||
} | ||
if (this.isEndpoint(response, 'get', '/collections/{}')) { | ||
response.data = Migrate.collection(response.data, response); | ||
} | ||
if (this.isEndpoint(response, 'get', '/processes')) { | ||
response.data.processes = response.data.processes.map(process => Migrate.process(process, response)); | ||
} | ||
|
||
response = Migrate.all(response); | ||
|
||
return response; | ||
} | ||
} | ||
|
||
const Migrate = { | ||
|
||
connection: null, | ||
|
||
all(response) { | ||
if (Array.isArray(response.data.links)) { | ||
response.data.links = this.connection.makeLinksAbsolute(response.data.links, response); | ||
} | ||
return response; | ||
}, | ||
|
||
collection(collection, response) { | ||
if (collection.stac_version) { | ||
return collection; | ||
} | ||
|
||
// Make sure the required properties are present | ||
collection = StacMigrate.collection(collection); | ||
// Make links absolute | ||
if (Array.isArray(collection.links)) { | ||
collection.links = this.connection.makeLinksAbsolute(collection.links, response); | ||
} | ||
|
||
return collection; | ||
}, | ||
|
||
process(process, response) { | ||
if (process.parameters || process.returns) { | ||
return process; | ||
} | ||
|
||
process.summary = process.title; | ||
|
||
process.parameters = []; | ||
for(let name in process.inputs) { | ||
let input = process.inputs[name]; | ||
process.parameters.push({ | ||
name, | ||
description: [input.title, input.description].filter(v => Boolean(v)).join("\n\n"), | ||
schema: input.schema, | ||
optional: typeof input.schema.default !== 'undefined' | ||
}); | ||
} | ||
|
||
process.returns = { | ||
description: 'see process description', | ||
schema: [] | ||
}; | ||
if (Utils.size(process.outputs) === 1) { | ||
let output = Object.values(process.outputs)[0]; | ||
process.returns = { | ||
description: [output.title, output.description].filter(v => Boolean(v)).join("\n\n"), | ||
schema: output.schema | ||
}; | ||
} | ||
else { | ||
process.returns = { | ||
description: 'see process description', | ||
schema: [] | ||
}; | ||
for(let name in process.outputs) { | ||
let output = process.outputs[name]; | ||
let schema = Object.assign({}, output.schema, {title: output.title, description: output.description}); | ||
process.returns.schema.push(schema); | ||
} | ||
} | ||
|
||
// Make links absolute | ||
if (Array.isArray(process.links)) { | ||
process.links = this.connection.makeLinksAbsolute(process.links, response); | ||
} | ||
|
||
return process; | ||
} | ||
|
||
}; | ||
|
||
module.exports = { | ||
GdcCapabilities, | ||
Migrate | ||
}; |