Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/development', v0.3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
m-mohr committed Feb 13, 2019
2 parents d229aa4 + 112b77b commit d547a32
Show file tree
Hide file tree
Showing 6 changed files with 794 additions and 33 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ JavaScript client for the openEO API.

[![Build Status](https://travis-ci.org/Open-EO/openeo-js-client.svg?branch=master)](https://travis-ci.org/Open-EO/openeo-js-client)

This client is in **version 0.3.1** and supports **openEO API versions 0.3.0 and 0.3.1**. Legacy versions are available as releases.
This client is in **version 0.3.2** and supports **openEO API versions 0.3.0 and 0.3.1**. Legacy versions are available as releases.

## Usage
This library can run in a recent browser supporting ECMAScript 2015 or node.js.

To use it in a browser environment simply add the following code to your HTML file:
```
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://unpkg.com/@openeo/js-client/openeo.js"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@openeo/js-client/openeo.js"></script>
```

To install it with npm: `npm install @openeo/js-client`
Expand Down
2 changes: 1 addition & 1 deletion examples/discovery.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<title>openEO JS client - Discovery example</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script src="../openeo.js"></script>
<script type="text/javascript">
var url = "https://earthengine.openeo.org/v0.3"; // Insert the openEO server URL here
Expand Down
109 changes: 85 additions & 24 deletions openeo.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class OpenEO {
}

version() {
return "0.3.0";
return "0.3.2";
}
}

Expand Down Expand Up @@ -94,6 +94,21 @@ class Connection {
.then(response => response.data);
}

buildProcessGraph() {
return this.listProcesses()
.then(data => {
var builder = {};
for(let i in data.processes) {
let process = data.processes[i];
builder[process.name] = (options) => {
options.process_id = process.name;
return options;
}
}
return builder;
});
}

authenticateOIDC(options = null) {
return Promise.reject(new Error("Not implemented yet."));
}
Expand Down Expand Up @@ -204,7 +219,7 @@ class Connection {
}

createJob(processGraph, outputFormat = null, outputParameters = {}, title = null, description = null, plan = null, budget = null, additional = {}) {
var jobObject = Object.assign(additional, {
var jobObject = Object.assign({}, additional, {
title: title,
description: description,
process_graph: processGraph,
Expand Down Expand Up @@ -328,7 +343,48 @@ class Connection {
break;
}

return axios(options);
return axios(options).catch(error => {
return new Promise((resolve, reject) => {
if (error.response !== null && typeof error.response === 'object' && error.response.data !== null && typeof error.response.data === 'object' && typeof error.response.data.type === 'string' && error.response.data.type.indexOf('/json') !== -1) {
// JSON error responses are Blobs and streams if responseType is set as such, so convert to JSON if required.
// See: https://github.com/axios/axios/issues/815
try {
switch(options.responseType) {
case 'blob':
const fileReader = new FileReader();
fileReader.onerror = () => {
fileReader.abort();
reject(error);
};
fileReader.onload = () => {
reject(JSON.parse(fileReader.result));
};
fileReader.readAsText(error.response.data);
break;
case 'stream':
const chunks = "";
error.response.data.on("data", chunk => {
chunks.push(chunk);
});
readStream.on("error", () => {
reject(error);
});
readStream.on("end", () => {
reject(JSON.parse(Buffer.concat(chunks).toString()));
});
break;
default:
reject(error);
}
} catch (exception) {
reject(error);
}
}
else {
reject(error);
}
});
});
}

_resetAuth() {
Expand Down Expand Up @@ -498,7 +554,7 @@ class Subscriptions {

_flushQueue() {
if(this.socket.readyState === this.socket.OPEN) {
for(var i in this.messageQueue) {
for(let i in this.messageQueue) {
this.socket.send(JSON.stringify(this.messageQueue[i]));
}

Expand Down Expand Up @@ -538,7 +594,7 @@ class Subscriptions {
parameters = {};
}

var payloadParameters = Object.assign(parameters, { topic: topic });
var payloadParameters = Object.assign({}, parameters, { topic: topic });

this._sendMessage(action, {
topics: [payloadParameters]
Expand All @@ -553,15 +609,19 @@ class Capabilities {
if(!data || !data.version || !data.endpoints) {
throw new Error("Data is not a valid Capabilities response");
}
this._data = data;
this.data = data;
}

toPlainObject() {
return this.data;
}

version() {
return this._data.version;
return this.data.version;
}

listFeatures() {
return this._data.endpoints;
return this.data.endpoints;
}

hasFeature(methodName) {
Expand Down Expand Up @@ -606,14 +666,14 @@ class Capabilities {
};

// regex-ify to allow custom parameter names
for (var key in clientMethodNameToAPIRequestMap) {
for(let key in clientMethodNameToAPIRequestMap) {
clientMethodNameToAPIRequestMap[key] = clientMethodNameToAPIRequestMap[key].replace(/{[^}]+}/, '{[^}]+}');
}

if (methodName === 'createFile') {
return this.hasFeature('uploadFile'); // createFile is always available, map it to uploadFile as it is more meaningful.
} else {
return this._data.endpoints
return this.data.endpoints
.map((e) => e.methods.map((method) => method + ' ' + e.path))
// .flat(1) // does exactly what we want, but (as of Sept. 2018) not yet part of the standard...
.reduce((a, b) => a.concat(b), []) // ES6-proof version of flat(1)
Expand All @@ -622,11 +682,11 @@ class Capabilities {
}

currency() {
return (this._data.billing ? this._data.billing.currency : null);
return (this.data.billing ? this.data.billing.currency : null);
}

listPlans() {
return (this._data.billing ? this._data.billing.plans : null);
return (this.data.billing ? this.data.billing.plans : null);
}
}

Expand All @@ -637,8 +697,8 @@ class BaseEntity {
this.connection = connection;
this.clientNames = {};
this.extra = {};
for(var i in properties) {
var backend, client;
for(let i in properties) {
let backend, client;
if (Array.isArray(properties[i])) {
backend = properties[i][0];
client = properties[i][1];
Expand All @@ -655,7 +715,7 @@ class BaseEntity {
}

setAll(metadata) {
for (var name in metadata) {
for(let name in metadata) {
if (typeof this.clientNames[name] === 'undefined') {
this.extra[name] = metadata[name];
}
Expand All @@ -668,8 +728,8 @@ class BaseEntity {

getAll() {
var obj = {};
for (var backend in this.clientNames) {
var client = this.clientNames[backend];
for(let backend in this.clientNames) {
let client = this.clientNames[backend];
obj[client] = this[client];
}
return Object.assign(obj, this.extra);
Expand Down Expand Up @@ -736,7 +796,7 @@ class File extends BaseEntity {
}
};
if (typeof statusCallback === 'function') {
options.onUploadProgress = function(progressEvent) {
options.onUploadProgress = (progressEvent) => {
var percentCompleted = Math.round( (progressEvent.loaded * 100) / progressEvent.total );
statusCallback(percentCompleted);
};
Expand Down Expand Up @@ -836,11 +896,11 @@ class Job extends BaseEntity {

var promises = [];
var files = [];
for(var i in list.links) {
var link = list.links[i].href;
var parsedUrl = url.parse(link);
var targetPath = path.join(targetFolder, path.basename(parsedUrl.pathname));
var p = this.connection.download(link, false)
for(let i in list.links) {
let link = list.links[i].href;
let parsedUrl = url.parse(link);
let targetPath = path.join(targetFolder, path.basename(parsedUrl.pathname));
let p = this.connection.download(link, false)
.then(response => this.connection._saveToFileNode(response.data, targetPath))
.then(() => files.push(targetPath));
promises.push(p);
Expand Down Expand Up @@ -926,13 +986,14 @@ if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = toExport;
}
else {
/* istanbul ignore next */
if (typeof define === 'function' && define.amd) {
define([], function () {
return toExport;
});
}
else {
for (let exportObjName in toExport) {
for(let exportObjName in toExport) {
window[exportObjName] = toExport[exportObjName];
}
}
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openeo/js-client",
"version": "0.3.1",
"version": "0.3.2",
"author": "openEO Consortium",
"contributors": [
{
Expand Down Expand Up @@ -30,7 +30,8 @@
"ws": "^6.1.2"
},
"scripts": {
"test": "jest --env=jsdom",
"test_node": "jest --env=node"
"test": "jest basic.test.js --env=jsdom",
"test_node": "jest basic.test.js --env=node",
"test_gee": "jest earthengine.test.js --env=node"
}
}
5 changes: 3 additions & 2 deletions tests/basic.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const { OpenEO } = require('../openeo.js');
const packageInfo = require('../package.json');

describe('Check version number', () => {
describe('Basic client tests', () => {
var obj = new OpenEO();
test('Check that import worked', () => {
expect(obj).not.toBeNull();
expect(obj).toBeInstanceOf(OpenEO);
});
test('Check version number', () => {
expect(obj.version()).toBe("0.3.0");
expect(obj.version()).toBe(packageInfo.version);
});
});
Loading

0 comments on commit d547a32

Please sign in to comment.