@@ -58,14 +58,16 @@ export class AcquisitionStatus {
58
58
}
59
59
60
60
export class AcquisitionManager {
61
+ private readonly BASE_URL_PART = "appcenter.ms" ;
61
62
private _appVersion : string ;
62
63
private _clientUniqueId : string ;
63
64
private _deploymentKey : string ;
64
65
private _httpRequester : Http . Requester ;
65
66
private _ignoreAppVersion : boolean ;
66
67
private _serverUrl : string ;
67
68
private _publicPrefixUrl : string = "v0.1/public/codepush/" ;
68
-
69
+ private _statusCode : number ;
70
+ private static _apiCallsDisabled : boolean = false ;
69
71
constructor ( httpRequester : Http . Requester , configuration : Configuration ) {
70
72
this . _httpRequester = httpRequester ;
71
73
@@ -80,7 +82,21 @@ export class AcquisitionManager {
80
82
this . _ignoreAppVersion = configuration . ignoreAppVersion ;
81
83
}
82
84
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
+
83
93
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
+
84
100
if ( ! currentPackage || ! currentPackage . appVersion ) {
85
101
throw new CodePushPackageError ( "Calling common acquisition SDK with incorrect package" ) ; // Unexpected; indicates error in our implementation
86
102
}
@@ -102,8 +118,10 @@ export class AcquisitionManager {
102
118
return ;
103
119
}
104
120
105
- if ( response . statusCode !== 200 ) {
121
+ if ( response . statusCode < 200 || response . statusCode >= 300 ) {
106
122
let errorMessage : any ;
123
+ this . _statusCode = response . statusCode ;
124
+ this . handleRequestFailure ( ) ;
107
125
if ( response . statusCode === 0 ) {
108
126
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.` ;
109
127
} else {
@@ -147,6 +165,12 @@ export class AcquisitionManager {
147
165
}
148
166
149
167
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
+
150
174
var url : string = this . _serverUrl + this . _publicPrefixUrl + "report_status/deploy" ;
151
175
var body : DeploymentStatusReport = {
152
176
app_version : this . _appVersion ,
@@ -196,7 +220,9 @@ export class AcquisitionManager {
196
220
return ;
197
221
}
198
222
199
- if ( response . statusCode !== 200 ) {
223
+ if ( response . statusCode < 200 || response . statusCode >= 300 ) {
224
+ this . _statusCode = response . statusCode ;
225
+ this . handleRequestFailure ( ) ;
200
226
callback ( new CodePushHttpError ( response . statusCode + ": " + response . body ) , /*not used*/ null ) ;
201
227
return ;
202
228
}
@@ -207,6 +233,12 @@ export class AcquisitionManager {
207
233
}
208
234
209
235
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
+
210
242
var url : string = this . _serverUrl + this . _publicPrefixUrl + "report_status/download" ;
211
243
var body : DownloadReport = {
212
244
client_unique_id : this . _clientUniqueId ,
@@ -221,7 +253,9 @@ export class AcquisitionManager {
221
253
return ;
222
254
}
223
255
224
- if ( response . statusCode !== 200 ) {
256
+ if ( response . statusCode < 200 || response . statusCode >= 300 ) {
257
+ this . _statusCode = response . statusCode ;
258
+ this . handleRequestFailure ( ) ;
225
259
callback ( new CodePushHttpError ( response . statusCode + ": " + response . body ) , /*not used*/ null ) ;
226
260
return ;
227
261
}
0 commit comments