From 0c05a9fc41f4c65f927331e643faa0b02c3400b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Fri, 7 Feb 2020 15:57:46 +0100 Subject: [PATCH] MOBILE-3339 ionic: Remove usage of angular/http deprecated package --- src/addon/mod/book/providers/book.ts | 33 ++++++------ src/addon/mod/page/providers/helper.ts | 31 +++++------- .../remotethemes/providers/remotethemes.ts | 50 ++++++++----------- src/app/app.module.ts | 2 - src/core/compile/providers/compile.ts | 3 +- src/core/siteplugins/providers/helper.ts | 14 ++---- src/providers/ws.ts | 35 ++++++++++--- 7 files changed, 81 insertions(+), 87 deletions(-) diff --git a/src/addon/mod/book/providers/book.ts b/src/addon/mod/book/providers/book.ts index 6944ea73e..1770eed4a 100644 --- a/src/addon/mod/book/providers/book.ts +++ b/src/addon/mod/book/providers/book.ts @@ -13,7 +13,6 @@ // limitations under the License. import { Injectable } from '@angular/core'; -import { Http, Response } from '@angular/http'; import { CoreFileProvider } from '@providers/file'; import { CoreFilepoolProvider } from '@providers/filepool'; import { CoreLoggerProvider } from '@providers/logger'; @@ -25,7 +24,7 @@ import { CoreCourseProvider } from '@core/course/providers/course'; import { CoreCourseLogHelperProvider } from '@core/course/providers/log-helper'; import { CoreSite } from '@classes/site'; import { CoreTagItem } from '@core/tag/providers/tag'; -import { CoreWSExternalWarning, CoreWSExternalFile } from '@providers/ws'; +import { CoreWSProvider, CoreWSExternalWarning, CoreWSExternalFile } from '@providers/ws'; /** * Service that provides some features for books. @@ -37,10 +36,16 @@ export class AddonModBookProvider { protected ROOT_CACHE_KEY = 'mmaModBook:'; protected logger; - constructor(logger: CoreLoggerProvider, private sitesProvider: CoreSitesProvider, private textUtils: CoreTextUtilsProvider, - private fileProvider: CoreFileProvider, private filepoolProvider: CoreFilepoolProvider, private http: Http, - private utils: CoreUtilsProvider, private courseProvider: CoreCourseProvider, private domUtils: CoreDomUtilsProvider, - private logHelper: CoreCourseLogHelperProvider) { + constructor(logger: CoreLoggerProvider, + protected sitesProvider: CoreSitesProvider, + protected textUtils: CoreTextUtilsProvider, + protected fileProvider: CoreFileProvider, + protected filepoolProvider: CoreFilepoolProvider, + protected wsProvider: CoreWSProvider, + protected utils: CoreUtilsProvider, + protected courseProvider: CoreCourseProvider, + protected domUtils: CoreDomUtilsProvider, + protected logHelper: CoreCourseLogHelperProvider) { this.logger = logger.getInstance('AddonModBookProvider'); } @@ -128,19 +133,11 @@ export class AddonModBookProvider { return this.sitesProvider.getCurrentSite().checkAndFixPluginfileURL(indexUrl); } - return promise.then((url) => { - // Fetch the URL content. - const promise = this.http.get(url).toPromise(); + return promise.then(async (url) => { + const content = await this.wsProvider.getText(url); - return promise.then((response: Response): any => { - const content = response.text(); - if (typeof content !== 'string') { - return Promise.reject(null); - } else { - // Now that we have the content, we update the SRC to point back to the external resource. - return this.domUtils.restoreSourcesInHtml(content, contentsMap[chapterId].paths); - } - }); + // Now that we have the content, we update the SRC to point back to the external resource. + return this.domUtils.restoreSourcesInHtml(content, contentsMap[chapterId].paths); }); } diff --git a/src/addon/mod/page/providers/helper.ts b/src/addon/mod/page/providers/helper.ts index 43887b52a..d95ff5a3b 100644 --- a/src/addon/mod/page/providers/helper.ts +++ b/src/addon/mod/page/providers/helper.ts @@ -20,7 +20,7 @@ import { CoreFilepoolProvider } from '@providers/filepool'; import { AddonModPageProvider } from './page'; import { CoreFileProvider } from '@providers/file'; import { CoreSitesProvider } from '@providers/sites'; -import { Http, Response } from '@angular/http'; +import { CoreWSProvider } from '@providers/ws'; /** * Service that provides some features for page. @@ -30,9 +30,13 @@ export class AddonModPageHelperProvider { protected logger; - constructor(logger: CoreLoggerProvider, private domUtils: CoreDomUtilsProvider, private filepoolProvider: CoreFilepoolProvider, - private fileProvider: CoreFileProvider, private textUtils: CoreTextUtilsProvider, private http: Http, - private sitesProvider: CoreSitesProvider) { + constructor(logger: CoreLoggerProvider, + protected domUtils: CoreDomUtilsProvider, + protected filepoolProvider: CoreFilepoolProvider, + protected fileProvider: CoreFileProvider, + protected textUtils: CoreTextUtilsProvider, + protected wsProvider: CoreWSProvider, + protected sitesProvider: CoreSitesProvider) { this.logger = logger.getInstance('AddonModPageHelperProvider'); } @@ -79,21 +83,12 @@ export class AddonModPageHelperProvider { promise = this.sitesProvider.getCurrentSite().checkAndFixPluginfileURL(indexUrl); } - return promise.then((url) => { + return promise.then(async (url) => { + const content = await this.wsProvider.getText(url); - // Fetch the URL content. - const promise = this.http.get(url).toPromise(); - - return promise.then((response: Response): any => { - const content = response.text(); - if (typeof content !== 'string') { - return Promise.reject(null); - } - - // Now that we have the content, we update the SRC to point back to the external resource. - // That will be caught by core-format-text. - return this.domUtils.restoreSourcesInHtml(content, paths); - }); + // Now that we have the content, we update the SRC to point back to the external resource. + // That will be caught by core-format-text. + return this.domUtils.restoreSourcesInHtml(content, paths); }); } diff --git a/src/addon/remotethemes/providers/remotethemes.ts b/src/addon/remotethemes/providers/remotethemes.ts index cba1cafe3..7af82922f 100644 --- a/src/addon/remotethemes/providers/remotethemes.ts +++ b/src/addon/remotethemes/providers/remotethemes.ts @@ -13,7 +13,7 @@ // limitations under the License. import { Injectable } from '@angular/core'; -import { Http } from '@angular/http'; +import { CoreWSProvider } from '@providers/ws'; import { CoreAppProvider } from '@providers/app'; import { CoreFileProvider } from '@providers/file'; import { CoreFilepoolProvider } from '@providers/filepool'; @@ -34,9 +34,13 @@ export class AddonRemoteThemesProvider { protected logger; protected stylesEls: {[siteId: string]: {element: HTMLStyleElement, hash: string}} = {}; - constructor(logger: CoreLoggerProvider, private sitesProvider: CoreSitesProvider, private fileProvider: CoreFileProvider, - private filepoolProvider: CoreFilepoolProvider, private http: Http, private utils: CoreUtilsProvider, - private appProvider: CoreAppProvider) { + constructor(logger: CoreLoggerProvider, + protected sitesProvider: CoreSitesProvider, + protected fileProvider: CoreFileProvider, + protected filepoolProvider: CoreFilepoolProvider, + protected wsProvider: CoreWSProvider, + protected utils: CoreUtilsProvider, + protected appProvider: CoreAppProvider) { this.logger = logger.getInstance('AddonRemoteThemesProvider'); } @@ -174,18 +178,13 @@ export class AddonRemoteThemesProvider { return; } - return promise.then((url) => { + return promise.then(async (url) => { this.logger.debug('Loading styles from: ', url); // Get the CSS content using HTTP because we will treat the styles before saving them in the file. - return this.http.get(url).toPromise(); - }).then((response): any => { - const text = response && response.text(); - if (typeof text == 'string') { - return {fileUrl: fileUrl, styles: this.get35Styles(text)}; - } else { - return Promise.reject(null); - } + const text = await this.wsProvider.getText(url); + + return {fileUrl: fileUrl, styles: this.get35Styles(text)}; }); }); } @@ -263,23 +262,18 @@ export class AddonRemoteThemesProvider { return Promise.resolve(); } - return this.http.get(url).toPromise().then((response) => { - let text = response && response.text(); - if (typeof text == 'string') { - text = this.get35Styles(text); + return this.wsProvider.getText(url).then((text) => { + text = this.get35Styles(text); - const styleEl = document.createElement('style'); - styleEl.setAttribute('id', 'mobilecssurl-tmpsite'); - styleEl.innerHTML = text; + const styleEl = document.createElement('style'); + styleEl.setAttribute('id', 'mobilecssurl-tmpsite'); + styleEl.innerHTML = text; - document.head.appendChild(styleEl); - this.stylesEls.tmpsite = { - element: styleEl, - hash: '' - }; - } else { - return Promise.reject(null); - } + document.head.appendChild(styleEl); + this.stylesEls.tmpsite = { + element: styleEl, + hash: '' + }; }); } diff --git a/src/app/app.module.ts b/src/app/app.module.ts index cd4d4e168..38fff7b5d 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -17,7 +17,6 @@ import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { NgModule, COMPILER_OPTIONS } from '@angular/core'; import { IonicApp, IonicModule, Platform, Content, ScrollEvent, Config, Refresher } from 'ionic-angular'; import { assert } from 'ionic-angular/util/util'; -import { HttpModule } from '@angular/http'; import { HttpClient, HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http'; import { JitCompilerFactory } from '@angular/platform-browser-dynamic'; import { LocationStrategy } from '@angular/common'; @@ -202,7 +201,6 @@ export const WP_PROVIDER: any = null; BrowserModule, BrowserAnimationsModule, HttpClientModule, // HttpClient is used to make JSON requests. It fails for HEAD requests because there is no content. - HttpModule, IonicModule.forRoot(MoodleMobileApp, { pageTransition: 'core-page-transition' }), diff --git a/src/core/compile/providers/compile.ts b/src/core/compile/providers/compile.ts index 74596aa20..8ad107f6a 100644 --- a/src/core/compile/providers/compile.ts +++ b/src/core/compile/providers/compile.ts @@ -46,7 +46,6 @@ import { CoreSitePluginsProvider } from '@core/siteplugins/providers/siteplugins // Import other libraries and providers. import { DomSanitizer } from '@angular/platform-browser'; import { FormBuilder, Validators } from '@angular/forms'; -import { Http } from '@angular/http'; import { HttpClient } from '@angular/common/http'; import { CoreConfigConstants } from '../../../configconstants'; import { CoreConstants } from '@core/constants'; @@ -136,7 +135,7 @@ export class CoreCompileProvider { // Other Ionic/Angular providers that don't depend on where they are injected. protected OTHER_PROVIDERS = [ - TranslateService, Http, HttpClient, Platform, DomSanitizer, ActionSheetController, AlertController, LoadingController, + TranslateService, HttpClient, Platform, DomSanitizer, ActionSheetController, AlertController, LoadingController, ModalController, PopoverController, ToastController, FormBuilder ]; diff --git a/src/core/siteplugins/providers/helper.ts b/src/core/siteplugins/providers/helper.ts index 170ccf2aa..caa617a9e 100644 --- a/src/core/siteplugins/providers/helper.ts +++ b/src/core/siteplugins/providers/helper.ts @@ -13,7 +13,6 @@ // limitations under the License. import { Injectable } from '@angular/core'; -import { Http } from '@angular/http'; import { TranslateService } from '@ngx-translate/core'; import { CoreAppProvider } from '@providers/app'; import { CoreEventsProvider } from '@providers/events'; @@ -33,6 +32,7 @@ import { CoreCourseProvider } from '@core/course/providers/course'; import { CoreCoursesProvider } from '@core/courses/providers/courses'; import { CoreFilterHelperProvider } from '@core/filter/providers/helper'; import { CorePluginFileDelegate } from '@providers/plugin-file-delegate'; +import { CoreWSProvider } from '@providers/ws'; // Delegates import { CoreMainMenuDelegate } from '@core/mainmenu/providers/delegate'; @@ -93,7 +93,7 @@ export class CoreSitePluginsHelperProvider { private moduleDelegate: CoreCourseModuleDelegate, private userDelegate: CoreUserDelegate, private langProvider: CoreLangProvider, - private http: Http, + private wsProvider: CoreWSProvider, private sitePluginsProvider: CoreSitePluginsProvider, private prefetchDelegate: CoreCourseModulePrefetchDelegate, private compileProvider: CoreCompileProvider, @@ -208,15 +208,7 @@ export class CoreSitePluginsHelperProvider { undefined, undefined, undefined, handlerSchema.styles.version).then((url) => { // File is downloaded, get the contents. - return this.http.get(url).toPromise(); - }).then((response): any => { - const text = response && response.text(); - - if (typeof text == 'string') { - return text; - } else { - return Promise.reject(null); - } + return this.wsProvider.getText(url); }); }); } diff --git a/src/providers/ws.ts b/src/providers/ws.ts index cf8aa1ef8..ce0fd8997 100644 --- a/src/providers/ws.ts +++ b/src/providers/ws.ts @@ -13,8 +13,7 @@ // limitations under the License. import { Injectable } from '@angular/core'; -import { Http } from '@angular/http'; -import { HttpClient } from '@angular/common/http'; +import { HttpClient, HttpResponse } from '@angular/common/http'; import { TranslateService } from '@ngx-translate/core'; import { FileTransfer, FileUploadOptions } from '@ionic-native/file-transfer'; import { CoreAppProvider } from './app'; @@ -92,10 +91,14 @@ export class CoreWSProvider { protected retryCalls = []; protected retryTimeout = 0; - constructor(private http: HttpClient, private translate: TranslateService, private appProvider: CoreAppProvider, - private textUtils: CoreTextUtilsProvider, logger: CoreLoggerProvider, - private fileProvider: CoreFileProvider, private fileTransfer: FileTransfer, private commonHttp: Http, - private mimeUtils: CoreMimetypeUtilsProvider) { + constructor(protected http: HttpClient, + protected translate: TranslateService, + protected appProvider: CoreAppProvider, + protected textUtils: CoreTextUtilsProvider, + protected fileProvider: CoreFileProvider, + protected fileTransfer: FileTransfer, + protected mimeUtils: CoreMimetypeUtilsProvider, + logger: CoreLoggerProvider) { this.logger = logger.getInstance('CoreWSProvider'); } @@ -499,11 +502,11 @@ export class CoreWSProvider { * @param url URL to perform the request. * @return Promise resolved with the response. */ - performHead(url: string): Promise { + performHead(url: string): Promise> { let promise = this.getPromiseHttp('head', url); if (!promise) { - promise = this.commonHttp.head(url).timeout(this.getRequestTimeout()).toPromise(); + promise = this.http.head(url, {observe: 'response'}).timeout(this.getRequestTimeout()).toPromise(); promise = this.setPromiseHttp(promise, 'head', url); } @@ -828,6 +831,22 @@ export class CoreWSProvider { return Promise.reject(this.translate.instant('core.errorinvalidresponse')); }); } + + /** + * Perform an HTTP request requesting for a text response. + * + * @param url Url to get. + * @return Resolved with the text when done. + */ + async getText(url: string): Promise { + // Fetch the URL content. + const content = await this.http.get(url, { responseType: 'text' }).toPromise(); + if (typeof content !== 'string') { + return Promise.reject(null); + } + + return content; + } } /**