Skip to content

Commit 9756c05

Browse files
committed
pass request options to retrieve instances
1 parent 65f3bac commit 9756c05

File tree

1 file changed

+61
-64
lines changed

1 file changed

+61
-64
lines changed

src/api.js

Lines changed: 61 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import { multipartEncode, multipartDecode } from './message.js';
22

3+
/**
4+
* @typedef {Object} Request
5+
* @property {XMLHttpRequest} [instance] - If specified, the request to use, otherwise one will be created.
6+
* @property {function(ProgressEvent):void} [progressCallback] - A callback function to handle progress events.
7+
* @property {boolean} [withCredentials] - Whether to include credentials in the request.
8+
* @property {string} [responseType] - The response type of the request.
9+
*/
10+
311
function isObject(obj) {
412
return typeof obj === 'object' && obj !== null;
513
}
@@ -194,77 +202,75 @@ class DICOMwebClient {
194202
* @param {String} url
195203
* @param {String} method
196204
* @param {Object} headers
197-
* @param {Object} options
198-
* @param {Array.<RequestHook>} options.requestHooks - Request hooks.
199-
* @param {XMLHttpRequest} [options.request] - if specified, the request to use, otherwise one will be created; useful for adding custom upload and abort listeners/objects
205+
* @param {Request} [request] - if specified, the request to use, otherwise one will be created; useful for adding custom upload and abort listeners/objects
200206
* @return {*}
201207
* @private
202208
*/
203-
_httpRequest(url, method, headers = {}, options = {}) {
209+
_httpRequest(url, method, headers = {}, request = {}) {
204210
const { errorInterceptor, requestHooks } = this;
205-
211+
console.log('request', request);
206212
return new Promise((resolve, reject) => {
207-
let request = options.request ? options.request : new XMLHttpRequest();
213+
let instance = request.instance ? request.instance : new XMLHttpRequest();
208214

209-
request.open(method, url, true);
210-
if ('responseType' in options) {
211-
request.responseType = options.responseType;
215+
instance.open(method, url, true);
216+
if ('responseType' in request) {
217+
instance.responseType = request.responseType;
212218
}
213219

214220
if (typeof headers === 'object') {
215221
Object.keys(headers).forEach(key => {
216-
request.setRequestHeader(key, headers[key]);
222+
instance.setRequestHeader(key, headers[key]);
217223
});
218224
}
219225

220226
// now add custom headers from the user
221227
// (e.g. access tokens)
222228
const userHeaders = this.headers;
223229
Object.keys(userHeaders).forEach(key => {
224-
request.setRequestHeader(key, userHeaders[key]);
230+
instance.setRequestHeader(key, userHeaders[key]);
225231
});
226232

227233
// Event triggered when upload starts
228-
request.onloadstart = function onloadstart() {
234+
instance.onloadstart = function onloadstart() {
229235
debugLog('upload started: ', url)
230236
};
231237

232238
// Event triggered when upload ends
233-
request.onloadend = function onloadend() {
239+
instance.onloadend = function onloadend() {
234240
debugLog('upload finished')
235241
};
236242

237243
// Handle response message
238-
request.onreadystatechange = () => {
239-
if (request.readyState === 4) {
240-
if (request.status === 200) {
241-
const contentType = request.getResponseHeader('Content-Type');
244+
instance.onreadystatechange = () => {
245+
if (instance.readyState === 4) {
246+
if (instance.status === 200) {
247+
const contentType = instance.getResponseHeader('Content-Type');
242248
// Automatically distinguishes between multipart and singlepart in an array buffer, and
243249
// converts them into a consistent type.
244250
if (contentType && contentType.indexOf('multipart') !== -1) {
245-
resolve(multipartDecode(request.response));
246-
} else if (request.responseType === 'arraybuffer') {
247-
resolve([request.response]);
251+
resolve(multipartDecode(instance.response));
252+
} else if (instance.responseType === 'arraybuffer') {
253+
resolve([instance.response]);
248254
} else {
249-
resolve(request.response);
255+
resolve(instance.response);
250256
}
251-
} else if (request.status === 202) {
257+
} else if (instance.status === 202) {
252258
if (this.verbose) {
253-
console.warn('some resources already existed: ', request);
259+
console.warn('some resources already existed: ', instance);
254260
}
255-
resolve(request.response);
256-
} else if (request.status === 204) {
261+
resolve(instance.response);
262+
} else if (instance.status === 204) {
257263
if (this.verbose) {
258-
console.warn('empty response for request: ', request);
264+
console.warn('empty response for request: ', instance);
259265
}
260266
resolve([]);
261267
} else {
262268
const error = new Error('request failed');
263-
error.request = request;
264-
error.response = request.response;
265-
error.status = request.status;
269+
error.request = instance;
270+
error.response = instance.response;
271+
error.status = instance.status;
266272
if (this.verbose) {
267-
console.error('request failed: ', request);
273+
console.error('request failed: ', instance);
268274
console.error(error);
269275
console.error(error.response);
270276
}
@@ -277,9 +283,9 @@ class DICOMwebClient {
277283
};
278284

279285
// Event triggered while download progresses
280-
if ('progressCallback' in options) {
281-
if (typeof options.progressCallback === 'function') {
282-
request.onprogress = options.progressCallback;
286+
if ('progressCallback' in request) {
287+
if (typeof request.progressCallback === 'function') {
288+
instance.onprogress = request.progressCallback;
283289
}
284290
}
285291

@@ -289,20 +295,20 @@ class DICOMwebClient {
289295
const pipeRequestHooks = functions => args =>
290296
functions.reduce((props, fn) => fn(props, metadata), args);
291297
const pipedRequest = pipeRequestHooks(requestHooks);
292-
request = pipedRequest(request);
298+
instance = pipedRequest(instance);
293299
}
294300

295301
// Add withCredentials to request if needed
296-
if ('withCredentials' in options) {
297-
if (options.withCredentials) {
298-
request.withCredentials = true;
302+
if ('withCredentials' in request) {
303+
if (request.withCredentials) {
304+
instance.withCredentials = true;
299305
}
300306
}
301307

302-
if ('data' in options) {
303-
request.send(options.data);
308+
if ('data' in request) {
309+
instance.send(request.data);
304310
} else {
305-
request.send();
311+
instance.send();
306312
}
307313
});
308314
}
@@ -313,18 +319,12 @@ class DICOMwebClient {
313319
* @param {String} url
314320
* @param {Object} headers
315321
* @param {Object} responseType
316-
* @param {Function} progressCallback
317-
* @param {XMLHttpRequest} request - if specified, the request to use, otherwise one will be created; useful for adding custom upload and abort listeners/objects
322+
* @param {Request} request
318323
* @return {*}
319324
* @private
320325
*/
321-
_httpGet(url, headers, responseType, progressCallback, withCredentials,request) {
322-
return this._httpRequest(url, 'get', headers, {
323-
responseType,
324-
progressCallback,
325-
withCredentials,
326-
request
327-
});
326+
_httpGet(url, headers, request) {
327+
return this._httpRequest(url, 'get', headers, request);
328328
}
329329

330330
/**
@@ -683,17 +683,14 @@ class DICOMwebClient {
683683
* @param {Object[]} mediaTypes - Acceptable media types and optionally the UIDs of the
684684
corresponding transfer syntaxes
685685
* @param {Object} params - Additional HTTP GET query parameters
686-
* @param {Function} progressCallback
687-
* @param {XMLHttpRequest} request - if specified, the request to use, otherwise one will be created; useful for adding custom upload and abort listeners/objects
686+
* @param {Request} request - request options
688687
* @private
689688
* @returns {Promise<Array>} Content of HTTP message body parts
690689
*/
691690
_httpGetMultipartApplicationDicom(
692691
url,
693692
mediaTypes,
694693
params,
695-
progressCallback,
696-
withCredentials,
697694
request
698695
) {
699696
const headers = {};
@@ -730,7 +727,7 @@ class DICOMwebClient {
730727
supportedMediaTypes,
731728
);
732729

733-
return this._httpGet(url, headers, 'arraybuffer', progressCallback, withCredentials, request);
730+
return this._httpGet(url, headers, request);
734731
}
735732

736733
/**
@@ -1758,7 +1755,8 @@ class DICOMwebClient {
17581755
* @param {String} options.studyInstanceUID - Study Instance UID
17591756
* @param {String} options.seriesInstanceUID - Series Instance UID
17601757
* @param {String} options.sopInstanceUID - SOP Instance UID
1761-
* @param {XMLHttpRequest} [options.request] - if specified, the request to use, otherwise one will be created; useful for adding custom upload and abort listeners/objects
1758+
* @param {string[]} options.mediaTypes
1759+
* @param {Request} options.request - if specified, the request to use, otherwise one will be created; useful for adding custom upload and abort listeners/objects
17621760
* @returns {Promise<ArrayBuffer>} DICOM Part 10 file as Arraybuffer
17631761
*/
17641762
retrieveInstance(options) {
@@ -1774,17 +1772,18 @@ class DICOMwebClient {
17741772
const url = `${this.wadoURL}/studies/${options.studyInstanceUID}/series/${options.seriesInstanceUID}/instances/${options.sopInstanceUID}`;
17751773

17761774
const { mediaTypes } = options;
1777-
const { withCredentials = false } = options;
1778-
const { progressCallback = false } = options;
1775+
const {request} = options;
1776+
1777+
if( !request.withCredentials) request.withCredentials = false;
1778+
if( !request.progressCallback) request.progressCallback = false;
1779+
if( !request.responseType) request.responseType = 'arraybuffer';
17791780

17801781
if (!mediaTypes) {
17811782
return this._httpGetMultipartApplicationDicom(
17821783
url,
17831784
false,
17841785
false,
1785-
progressCallback,
1786-
withCredentials,
1787-
options.request
1786+
request
17881787
).then(getFirstResult);
17891788
}
17901789

@@ -1794,9 +1793,7 @@ class DICOMwebClient {
17941793
url,
17951794
mediaTypes,
17961795
false,
1797-
progressCallback,
1798-
withCredentials,
1799-
options.request
1796+
request
18001797
).then(getFirstResult);
18011798
}
18021799

@@ -1971,7 +1968,7 @@ class DICOMwebClient {
19711968
* @param {Object} options
19721969
* @param {ArrayBuffer[]} options.datasets - DICOM Instances in PS3.10 format
19731970
* @param {String} [options.studyInstanceUID] - Study Instance UID
1974-
* @param {XMLHttpRequest} [options.request] - if specified, the request to use, otherwise one will be created; useful for adding custom upload and abort listeners/objects
1971+
* @param {Request} [options.request]
19751972
* @returns {Promise} Response message
19761973
*/
19771974
storeInstances(options) {

0 commit comments

Comments
 (0)