Skip to content

Commit d547a32

Browse files
committed
Merge remote-tracking branch 'origin/development', v0.3.2
2 parents d229aa4 + 112b77b commit d547a32

File tree

6 files changed

+794
-33
lines changed

6 files changed

+794
-33
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ JavaScript client for the openEO API.
44

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

7-
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.
7+
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.
88

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

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

1818
To install it with npm: `npm install @openeo/js-client`

examples/discovery.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<title>openEO JS client - Discovery example</title>
55
<meta charset="UTF-8">
66
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7-
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
7+
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
88
<script src="../openeo.js"></script>
99
<script type="text/javascript">
1010
var url = "https://earthengine.openeo.org/v0.3"; // Insert the openEO server URL here

openeo.js

Lines changed: 85 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class OpenEO {
3535
}
3636

3737
version() {
38-
return "0.3.0";
38+
return "0.3.2";
3939
}
4040
}
4141

@@ -94,6 +94,21 @@ class Connection {
9494
.then(response => response.data);
9595
}
9696

97+
buildProcessGraph() {
98+
return this.listProcesses()
99+
.then(data => {
100+
var builder = {};
101+
for(let i in data.processes) {
102+
let process = data.processes[i];
103+
builder[process.name] = (options) => {
104+
options.process_id = process.name;
105+
return options;
106+
}
107+
}
108+
return builder;
109+
});
110+
}
111+
97112
authenticateOIDC(options = null) {
98113
return Promise.reject(new Error("Not implemented yet."));
99114
}
@@ -204,7 +219,7 @@ class Connection {
204219
}
205220

