geoserver-node-client 0.0.6 → 1.1.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.
@@ -1,4 +1,5 @@
1
1
  import fetch from 'node-fetch';
2
+ import { getGeoServerResponseText, GeoServerResponseError } from './util/geoserver.js';
2
3
 
3
4
  /**
4
5
  * Client for GeoServer image mosaics
@@ -10,13 +11,11 @@ export default class ImageMosaicClient {
10
11
  * Creates a GeoServer REST ImageMosaicClient instance.
11
12
  *
12
13
  * @param {String} url The URL of the GeoServer REST API endpoint
13
- * @param {String} user The user for the GeoServer REST API
14
- * @param {String} password The password for the GeoServer REST API
14
+ * @param {String} auth The Basic Authentication string
15
15
  */
16
- constructor (url, user, password) {
17
- this.url = url.endsWith('/') ? url : url + '/';
18
- this.user = user;
19
- this.password = password;
16
+ constructor (url, auth) {
17
+ this.url = url;
18
+ this.auth = auth;
20
19
  }
21
20
 
22
21
  /**
@@ -26,32 +25,28 @@ export default class ImageMosaicClient {
26
25
  * @param {String} coverageStore CoverageStore of image mosaic
27
26
  * @param {*} coverage Name of image mosaic
28
27
  *
29
- * @returns {Object|Boolean} An object with the granules or 'false'
28
+ * @throws Error if request fails
29
+ *
30
+ * @returns {Object} An object with the granules
30
31
  */
31
32
  async getGranules (workspace, coverageStore, coverage) {
32
- try {
33
- const auth =
34
- Buffer.from(this.user + ':' + this.password).toString('base64');
35
- const url = this.url + 'workspaces/' + workspace + '/coveragestores/' +
33
+ const url = this.url + 'workspaces/' + workspace + '/coveragestores/' +
36
34
  coverageStore + '/coverages/' + coverage + '/index/granules.json';
37
- const response = await fetch(url, {
38
- credentials: 'include',
39
- method: 'GET',
40
- headers: {
41
- Authorization: 'Basic ' + auth,
42
- 'Content-type': 'text/plain'
43
- }
44
- });
45
-
46
- if (response.status === 200) {
47
- return await response.json();
48
- } else {
49
- console.warn(await response.text());
50
- return false;
35
+ const response = await fetch(url, {
36
+ credentials: 'include',
37
+ method: 'GET',
38
+ headers: {
39
+ Authorization: this.auth,
40
+ 'Content-type': 'text/plain'
51
41
  }
52
- } catch (error) {
53
- return false;
42
+ });
43
+
44
+ if (!response.ok) {
45
+ const geoServerResponse = await getGeoServerResponseText(response);
46
+ throw new GeoServerResponseError(null, geoServerResponse);
54
47
  }
48
+
49
+ return response.json();
55
50
  }
56
51
 
57
52
  /**
@@ -61,33 +56,29 @@ export default class ImageMosaicClient {
61
56
  * @param {String} coverageStore CoverageStore of image mosaic
62
57
  * @param {String} filePath Server path of folder to harvest
63
58
  *
64
- * @returns {Object|Boolean} An object with the granules or 'false'
59
+ * @throws Error if request fails
60
+ *
61
+ * @returns {Object} An object with the granules
65
62
  */
66
63
  async harvestGranules (workspace, coverageStore, filePath) {
67
- try {
68
- const auth =
69
- Buffer.from(this.user + ':' + this.password).toString('base64');
70
- const url = this.url + 'workspaces/' + workspace + '/coveragestores/' + coverageStore + '/external.imagemosaic';
64
+ const url = this.url + 'workspaces/' + workspace + '/coveragestores/' + coverageStore + '/external.imagemosaic';
71
65
 
72
- const response = await fetch(url, {
73
- credentials: 'include',
74
- method: 'POST',
75
- headers: {
76
- Authorization: 'Basic ' + auth,
77
- 'Content-Type': 'text/plain'
78
- },
79
- body: filePath
80
- });
66
+ const response = await fetch(url, {
67
+ credentials: 'include',
68
+ method: 'POST',
69
+ headers: {
70
+ Authorization: this.auth,
71
+ 'Content-Type': 'text/plain'
72
+ },
73
+ body: filePath
74
+ });
81
75
 
82
- if (response.status === 200) {
83
- return await response.text();
84
- } else {
85
- console.warn(await response.text());
86
- return false;
87
- }
88
- } catch (error) {
89
- return false;
76
+ if (!response.ok) {
77
+ const geoServerResponse = await getGeoServerResponseText(response);
78
+ throw new GeoServerResponseError(null, geoServerResponse);
90
79
  }
80
+
81
+ return response.json();
91
82
  }
92
83
 
93
84
  /**
@@ -97,69 +88,55 @@ export default class ImageMosaicClient {
97
88
  * @param {String} coverageStore CoverageStore of image mosaic
98
89
  * @param {String} filePath Server file path of new granule
99
90
  *
100
- * @returns {Boolean} If granule could be added
91
+ * @throws Error if request fails
101
92
  */
102
93
  async addGranuleByServerFile (workspace, coverageStore, filePath) {
103
- try {
104
- const auth =
105
- Buffer.from(this.user + ':' + this.password).toString('base64');
106
- const url = this.url + 'workspaces/' + workspace + '/coveragestores/' + coverageStore + '/external.imagemosaic';
94
+ const url = this.url + 'workspaces/' + workspace + '/coveragestores/' + coverageStore + '/external.imagemosaic';
107
95
 
108
- const response = await fetch(url, {
109
- credentials: 'include',
110
- method: 'POST',
111
- headers: {
112
- Authorization: 'Basic ' + auth,
113
- 'Content-type': 'text/plain'
114
- },
115
- body: filePath
116
- });
96
+ const response = await fetch(url, {
97
+ credentials: 'include',
98
+ method: 'POST',
99
+ headers: {
100
+ Authorization: this.auth,
101
+ 'Content-type': 'text/plain'
102
+ },
103
+ body: filePath
104
+ });
117
105
 
118
- if (response.status === 202) {
119
- return true;
120
- } else {
121
- console.warn(await response.text());
122
- return false;
123
- }
124
- } catch (error) {
125
- return false;
106
+ if (!response.ok) {
107
+ const geoServerResponse = await getGeoServerResponseText(response);
108
+ throw new GeoServerResponseError(null, geoServerResponse);
126
109
  }
127
110
  }
128
111
 
129
112
  /**
130
113
  * Deletes a single granule of an image mosaic.
131
114
  *
132
- * @param {*} workspace Workspace of image mosaic
133
- * @param {*} coverageStore CoverageStore of image mosaic
134
- * @param {*} coverage Name of image mosaic
135
- * @param {*} covFileLocation Location of coverage file
115
+ * @param {String} workspace Workspace of image mosaic
116
+ * @param {String} coverageStore CoverageStore of image mosaic
117
+ * @param {String} coverage Name of image mosaic
118
+ * @param {String} covFileLocation Location of coverage file
136
119
  *
137
- * @returns {Boolean} If granule could be deleted
120
+ * @throws Error if request fails
138
121
  */
139
122
  async deleteSingleGranule (workspace, coverageStore, coverage, covFileLocation) {
140
- try {
141
- const auth =
142
- Buffer.from(this.user + ':' + this.password).toString('base64');
143
- let url = this.url + 'workspaces/' + workspace + '/coveragestores/' + coverageStore + '/coverages/' + coverage + '/index/granules.xml';
144
- url += '?filter=location=\'' + covFileLocation + '\'';
123
+ let url = this.url + 'workspaces/' + workspace + '/coveragestores/' + coverageStore + '/coverages/' + coverage + '/index/granules.xml';
124
+ url += '?filter=location=\'' + covFileLocation + '\'';
145
125
 
146
- const response = await fetch(url, {
147
- credentials: 'include',
148
- method: 'DELETE',
149
- headers: {
150
- Authorization: 'Basic ' + auth,
151
- 'Content-type': 'text/plain'
152
- }
153
- });
154
-
155
- if (response.status === 200) {
156
- return true;
157
- } else {
158
- console.warn(await response.text());
159
- return false;
126
+ const response = await fetch(url, {
127
+ credentials: 'include',
128
+ method: 'DELETE',
129
+ headers: {
130
+ Authorization: this.auth,
131
+ 'Content-type': 'text/plain'
160
132
  }
161
- } catch (error) {
162
- return false;
133
+ });
134
+
135
+ if (!response.ok) {
136
+ const geoServerResponse = await getGeoServerResponseText(response);
137
+ throw new GeoServerResponseError(null, geoServerResponse);
163
138
  }
139
+
140
+ return true;
164
141
  }
165
142
  }