geoserver-node-client 0.0.7 → 1.2.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/settings.js CHANGED
@@ -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 settings.
@@ -10,60 +11,54 @@ export default class SettingsClient {
10
11
  * Creates a GeoServer REST SettingsClient 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
  /**
23
22
  * Get the complete GeoServer settings object.
24
23
  *
25
- * @returns {Object|Boolean} Settings object or 'false'
24
+ * @throws Error if request fails
25
+ *
26
+ * @returns {Object} Settings object
26
27
  */
27
28
  async getSettings () {
28
- try {
29
- const auth =
30
- Buffer.from(this.user + ':' + this.password).toString('base64');
31
- const response = await fetch(this.url + 'settings.json', {
32
- credentials: 'include',
33
- method: 'GET',
34
- headers: {
35
- Authorization: 'Basic ' + auth
36
- }
37
- });
38
- return await response.json();
39
- } catch (error) {
40
- return false;
29
+ const response = await fetch(this.url + 'settings.json', {
30
+ credentials: 'include',
31
+ method: 'GET',
32
+ headers: {
33
+ Authorization: this.auth
34
+ }
35
+ });
36
+ if (!response.ok) {
37
+ const geoServerResponse = await getGeoServerResponseText(response);
38
+ throw new GeoServerResponseError(null, geoServerResponse);
41
39
  }
40
+ return response.json();
42
41
  }
43
42
 
44
43
  /**
45
44
  * Update the global GeoServer settings.
46
45
  *
47
- * @param {GeoServer} settings
48
- * @returns {Boolean} Flag indicating if request was successful
46
+ * @param {Object} settings The adapted GeoServer settings object
49
47
  */
50
48
  async updateSettings (settings) {
51
- try {
52
- const auth =
53
- Buffer.from(this.user + ':' + this.password).toString('base64');
54
- const response = await fetch(this.url + 'settings', {
55
- credentials: 'include',
56
- method: 'PUT',
57
- headers: {
58
- Authorization: 'Basic ' + auth,
59
- 'Content-Type': 'application/json'
60
- },
61
- body: JSON.stringify(settings)
62
- });
49
+ const response = await fetch(this.url + 'settings', {
50
+ credentials: 'include',
51
+ method: 'PUT',
52
+ headers: {
53
+ Authorization: this.auth,
54
+ 'Content-Type': 'application/json'
55
+ },
56
+ body: JSON.stringify(settings)
57
+ });
63
58
 
64
- return response.status === 200;
65
- } catch (error) {
66
- return false;
59
+ if (!response.ok) {
60
+ const geoServerResponse = await getGeoServerResponseText(response);
61
+ throw new GeoServerResponseError(null, geoServerResponse);
67
62
  }
68
63
  }
69
64
 
@@ -71,42 +66,41 @@ export default class SettingsClient {
71
66
  * Update the global proxyBaseUrl setting.
72
67
  *
73
68
  * @param {String} proxyBaseUrl The proxy base URL
74
- * @returns {Boolean} Flag indicating if request was successful
75
69
  */
76
70
  async updateProxyBaseUrl (proxyBaseUrl) {
77
71
  const settingsJson = await this.getSettings();
78
72
 
73
+ // check if settings are correctly formatted
79
74
  if (!settingsJson.global && !settingsJson.global.settings) {
80
- // settings seem to be wrongly formated
81
75
  return false;
82
76
  }
83
77
 
84
78
  // add proxyBaseUrl to settings
85
79
  settingsJson.global.settings.proxyBaseUrl = proxyBaseUrl;
86
80
 
87
- return await this.updateSettings(settingsJson);
81
+ await this.updateSettings(settingsJson);
88
82
  }
89
83
 
90
84
  /**
91
85
  * Get the contact information of the GeoServer.
92
86
  *
93
- * @returns {Object|Boolean} An object with contact information or 'false'
87
+ * @throws Error if request fails
88
+ *
89
+ * @returns {Object} An object with contact information
94
90
  */
95
91
  async getContactInformation () {
96
- try {
97
- const auth =
98
- Buffer.from(this.user + ':' + this.password).toString('base64');
99
- const response = await fetch(this.url + 'settings/contact', {
100
- credentials: 'include',
101
- method: 'GET',
102
- headers: {
103
- Authorization: 'Basic ' + auth
104
- }
105
- });
106
- return await response.json();
107
- } catch (error) {
108
- return false;
92
+ const response = await fetch(this.url + 'settings/contact', {
93
+ credentials: 'include',
94
+ method: 'GET',
95
+ headers: {
96
+ Authorization: this.auth
97
+ }
98
+ });
99
+ if (!response.ok) {
100
+ const geoServerResponse = await getGeoServerResponseText(response);
101
+ throw new GeoServerResponseError(null, geoServerResponse);
109
102
  }
103
+ return response.json();
110
104
  }
111
105
 
112
106
  /**
@@ -124,47 +118,38 @@ export default class SettingsClient {
124
118
  * @param {String} [contactPerson] The contact person
125
119
  * @param {String} [phoneNumber] The contact's phone number
126
120
  *
127
- * @returns {Boolean} If contact information could be updated.
121
+ * @throws Error if request fails
128
122
  */
129
123
  async updateContactInformation (address, city, country, postalCode, state, email, organization, contactPerson, phoneNumber) {
130
- try {
131
- const contact = {
132
- address: address,
133
- addressCity: city,
134
- addressCountry: country,
135
- addressPostalCode: postalCode,
136
- addressState: state,
137
- contactEmail: email,
138
- contactOrganization: organization,
139
- contactPerson: contactPerson,
140
- contactVoice: phoneNumber
141
- };
142
-
143
- const body = {
144
- contact: contact
145
- };
124
+ const contact = {
125
+ address: address,
126
+ addressCity: city,
127
+ addressCountry: country,
128
+ addressPostalCode: postalCode,
129
+ addressState: state,
130
+ contactEmail: email,
131
+ contactOrganization: organization,
132
+ contactPerson: contactPerson,
133
+ contactVoice: phoneNumber
134
+ };
146
135
 
147
- const auth = Buffer.from(this.user + ':' + this.password).toString('base64');
148
- const url = this.url + 'settings/contact';
149
- const response = await fetch(url, {
150
- credentials: 'include',
151
- method: 'PUT',
152
- headers: {
153
- Authorization: 'Basic ' + auth,
154
- 'Content-Type': 'application/json'
155
- },
156
- body: JSON.stringify(body)
157
- });
136
+ const body = {
137
+ contact: contact
138
+ };
158
139
 
159
- if (response.status === 200) {
160
- return true;
161
- } else {
162
- console.warn(await response.text());
163
- return false;
164
- }
165
- } catch (error) {
166
- console.log(error);
167
- return false;
140
+ const url = this.url + 'settings/contact';
141
+ const response = await fetch(url, {
142
+ credentials: 'include',
143
+ method: 'PUT',
144
+ headers: {
145
+ Authorization: this.auth,
146
+ 'Content-Type': 'application/json'
147
+ },
148
+ body: JSON.stringify(body)
149
+ });
150
+ if (!response.ok) {
151
+ const geoServerResponse = await getGeoServerResponseText(response);
152
+ throw new GeoServerResponseError(null, geoServerResponse);
168
153
  }
169
154
  }
170
155
  }