206221
createJob(processGraph, outputFormat = null, outputParameters = {}, title = null, description = null, plan = null, budget = null, additional = {}) {
207-
var jobObject = Object.assign(additional, {
222+
var jobObject = Object.assign({}, additional, {
208223
title: title,
209224
description: description,
210225
process_graph: processGraph,
@@ -328,7 +343,48 @@ class Connection {
328343
break;
329344
}
330345

331-
return axios(options);
346+
return axios(options).catch(error => {
347+
return new Promise((resolve, reject) => {
348+
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) {
349+
// JSON error responses are Blobs and streams if responseType is set as such, so convert to JSON if required.
350+
// See: https://github.com/axios/axios/issues/815
351+
try {
352+
switch(options.responseType) {
353+
case 'blob':
354+
const fileReader = new FileReader();
355+
fileReader.onerror = () => {
356+
fileReader.abort();
357+
reject(error);
358+
};
359+
fileReader.onload = () => {
360+
reject(JSON.parse(fileReader.result));
361+
};
362+
fileReader.readAsText(error.response.data);
363+
break;
364+
case 'stream':
365+
const chunks = "";
366+
error.response.data.on("data", chunk => {
367+
chunks.push(chunk);
368+
});
369+
readStream.on("error", () => {
370+
reject(error);
371+
});
372+
readStream.on("end", () => {
373+
reject(JSON.parse(Buffer.concat(chunks).toString()));
374+
});
375+
break;
376+
default:
377+
reject(error);
378+
}
379+
} catch (exception) {
380+
reject(error);
381+
}
382+
}
383+
else {
384+
reject(error);
385+
}
386+
});
387+
});
332388
}
333389

334390
_resetAuth() {
@@ -498,7 +554,7 @@ class Subscriptions {
498554

499555
_flushQueue() {
500556
if(this.socket.readyState === this.socket.OPEN) {
501-
for(var i in this.messageQueue) {
557+
for(let i in this.messageQueue) {
502558
this.socket.send(JSON.stringify(this.messageQueue[i]));
503559
}
504560

@@ -538,7 +594,7 @@ class Subscriptions {
538594
parameters = {};
539595
}
540596

541-
var payloadParameters = Object.assign(parameters, { topic: topic });
597+
var payloadParameters = Object.assign({}, parameters, { topic: topic });
542598

543599
this._sendMessage(action, {
544600
topics: [payloadParameters]
@@ -553,15 +609,19 @@ class Capabilities {
553609
if(!data || !data.version || !data.endpoints) {
554610
throw new Error("Data is not a valid Capabilities response");
555611
}
556-
this._data = data;
612+
this.data = data;
613+
}
614+
615+
toPlainObject() {
616+
return this.data;
557617
}
558618

559619
version() {
560-
return this._data.version;
620+
return this.data.version;
561621
}
562622

563623
listFeatures() {
564-
return this._data.endpoints;
624+
return this.data.endpoints;
565625
}
566626

567627
hasFeature(methodName) {
@@ -606,14 +666,14 @@ class Capabilities {
606666
};
607667

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

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

624684
currency() {
625-
return (this._data.billing ? this._data.billing.currency : null);
685+
return (this.data.billing ? this.data.billing.currency : null);
626686
}
627687

628688
listPlans() {
629-
return (this._data.billing ? this._data.billing.plans : null);
689+
return (this.data.billing ? this.data.billing.plans : null);
630690
}
631691
}
632692

@@ -637,8 +697,8 @@ class BaseEntity {
637697
this.connection = connection;
638698
this.clientNames = {};
639699
this.extra = {};
640-
for(var i in properties) {
641-
var backend, client;
700+
for(let i in properties) {
701+
let backend, client;
642702
if (Array.isArray(properties[i])) {
643703
backend = properties[i][0];
644704
client = properties[i][1];
@@ -655,7 +715,7 @@ class BaseEntity {
655715
}
656716

657717
setAll(metadata) {
658-
for (var name in metadata) {
718+
for(let name in metadata) {
659719
if (typeof this.clientNames[name] === 'undefined') {
660720
this.extra[name] = metadata[name];
661721
}
@@ -668,8 +728,8 @@ class BaseEntity {
668728

669729
getAll() {
670730
var obj = {};
671-
for (var backend in this.clientNames) {
672-
var client = this.clientNames[backend];
731+
for(let backend in this.clientNames) {
732+
let client = this.clientNames[backend];
673733
obj[client] = this[client];
674734
}
675735
return Object.assign(obj, this.extra);
@@ -736,7 +796,7 @@ class File extends BaseEntity {
736796
}
737797
};
738798
if (typeof statusCallback === 'function') {
739-
options.onUploadProgress = function(progressEvent) {
799+
options.onUploadProgress = (progressEvent) => {
740800
var percentCompleted = Math.round( (progressEvent.loaded * 100) / progressEvent.total );
741801
statusCallback(percentCompleted);
742802
};
@@ -836,11 +896,11 @@ class Job extends BaseEntity {
836896

837897
var promises = [];
838898
var files = [];
839-
for(var i in list.links) {
840-
var link = list.links[i].href;
841-
var parsedUrl = url.parse(link);
842-
var targetPath = path.join(targetFolder, path.basename(parsedUrl.pathname));
843-
var p = this.connection.download(link, false)
899+
for(let i in list.links) {
900+
let link = list.links[i].href;
901+
let parsedUrl = url.parse(link);
902+
let targetPath = path.join(targetFolder, path.basename(parsedUrl.pathname));
903+
let p = this.connection.download(link, false)
844904
.then(response => this.connection._saveToFileNode(response.data, targetPath))
845905
.then(() => files.push(targetPath));
846906
promises.push(p);
@@ -926,13 +986,14 @@ if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
926986
module.exports = toExport;
927987
}
928988
else {
989+
/* istanbul ignore next */
929990
if (typeof define === 'function' && define.amd) {
930991
define([], function () {
931992
return toExport;
932993
});
933994
}
934995
else {
935-
for (let exportObjName in toExport) {
996+
for(let exportObjName in toExport) {
936997
window[exportObjName] = toExport[exportObjName];
937998
}
938999
}

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@openeo/js-client",
3-
"version": "0.3.1",
3+
"version": "0.3.2",
44
"author": "openEO Consortium",
55
"contributors": [
66
{
@@ -30,7 +30,8 @@
3030
"ws": "^6.1.2"
3131
},
3232
"scripts": {
33-
"test": "jest --env=jsdom",
34-
"test_node": "jest --env=node"
33+
"test": "jest basic.test.js --env=jsdom",
34+
"test_node": "jest basic.test.js --env=node",
35+
"test_gee": "jest earthengine.test.js --env=node"
3536
}
3637
}

tests/basic.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
const { OpenEO } = require('../openeo.js');
2+
const packageInfo = require('../package.json');
23

3-
describe('Check version number', () => {
4+
describe('Basic client tests', () => {
45
var obj = new OpenEO();
56
test('Check that import worked', () => {
67
expect(obj).not.toBeNull();
78
expect(obj).toBeInstanceOf(OpenEO);
89
});
910
test('Check version number', () => {
10-
expect(obj.version()).toBe("0.3.0");
11+
expect(obj.version()).toBe(packageInfo.version);
1112
});
1213
});

0 commit comments

Comments
 (0)