@esfaenza/httpservice 11.2.45 → 11.2.47

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.
@@ -3,8 +3,8 @@ import { HttpEventType, HttpHeaders, HttpErrorResponse, HttpClient } from '@angu
3
3
  import { InjectionToken, Injectable, Inject, Optional, NgModule } from '@angular/core';
4
4
  import { CacheService } from '@esfaenza/preferences';
5
5
  import { UtilityService } from '@esfaenza/extensions';
6
- import { delay, finalize, map, catchError, scan } from 'rxjs/operators';
7
- import { forkJoin, of, throwError, Observable } from 'rxjs';
6
+ import { concatMap, delay, finalize, map, catchError, scan } from 'rxjs/operators';
7
+ import { forkJoin, from, of, throwError, Observable } from 'rxjs';
8
8
 
9
9
  /**
10
10
  * Dizionario di invalidazioni delle chiamate HTTP
@@ -232,25 +232,27 @@ class HTTPService {
232
232
  * @returns {Observable<T>} Observable che contiene il risultato della chiamata
233
233
  */
234
234
  get(url, params = null, transaction = "", succesfulResultMandatory = true, errorCallback = null) {
235
- let myGuid = this.defaultStartCall(url);
236
- let cachedInfos = this.tryGetCacheInformations(url, params);
237
- if (cachedInfos.cache && cachedInfos.cacheItem) {
238
- this.log(this.loc("Result for") + " " + url + " " + this.loc("retrieved from cache"));
239
- return of(cachedInfos.cacheItem).pipe(delay(0), finalize(() => { this.tryFinalizeByGuid(myGuid); }));
240
- }
241
- var httpCall = this.http.get(cachedInfos.finalUrl, { headers: this.getHeaders(transaction), params: params });
242
- return httpCall.pipe(map((res) => {
243
- let responseItem = res;
244
- if (cachedInfos.cache) {
245
- this.cacheResponse(this.utisExts.deepClone(responseItem), cachedInfos);
246
- this.log(this.loc("Cached result for") + " " + url);
235
+ // TODO provare e testare
236
+ return from(this.defaultStartCall(url)).pipe(concatMap((myGuid) => {
237
+ let cachedInfos = this.tryGetCacheInformations(url, params);
238
+ if (cachedInfos.cache && cachedInfos.cacheItem) {
239
+ this.log(this.loc("Result for") + " " + url + " " + this.loc("retrieved from cache"));
240
+ return of(cachedInfos.cacheItem).pipe(delay(0), finalize(() => { this.tryFinalizeByGuid(myGuid); }));
247
241
  }
248
- return responseItem;
249
- }), catchError((err, caught) => {
250
- if (errorCallback)
251
- errorCallback();
252
- return !succesfulResultMandatory ? throwError(err) : this.registerErrorAndReturnEmptyObservable(err, myGuid);
253
- }), finalize(() => { this.tryFinalizeByGuid(myGuid, cachedInfos); }));
242
+ var httpCall = this.http.get(cachedInfos.finalUrl, { headers: this.getHeaders(transaction), params: params });
243
+ return httpCall.pipe(map((res) => {
244
+ let responseItem = res;
245
+ if (cachedInfos.cache) {
246
+ this.cacheResponse(this.utisExts.deepClone(responseItem), cachedInfos);
247
+ this.log(this.loc("Cached result for") + " " + url);
248
+ }
249
+ return responseItem;
250
+ }), catchError((err, caught) => {
251
+ if (errorCallback)
252
+ errorCallback();
253
+ return !succesfulResultMandatory ? throwError(err) : this.registerErrorAndReturnEmptyObservable(err, myGuid);
254
+ }), finalize(() => { this.tryFinalizeByGuid(myGuid, cachedInfos); }));
255
+ }));
254
256
  }
255
257
  /**
256
258
  * Esecuzione di una GET HTTP con eventualmente la gestione della cache qualora la chiamata termini con la convenzione "#cache"
@@ -268,24 +270,25 @@ class HTTPService {
268
270
  * @returns {Observable<Blob | ArrayBuffer>} Observable che contiene il risultato della chiamata
269
271
  */
270
272
  getRaw(url, params = null, type = "arraybuffer", transaction = "", succesfulResultMandatory = true, errorCallback = null) {
271
- let myGuid = this.defaultStartCall(url);
272
- let cachedInfos = this.tryGetCacheInformations(url, params);
273
- if (cachedInfos.cache && cachedInfos.cacheItem) {
274
- this.log(this.loc("Result for") + " " + url + " " + this.loc("retrieved from cache"));
275
- return of(cachedInfos.cacheItem).pipe(delay(0), finalize(() => { this.tryFinalizeByGuid(myGuid); }));
276
- }
277
- var httpCall = this.http.get(cachedInfos.finalUrl, { responseType: type, headers: this.getHeaders(transaction), params: params });
278
- return httpCall.pipe(map((blob) => {
279
- if (cachedInfos.cache) {
280
- this.cacheResponse(blob, cachedInfos);
281
- this.log(this.loc("Cached result for") + " " + url);
273
+ return from(this.defaultStartCall(url)).pipe(concatMap((myGuid) => {
274
+ let cachedInfos = this.tryGetCacheInformations(url, params);
275
+ if (cachedInfos.cache && cachedInfos.cacheItem) {
276
+ this.log(this.loc("Result for") + " " + url + " " + this.loc("retrieved from cache"));
277
+ return of(cachedInfos.cacheItem).pipe(delay(0), finalize(() => { this.tryFinalizeByGuid(myGuid); }));
282
278
  }
283
- return blob;
284
- }), catchError((err, caught) => {
285
- if (errorCallback)
286
- errorCallback();
287
- return !succesfulResultMandatory ? throwError(err) : this.registerErrorAndReturnEmptyObservable(err, myGuid);
288
- }), finalize(() => { this.tryFinalizeByGuid(myGuid, cachedInfos); }));
279
+ var httpCall = this.http.get(cachedInfos.finalUrl, { responseType: type, headers: this.getHeaders(transaction), params: params });
280
+ return httpCall.pipe(map((blob) => {
281
+ if (cachedInfos.cache) {
282
+ this.cacheResponse(blob, cachedInfos);
283
+ this.log(this.loc("Cached result for") + " " + url);
284
+ }
285
+ return blob;
286
+ }), catchError((err, caught) => {
287
+ if (errorCallback)
288
+ errorCallback();
289
+ return !succesfulResultMandatory ? throwError(err) : this.registerErrorAndReturnEmptyObservable(err, myGuid);
290
+ }), finalize(() => { this.tryFinalizeByGuid(myGuid, cachedInfos); }));
291
+ }));
289
292
  }
290
293
  /**
291
294
  * Esecuzione di una POST HTTP
@@ -300,14 +303,12 @@ class HTTPService {
300
303
  * @returns {Observable<T>} Observable che contiene il risultato della chiamata
301
304
  */
302
305
  post(url, bodyData, params = null, transaction = "", succesfulResultMandatory = true) {
303
- let myGuid = this.defaultStartCall(url);
304
- var httpCall = this.http.post(url, bodyData, { headers: this.getHeaders(transaction), params: params });
305
- return httpCall.pipe(map((res) => {
306
- let responseItem = res;
307
- return responseItem;
308
- }), catchError((err, caught) => {
309
- return !succesfulResultMandatory ? throwError(err) : this.registerErrorAndReturnEmptyObservable(err, myGuid);
310
- }), finalize(() => { this.tryFinalizeByGuid(myGuid); }));
306
+ return from(this.defaultStartCall(url)).pipe(concatMap((myGuid) => {
307
+ var httpCall = this.http.post(url, bodyData, { headers: this.getHeaders(transaction), params: params });
308
+ return httpCall.pipe(catchError((err, caught) => {
309
+ return !succesfulResultMandatory ? throwError(err) : this.registerErrorAndReturnEmptyObservable(err, myGuid);
310
+ }), finalize(() => { this.tryFinalizeByGuid(myGuid); }));
311
+ }));
311
312
  }
312
313
  /**
313
314
  * Esecuzione di una DELETE HTTP
@@ -321,14 +322,12 @@ class HTTPService {
321
322
  * @returns {Observable<T>} Observable che contiene il risultato della chiamata
322
323
  */
323
324
  delete(url, params = null, transaction = "", succesfulResultMandatory = true) {
324
- let myGuid = this.defaultStartCall(url);
325
- var httpCall = this.http.delete(url, { headers: this.getHeaders(transaction), params: params });
326
- return httpCall.pipe(map((res) => {
327
- let responseItem = res;
328
- return responseItem;
329
- }), catchError((err, caught) => {
330
- return !succesfulResultMandatory ? throwError(err) : this.registerErrorAndReturnEmptyObservable(err, myGuid);
331
- }), finalize(() => { this.tryFinalizeByGuid(myGuid); }));
325
+ return from(this.defaultStartCall(url)).pipe(concatMap((myGuid) => {
326
+ var httpCall = this.http.delete(url, { headers: this.getHeaders(transaction), params: params });
327
+ return httpCall.pipe(catchError((err, caught) => {
328
+ return !succesfulResultMandatory ? throwError(err) : this.registerErrorAndReturnEmptyObservable(err, myGuid);
329
+ }), finalize(() => { this.tryFinalizeByGuid(myGuid); }));
330
+ }));
332
331
  }
333
332
  /**
334
333
  * Esecuzione di una PUT HTTP
@@ -343,14 +342,12 @@ class HTTPService {
343
342
  * @returns {Observable<T>} Observable che contiene il risultato della chiamata
344
343
  */
345
344
  put(url, bodyData, params = null, transaction = "", succesfulResultMandatory = true) {
346
- let myGuid = this.defaultStartCall(url);
347
- var httpCall = this.http.put(url, bodyData, { headers: this.getHeaders(transaction), params: params });
348
- return httpCall.pipe(map((res) => {
349
- let responseItem = res;
350
- return responseItem;
351
- }), catchError((err, caught) => {
352
- return !succesfulResultMandatory ? throwError(err) : this.registerErrorAndReturnEmptyObservable(err, myGuid);
353
- }), finalize(() => { this.tryFinalizeByGuid(myGuid); }));
345
+ return from(this.defaultStartCall(url)).pipe(concatMap((myGuid) => {
346
+ var httpCall = this.http.put(url, bodyData, { headers: this.getHeaders(transaction), params: params });
347
+ return httpCall.pipe(catchError((err, caught) => {
348
+ return !succesfulResultMandatory ? throwError(err) : this.registerErrorAndReturnEmptyObservable(err, myGuid);
349
+ }), finalize(() => { this.tryFinalizeByGuid(myGuid); }));
350
+ }));
354
351
  }
355
352
  /**
356
353
  * Esecuzione di una POST HTTP
@@ -364,11 +361,12 @@ class HTTPService {
364
361
  * @returns {Observable<T>} Observable che contiene il risultato della chiamata
365
362
  */
366
363
  postwithFiles(url, bodyData, fileArrayOrFile) {
367
- let myGuid = this.defaultStartCall(url);
368
- var formData = this.getFilesFormData(fileArrayOrFile);
369
- formData.append("body", JSON.stringify(bodyData));
370
- var httpCall = this.http.post(url, formData, { headers: this.getHeaders(null, true, null, null) });
371
- return httpCall.pipe(catchError((err, caught) => { return this.registerErrorAndReturnEmptyObservable(err, myGuid); }), finalize(() => { this.tryFinalizeByGuid(myGuid); }));
364
+ return from(this.defaultStartCall(url)).pipe(concatMap((myGuid) => {
365
+ var formData = this.getFilesFormData(fileArrayOrFile);
366
+ formData.append("body", JSON.stringify(bodyData));
367
+ var httpCall = this.http.post(url, formData, { headers: this.getHeaders(null, true, null, null) });
368
+ return httpCall.pipe(catchError((err, caught) => { return this.registerErrorAndReturnEmptyObservable(err, myGuid); }), finalize(() => { this.tryFinalizeByGuid(myGuid); }));
369
+ }));
372
370
  }
373
371
  /**
374
372
  * Esecuzione di una POST HTTP
@@ -532,17 +530,20 @@ class HTTPService {
532
530
  */
533
531
  tryFinalizeByGuid(guid, cacheInfos) {
534
532
  return __awaiter(this, void 0, void 0, function* () {
535
- this.log(this.loc("Finalizing call") + " " + guid + " " + this.loc("with status") + ": " + (this.callStatus[guid].success ? this.loc('Success') : this.loc('Failure')));
536
- // Alla prima chiamata buona, significa che almeno il login ce l'ho. Posso mettermi in modalità redirectInProgress false
537
- // e riiniziare a valutare errori di javashit
538
- if (this.callStatus[guid].success && this.ExtensionsImpl.readRedirectionStatus())
539
- this.ExtensionsImpl.writeRedirectionStatus(false);
540
- // Aggiungo la chiamata alla lista delle ultime 3 completate
541
- this.LastThreeCompletedCalls.unshift(this.callStatus[guid].url);
542
- if (this.LastThreeCompletedCalls.length > 3)
543
- this.LastThreeCompletedCalls.pop();
544
- // In ogni caso questa posso rimuoverla
545
- delete this.callStatus[guid];
533
+ this.log(this.loc("Finalizing call") + " " + guid + " " + this.loc("with status") + ": " + (!this.callStatus[guid] ? 'Unknown' : this.callStatus[guid].success ? this.loc('Success') : this.loc('Failure')));
534
+ // TODO: Perdere 3-4 ore per capire perché ogni tanto arriva null in casi pesanti di Observable spammati a random
535
+ if (this.callStatus[guid]) {
536
+ // Alla prima chiamata buona, significa che almeno il login ce l'ho. Posso mettermi in modalità redirectInProgress false
537
+ // e riiniziare a valutare errori di javashit
538
+ if (this.callStatus[guid].success && this.ExtensionsImpl.readRedirectionStatus())
539
+ this.ExtensionsImpl.writeRedirectionStatus(false);
540
+ // Aggiungo la chiamata alla lista delle ultime 3 completate
541
+ this.LastThreeCompletedCalls.unshift(this.callStatus[guid].url);
542
+ if (this.LastThreeCompletedCalls.length > 3)
543
+ this.LastThreeCompletedCalls.pop();
544
+ // In ogni caso questa posso rimuoverla
545
+ delete this.callStatus[guid];
546
+ }
546
547
  // Se vengo da una chiamata che ha aggiornato la cache a timer o globale marco il fatto che quella cache dev'essere aggiornata
547
548
  if (cacheInfos && cacheInfos.cache) {
548
549
  this.PersistTimedCacheASAP = this.PersistTimedCacheASAP || !!cacheInfos.cacheTime;