Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 9baedae

Browse files
Refactor and reformat code
1 parent c3e1c78 commit 9baedae

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

src/script/acquisition-sdk.ts

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ export class AcquisitionStatus {
5858
}
5959

6060
export class AcquisitionManager {
61-
private readonly BASER_URL = "https://codepush.appcenter.ms"
61+
private readonly BASER_URL_PART = "appcenter.ms"
6262
private _appVersion: string;
6363
private _clientUniqueId: string;
6464
private _deploymentKey: string;
6565
private _httpRequester: Http.Requester;
6666
private _ignoreAppVersion: boolean;
6767
private _serverUrl: string;
6868
private _publicPrefixUrl: string = "v0.1/public/codepush/";
69-
private static _apiCallsDisabled: boolean = false;
69+
private static _apiCallsDisabled: boolean = false;
7070
constructor(httpRequester: Http.Requester, configuration: Configuration) {
7171
this._httpRequester = httpRequester;
7272

@@ -80,18 +80,23 @@ export class AcquisitionManager {
8080
this._deploymentKey = configuration.deploymentKey;
8181
this._ignoreAppVersion = configuration.ignoreAppVersion;
8282
}
83-
83+
// Used for Tests
8484
public static get apiCallsDisabled(): boolean {
8585
return this._apiCallsDisabled;
8686
}
87-
private disableApiCalls(statusCode: number) {
88-
if (this._serverUrl.includes(this.BASER_URL) && !(statusCode >= 500 || statusCode == 408 || statusCode == 429)) {
89-
AcquisitionManager._apiCallsDisabled = true
87+
88+
private handleRequestFailure(statusCode: number) {
89+
if (this._serverUrl.includes(this.BASER_URL_PART) && !(statusCode >= 500 || statusCode == 408 || statusCode == 429)) {
90+
AcquisitionManager._apiCallsDisabled = true;
9091
}
9192
}
9293

9394
public queryUpdateWithCurrentPackage(currentPackage: Package, callback?: Callback<RemotePackage | NativeUpdateNotification>): void {
94-
if (AcquisitionManager._apiCallsDisabled) return
95+
if (AcquisitionManager._apiCallsDisabled) {
96+
console.log(`[CodePush] Api calls are disabled, skipping queryUpdateWithCurrentPackage`);
97+
return;
98+
}
99+
95100
if (!currentPackage || !currentPackage.appVersion) {
96101
throw new CodePushPackageError("Calling common acquisition SDK with incorrect package"); // Unexpected; indicates error in our implementation
97102
}
@@ -115,7 +120,7 @@ export class AcquisitionManager {
115120

116121
if (response.statusCode !== 200) {
117122
let errorMessage: any;
118-
this.disableApiCalls(response.statusCode)
123+
this.handleRequestFailure(response.statusCode)
119124
if (response.statusCode === 0) {
120125
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.`;
121126
} else {
@@ -159,7 +164,11 @@ export class AcquisitionManager {
159164
}
160165

161166
public reportStatusDeploy(deployedPackage?: Package, status?: string, previousLabelOrAppVersion?: string, previousDeploymentKey?: string, callback?: Callback<void>): void {
162-
if (AcquisitionManager._apiCallsDisabled) return
167+
if (AcquisitionManager._apiCallsDisabled) {
168+
console.log(`[CodePush] Api calls are disabled, skipping queryUpdateWithCurrentPackage`);
169+
return;
170+
}
171+
163172
var url: string = this._serverUrl + this._publicPrefixUrl + "report_status/deploy";
164173
var body: DeploymentStatusReport = {
165174
app_version: this._appVersion,
@@ -210,7 +219,7 @@ export class AcquisitionManager {
210219
}
211220

212221
if (response.statusCode !== 200) {
213-
this.disableApiCalls(response.statusCode)
222+
this.handleRequestFailure(response.statusCode)
214223
callback(new CodePushHttpError(response.statusCode + ": " + response.body), /*not used*/ null);
215224
return;
216225
}
@@ -221,7 +230,11 @@ export class AcquisitionManager {
221230
}
222231

223232
public reportStatusDownload(downloadedPackage: Package, callback?: Callback<void>): void {
224-
if (AcquisitionManager._apiCallsDisabled) return
233+
if (AcquisitionManager._apiCallsDisabled) {
234+
console.log(`[CodePush] Api calls are disabled, skipping queryUpdateWithCurrentPackage`);
235+
return;
236+
}
237+
225238
var url: string = this._serverUrl + this._publicPrefixUrl + "report_status/download";
226239
var body: DownloadReport = {
227240
client_unique_id: this._clientUniqueId,
@@ -237,11 +250,10 @@ export class AcquisitionManager {
237250
}
238251

239252
if (response.statusCode !== 200) {
240-
this.disableApiCalls(response.statusCode)
253+
this.handleRequestFailure(response.statusCode)
241254
callback(new CodePushHttpError(response.statusCode + ": " + response.body), /*not used*/ null);
242255
return;
243256
}
244-
245257
callback(/*error*/ null, /*not used*/ null);
246258
}
247259
});

src/test/acquisition-sdk.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,31 @@ describe("Acquisition SDK", () => {
234234
};
235235
configuration = { ...configuration, serverUrl: "https://codepush.appcenter.ms" }
236236
var acquisition = new acquisitionSdk.AcquisitionManager(new mockApi.CustomResponseHttpRequester(invalidJsonResponse), configuration);
237+
237238
acquisition.queryUpdateWithCurrentPackage(templateCurrentPackage, (error: Error, returnPackage: acquisitionSdk.RemotePackage | acquisitionSdk.NativeUpdateNotification) => {
238239
assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, true);
239240
done();
240241
});
242+
243+
acquisition.queryUpdateWithCurrentPackage(templateCurrentPackage, (error: Error, returnPackage: acquisitionSdk.RemotePackage | acquisitionSdk.NativeUpdateNotification) => {
244+
assert.strictEqual(returnPackage, undefined);
245+
done();
246+
})
241247
})
242-
});
243248

249+
it("doesn't disable api calls on successful response", (done: Mocha.Done): void => {
250+
var acquisition = new acquisitionSdk.AcquisitionManager(new mockApi.HttpRequester(), configuration);
251+
252+
acquisition.reportStatusDeploy(templateCurrentPackage, acquisitionSdk.AcquisitionStatus.DeploymentSucceeded, "1.5.0", mockApi.validDeploymentKey, ((error: Error, parameter: void): void => {
253+
assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, false);
254+
}))
244255

256+
acquisition.reportStatusDownload(templateCurrentPackage, ((error: Error, parameter: void): void => {
257+
assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, false);
258+
done();
259+
}));
260+
})
261+
});
245262

246263
function clone<T>(initialObject: T): T {
247264
return JSON.parse(JSON.stringify(initialObject));

0 commit comments

Comments
 (0)