@openeo/js-client 2.5.1 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/logs.js CHANGED
@@ -1,68 +1,73 @@
1
- const Utils = require('@openeo/js-commons/src/utils');
2
-
3
- /**
4
- * Interface to loop through the logs.
5
- */
6
- class Logs {
7
-
8
- /**
9
- * Creates a new Logs instance to retrieve logs from a back-end.
10
- *
11
- * @param {Connection} connection - A Connection object representing an established connection to an openEO back-end.
12
- * @param {string} endpoint - The relative endpoint to request the logs from, usually `/jobs/.../logs` or `/services/.../logs` with `...` being the actual job or service id.
13
- */
14
- constructor(connection, endpoint) {
15
- /**
16
- * @protected
17
- * @type {Connection}
18
- */
19
- this.connection = connection;
20
- this.endpoint = endpoint;
21
- this.lastId = "";
22
- }
23
-
24
- /**
25
- * Retrieves the next log entries since the last request.
26
- *
27
- * Retrieves log entries only.
28
- *
29
- * @async
30
- * @param {number} limit - The number of log entries to retrieve per request, as integer.
31
- * @returns {Promise<Array.<Log>>}
32
- */
33
- async nextLogs(limit = null) {
34
- let response = await this.next(limit);
35
- return Array.isArray(response.logs) ? response.logs : [];
36
- }
37
-
38
- /**
39
- * Retrieves the next log entries since the last request.
40
- *
41
- * Retrieves the full response compliant to the API, including log entries and links.
42
- *
43
- * @async
44
- * @param {number} limit - The number of log entries to retrieve per request, as integer.
45
- * @returns {Promise<LogsAPI>}
46
- */
47
- async next(limit = null) {
48
- let query = {
49
- offset: this.lastId
50
- };
51
- if (limit > 0) {
52
- query.limit = limit;
53
- }
54
- let response = await this.connection._get(this.endpoint, query);
55
- if (Array.isArray(response.data.logs) && response.data.logs.length > 0) {
56
- response.data.logs = response.data.logs.filter(log => Utils.isObject(log) && typeof log.id === 'string');
57
- this.lastId = response.data.logs[response.data.logs.length - 1].id;
58
- }
59
- else {
60
- response.data.logs = [];
61
- }
62
- response.data.links = Array.isArray(response.data.links) ? response.data.links : [];
63
- return response.data;
64
- }
65
-
66
- }
67
-
68
- module.exports = Logs;
1
+ const Utils = require('@openeo/js-commons/src/utils');
2
+
3
+ /**
4
+ * Interface to loop through the logs.
5
+ */
6
+ class Logs {
7
+
8
+ /**
9
+ * Creates a new Logs instance to retrieve logs from a back-end.
10
+ *
11
+ * @param {Connection} connection - A Connection object representing an established connection to an openEO back-end.
12
+ * @param {string} endpoint - The relative endpoint to request the logs from, usually `/jobs/.../logs` or `/services/.../logs` with `...` being the actual job or service id.
13
+ * @param {?string} [level=null] - Minimum level of logs to return.
14
+ */
15
+ constructor(connection, endpoint, level = null) {
16
+ /**
17
+ * @protected
18
+ * @type {Connection}
19
+ */
20
+ this.connection = connection;
21
+ this.endpoint = endpoint;
22
+ this.lastId = "";
23
+ this.level = level;
24
+ }
25
+
26
+ /**
27
+ * Retrieves the next log entries since the last request.
28
+ *
29
+ * Retrieves log entries only.
30
+ *
31
+ * @async
32
+ * @param {number} limit - The number of log entries to retrieve per request, as integer.
33
+ * @returns {Promise<Array.<Log>>}
34
+ */
35
+ async nextLogs(limit = null) {
36
+ let response = await this.next(limit);
37
+ return Array.isArray(response.logs) ? response.logs : [];
38
+ }
39
+
40
+ /**
41
+ * Retrieves the next log entries since the last request.
42
+ *
43
+ * Retrieves the full response compliant to the API, including log entries and links.
44
+ *
45
+ * @async
46
+ * @param {number} limit - The number of log entries to retrieve per request, as integer.
47
+ * @returns {Promise<LogsAPI>}
48
+ */
49
+ async next(limit = null) {
50
+ let query = {
51
+ offset: this.lastId
52
+ };
53
+ if (limit > 0) {
54
+ query.limit = limit;
55
+ }
56
+ if (this.level) {
57
+ query.level = this.level;
58
+ }
59
+ let response = await this.connection._get(this.endpoint, query);
60
+ if (Array.isArray(response.data.logs) && response.data.logs.length > 0) {
61
+ response.data.logs = response.data.logs.filter(log => Utils.isObject(log) && typeof log.id === 'string');
62
+ this.lastId = response.data.logs[response.data.logs.length - 1].id;
63
+ }
64
+ else {
65
+ response.data.logs = [];
66
+ }
67
+ response.data.links = Array.isArray(response.data.links) ? response.data.links : [];
68
+ return response.data;
69
+ }
70
+
71
+ }
72
+
73
+ module.exports = Logs;
package/src/node.js CHANGED
@@ -1,168 +1,168 @@
1
- const fs = require('fs');
2
- const url = require("url");
3
- const path = require("path");
4
- const Stream = require('stream');
5
-
6
- /**
7
- * Platform dependant utilities for the openEO JS Client.
8
- *
9
- * Node.js implementation, don't use in other environments.
10
- *
11
- * @hideconstructor
12
- */
13
- class Environment {
14
-
15
- /**
16
- * Returns the name of the Environment, here `Node`.
17
- *
18
- * @returns {string}
19
- * @static
20
- */
21
- static getName() {
22
- return 'Node';
23
- }
24
-
25
- /**
26
- * Returns the URL of the server instance.
27
- *
28
- * @returns {string}
29
- * @static
30
- */
31
- static getUrl() {
32
- return Environment.url;
33
- }
34
-
35
- /**
36
- * Sets the URL of the server instance.
37
- *
38
- * @param {string} uri
39
- * @static
40
- */
41
- static setUrl(uri) {
42
- Environment.url = uri;
43
- }
44
-
45
- /**
46
- * Handles errors from the API that are returned as Streams.
47
- *
48
- * @ignore
49
- * @static
50
- * @param {Stream.Readable} error
51
- * @returns {Promise<void>}
52
- */
53
- static handleErrorResponse(error) {
54
- return new Promise((resolve, reject) => {
55
- let chunks = [];
56
- error.response.data.on("data", chunk => chunks.push(chunk));
57
- error.response.data.on("error", streamError => reject(streamError));
58
- error.response.data.on("end", () => resolve(JSON.parse(Buffer.concat(chunks).toString())));
59
- });
60
- }
61
-
62
- /**
63
- * Returns how binary responses from the servers are returned (`stream` or `blob`).
64
- *
65
- * @returns {string}
66
- * @static
67
- */
68
- static getResponseType() {
69
- return 'stream';
70
- }
71
-
72
- /**
73
- * Encodes a string into Base64 encoding.
74
- *
75
- * @static
76
- * @param {string|Buffer} str - String to encode.
77
- * @returns {string} String encoded in Base64.
78
- */
79
- static base64encode(str) {
80
- let buffer;
81
- if (str instanceof Buffer) {
82
- buffer = str;
83
- } else {
84
- buffer = Buffer.from(str.toString(), 'binary');
85
- }
86
- return buffer.toString('base64');
87
- }
88
-
89
- /**
90
- * Detect the file name for the given data source.
91
- *
92
- * @ignore
93
- * @static
94
- * @param {string} source - A path to a file as string.
95
- * @returns {string}
96
- */
97
- static fileNameForUpload(source) {
98
- return path.basename(source);
99
- }
100
-
101
- /**
102
- * Get the data from the source that should be uploaded.
103
- *
104
- * @ignore
105
- * @static
106
- * @param {string} source - A path to a file as string.
107
- * @returns {Stream.Readable}
108
- */
109
- static dataForUpload(source) {
110
- return fs.createReadStream(source);
111
- }
112
-
113
- /**
114
- * Downloads files to local storage and returns a list of file paths.
115
- *
116
- * @static
117
- * @param {Connection} con
118
- * @param {Array.<object.<string, *>>} assets
119
- * @param {string} targetFolder
120
- * @returns {Promise<Array.<string>>}
121
- * @throws {Error}
122
- */
123
- static async downloadResults(con, assets, targetFolder) {
124
- let files = [];
125
- const promises = assets.map(async (link) => {
126
- let parsedUrl = url.parse(link.href);
127
- let targetPath = path.join(targetFolder, path.basename(parsedUrl.pathname));
128
- let data = await con.download(link.href, false);
129
- if (data instanceof Stream.Readable) {
130
- await Environment.saveToFile(data, targetPath);
131
- files.push(targetPath);
132
- }
133
- else {
134
- throw new Error("Data retrieved is not a Stream");
135
- }
136
- });
137
-
138
- await Promise.all(promises);
139
- return files;
140
- }
141
-
142
- /**
143
- * Streams data into a file.
144
- *
145
- * @static
146
- * @async
147
- * @param {Stream.Readable} data - Data stream to read from.
148
- * @param {string} filename - File path to store the data at.
149
- * @returns {Promise<void>}
150
- * @throws {Error}
151
- */
152
- static saveToFile(data, filename) {
153
- return new Promise((resolve, reject) => {
154
- let writeStream = fs.createWriteStream(filename);
155
- writeStream.on('close', (err) => {
156
- if (err) {
157
- return reject(err);
158
- }
159
- resolve();
160
- });
161
- data.pipe(writeStream);
162
- });
163
- }
164
- }
165
-
166
- Environment.url = '';
167
-
168
- module.exports = Environment;
1
+ const fs = require('fs');
2
+ const url = require("url");
3
+ const path = require("path");
4
+ const Stream = require('stream');
5
+
6
+ /**
7
+ * Platform dependant utilities for the openEO JS Client.
8
+ *
9
+ * Node.js implementation, don't use in other environments.
10
+ *
11
+ * @hideconstructor
12
+ */
13
+ class Environment {
14
+
15
+ /**
16
+ * Returns the name of the Environment, here `Node`.
17
+ *
18
+ * @returns {string}
19
+ * @static
20
+ */
21
+ static getName() {
22
+ return 'Node';
23
+ }
24
+
25
+ /**
26
+ * Returns the URL of the server instance.
27
+ *
28
+ * @returns {string}
29
+ * @static
30
+ */
31
+ static getUrl() {
32
+ return Environment.url;
33
+ }
34
+
35
+ /**
36
+ * Sets the URL of the server instance.
37
+ *
38
+ * @param {string} uri
39
+ * @static
40
+ */
41
+ static setUrl(uri) {
42
+ Environment.url = uri;
43
+ }
44
+
45
+ /**
46
+ * Handles errors from the API that are returned as Streams.
47
+ *
48
+ * @ignore
49
+ * @static
50
+ * @param {Stream.Readable} error
51
+ * @returns {Promise<void>}
52
+ */
53
+ static handleErrorResponse(error) {
54
+ return new Promise((resolve, reject) => {
55
+ let chunks = [];
56
+ error.response.data.on("data", chunk => chunks.push(chunk));
57
+ error.response.data.on("error", streamError => reject(streamError));
58
+ error.response.data.on("end", () => resolve(JSON.parse(Buffer.concat(chunks).toString())));
59
+ });
60
+ }
61
+
62
+ /**
63
+ * Returns how binary responses from the servers are returned (`stream` or `blob`).
64
+ *
65
+ * @returns {string}
66
+ * @static
67
+ */
68
+ static getResponseType() {
69
+ return 'stream';
70
+ }
71
+
72
+ /**
73
+ * Encodes a string into Base64 encoding.
74
+ *
75
+ * @static
76
+ * @param {string|Buffer} str - String to encode.
77
+ * @returns {string} String encoded in Base64.
78
+ */
79
+ static base64encode(str) {
80
+ let buffer;
81
+ if (str instanceof Buffer) {
82
+ buffer = str;
83
+ } else {
84
+ buffer = Buffer.from(str.toString(), 'binary');
85
+ }
86
+ return buffer.toString('base64');
87
+ }
88
+
89
+ /**
90
+ * Detect the file name for the given data source.
91
+ *
92
+ * @ignore
93
+ * @static
94
+ * @param {string} source - A path to a file as string.
95
+ * @returns {string}
96
+ */
97
+ static fileNameForUpload(source) {
98
+ return path.basename(source);
99
+ }
100
+
101
+ /**
102
+ * Get the data from the source that should be uploaded.
103
+ *
104
+ * @ignore
105
+ * @static
106
+ * @param {string} source - A path to a file as string.
107
+ * @returns {Stream.Readable}
108
+ */
109
+ static dataForUpload(source) {
110
+ return fs.createReadStream(source);
111
+ }
112
+
113
+ /**
114
+ * Downloads files to local storage and returns a list of file paths.
115
+ *
116
+ * @static
117
+ * @param {Connection} con
118
+ * @param {Array.<object.<string, *>>} assets
119
+ * @param {string} targetFolder
120
+ * @returns {Promise<Array.<string>>}
121
+ * @throws {Error}
122
+ */
123
+ static async downloadResults(con, assets, targetFolder) {
124
+ let files = [];
125
+ const promises = assets.map(async (link) => {
126
+ let parsedUrl = url.parse(link.href);
127
+ let targetPath = path.join(targetFolder, path.basename(parsedUrl.pathname));
128
+ let data = await con.download(link.href, false);
129
+ if (data instanceof Stream.Readable) {
130
+ await Environment.saveToFile(data, targetPath);
131
+ files.push(targetPath);
132
+ }
133
+ else {
134
+ throw new Error("Data retrieved is not a Stream");
135
+ }
136
+ });
137
+
138
+ await Promise.all(promises);
139
+ return files;
140
+ }
141
+
142
+ /**
143
+ * Streams data into a file.
144
+ *
145
+ * @static
146
+ * @async
147
+ * @param {Stream.Readable} data - Data stream to read from.
148
+ * @param {string} filename - File path to store the data at.
149
+ * @returns {Promise<void>}
150
+ * @throws {Error}
151
+ */
152
+ static saveToFile(data, filename) {
153
+ return new Promise((resolve, reject) => {
154
+ let writeStream = fs.createWriteStream(filename);
155
+ writeStream.on('close', (err) => {
156
+ if (err) {
157
+ return reject(err);
158
+ }
159
+ resolve();
160
+ });
161
+ data.pipe(writeStream);
162
+ });
163
+ }
164
+ }
165
+
166
+ Environment.url = '';
167
+
168
+ module.exports = Environment;