@@ -58,14 +58,16 @@ export class AcquisitionStatus {
5858}
5959
6060export class AcquisitionManager {
61+ private readonly BASE_URL_PART = "appcenter.ms" ;
6162 private _appVersion : string ;
6263 private _clientUniqueId : string ;
6364 private _deploymentKey : string ;
6465 private _httpRequester : Http . Requester ;
6566 private _ignoreAppVersion : boolean ;
6667 private _serverUrl : string ;
6768 private _publicPrefixUrl : string = "v0.1/public/codepush/" ;
68-
69+ private _statusCode : number ;
70+ private static _apiCallsDisabled : boolean = false ;
6971 constructor ( httpRequester : Http . Requester , configuration : Configuration ) {
7072 this . _httpRequester = httpRequester ;
7173
@@ -80,7 +82,21 @@ export class AcquisitionManager {
8082 this . _ignoreAppVersion = configuration . ignoreAppVersion ;
8183 }
8284
85+ private isRecoverable = ( statusCode : number ) : boolean => statusCode >= 500 || statusCode === 408 || statusCode === 429 ;
86+
87+ private handleRequestFailure ( ) {
88+ if ( this . _serverUrl . includes ( this . BASE_URL_PART ) && ! this . isRecoverable ( this . _statusCode ) ) {
89+ AcquisitionManager . _apiCallsDisabled = true ;
90+ }
91+ }
92+
8393 public queryUpdateWithCurrentPackage ( currentPackage : Package , callback ?: Callback < RemotePackage | NativeUpdateNotification > ) : void {
94+ if ( AcquisitionManager . _apiCallsDisabled ) {
95+ console . log ( `[CodePush] Api calls are disabled, skipping API call` ) ;
96+ callback ( /*error=*/ null , /*remotePackage=*/ null ) ;
97+ return ;
98+ }
99+
84100 if ( ! currentPackage || ! currentPackage . appVersion ) {
85101 throw new CodePushPackageError ( "Calling common acquisition SDK with incorrect package" ) ; // Unexpected; indicates error in our implementation
86102 }
@@ -102,8 +118,10 @@ export class AcquisitionManager {
102118 return ;
103119 }
104120
105- if ( response . statusCode !== 200 ) {
121+ if ( response . statusCode < 200 || response . statusCode >= 300 ) {
106122 let errorMessage : any ;
123+ this . _statusCode = response . statusCode ;
124+ this . handleRequestFailure ( ) ;
107125 if ( response . statusCode === 0 ) {
108126 errorMessage = `Couldn't send request to ${ requestUrl } , xhr.statusCode = 0 was returned. One of the possible reasons for that might be connection problems. Please, check your internet connection.` ;
109127 } else {
@@ -147,6 +165,12 @@ export class AcquisitionManager {
147165 }
148166
149167 public reportStatusDeploy ( deployedPackage ?: Package , status ?: string , previousLabelOrAppVersion ?: string , previousDeploymentKey ?: string , callback ?: Callback < void > ) : void {
168+ if ( AcquisitionManager . _apiCallsDisabled ) {
169+ console . log ( `[CodePush] Api calls are disabled, skipping API call` ) ;
170+ callback ( /*error*/ null , /*not used*/ null ) ;
171+ return ;
172+ }
173+
150174 var url : string = this . _serverUrl + this . _publicPrefixUrl + "report_status/deploy" ;
151175 var body : DeploymentStatusReport = {
152176 app_version : this . _appVersion ,
@@ -196,7 +220,9 @@ export class AcquisitionManager {
196220 return ;
197221 }
198222
199- if ( response . statusCode !== 200 ) {
223+ if ( response . statusCode < 200 || response . statusCode >= 300 ) {
224+ this . _statusCode = response . statusCode ;
225+ this . handleRequestFailure ( ) ;
200226 callback ( new CodePushHttpError ( response . statusCode + ": " + response . body ) , /*not used*/ null ) ;
201227 return ;
202228 }
@@ -207,6 +233,12 @@ export class AcquisitionManager {
207233 }
208234
209235 public reportStatusDownload ( downloadedPackage : Package , callback ?: Callback < void > ) : void {
236+ if ( AcquisitionManager . _apiCallsDisabled ) {
237+ console . log ( `[CodePush] Api calls are disabled, skipping API call` ) ;
238+ callback ( /*error*/ null , /*not used*/ null ) ;
239+ return ;
240+ }
241+
210242 var url : string = this . _serverUrl + this . _publicPrefixUrl + "report_status/download" ;
211243 var body : DownloadReport = {
212244 client_unique_id : this . _clientUniqueId ,
@@ -221,7 +253,9 @@ export class AcquisitionManager {
221253 return ;
222254 }
223255
224- if ( response . statusCode !== 200 ) {
256+ if ( response . statusCode < 200 || response . statusCode >= 300 ) {
257+ this . _statusCode = response . statusCode ;
258+ this . handleRequestFailure ( ) ;
225259 callback ( new CodePushHttpError ( response . statusCode + ": " + response . body ) , /*not used*/ null ) ;
226260 return ;
227261 }
0 commit comments