Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable api calls #851

Merged
merged 13 commits into from
Dec 16, 2024
34 changes: 34 additions & 0 deletions src/script/acquisition-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export class AcquisitionStatus {
}

export class AcquisitionManager {
private readonly BASER_URL_PART = "appcenter.ms";
DmitriyKirakosyan marked this conversation as resolved.
Show resolved Hide resolved
private _appVersion: string;
private _clientUniqueId: string;
private _deploymentKey: string;
Expand All @@ -66,6 +67,7 @@ export class AcquisitionManager {
private _serverUrl: string;
private _publicPrefixUrl: string = "v0.1/public/codepush/";

private static _apiCallsDisabled: boolean = false;
constructor(httpRequester: Http.Requester, configuration: Configuration) {
DordeDimitrijev marked this conversation as resolved.
Show resolved Hide resolved
this._httpRequester = httpRequester;

Expand All @@ -80,7 +82,24 @@ export class AcquisitionManager {
this._ignoreAppVersion = configuration.ignoreAppVersion;
}

// Used for Tests
DmitriyKirakosyan marked this conversation as resolved.
Show resolved Hide resolved
public static get apiCallsDisabled(): boolean {
DmitriyKirakosyan marked this conversation as resolved.
Show resolved Hide resolved
return this._apiCallsDisabled;
}

private handleRequestFailure(statusCode: number) {
if (this._serverUrl.includes(this.BASER_URL_PART) && !(statusCode >= 500 || statusCode == 408 || statusCode == 429)) {
DmitriyKirakosyan marked this conversation as resolved.
Show resolved Hide resolved
AcquisitionManager._apiCallsDisabled = true;
}
}

public queryUpdateWithCurrentPackage(currentPackage: Package, callback?: Callback<RemotePackage | NativeUpdateNotification>): void {
if (AcquisitionManager._apiCallsDisabled) {
console.log(`[CodePush] Api calls are disabled, skipping API call`);
callback(/*error=*/ null, /*remotePackage=*/ null);
return;
}

if (!currentPackage || !currentPackage.appVersion) {
throw new CodePushPackageError("Calling common acquisition SDK with incorrect package"); // Unexpected; indicates error in our implementation
}
Expand All @@ -104,6 +123,7 @@ export class AcquisitionManager {

if (response.statusCode !== 200) {
let errorMessage: any;
this.handleRequestFailure(response.statusCode)
if (response.statusCode === 0) {
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.`;
} else {
Expand Down Expand Up @@ -147,6 +167,12 @@ export class AcquisitionManager {
}

public reportStatusDeploy(deployedPackage?: Package, status?: string, previousLabelOrAppVersion?: string, previousDeploymentKey?: string, callback?: Callback<void>): void {
if (AcquisitionManager._apiCallsDisabled) {
console.log(`[CodePush] Api calls are disabled, skipping API call`);
callback(null, null)
return;
DmitriyKirakosyan marked this conversation as resolved.
Show resolved Hide resolved
}

var url: string = this._serverUrl + this._publicPrefixUrl + "report_status/deploy";
var body: DeploymentStatusReport = {
app_version: this._appVersion,
Expand Down Expand Up @@ -197,6 +223,7 @@ export class AcquisitionManager {
}

if (response.statusCode !== 200) {
this.handleRequestFailure(response.statusCode)
callback(new CodePushHttpError(response.statusCode + ": " + response.body), /*not used*/ null);
return;
}
Expand All @@ -207,6 +234,12 @@ export class AcquisitionManager {
}

public reportStatusDownload(downloadedPackage: Package, callback?: Callback<void>): void {
if (AcquisitionManager._apiCallsDisabled) {
console.log(`[CodePush] Api calls are disabled, skipping API call`);
callback(null, null)
return;
}

var url: string = this._serverUrl + this._publicPrefixUrl + "report_status/download";
var body: DownloadReport = {
client_unique_id: this._clientUniqueId,
Expand All @@ -222,6 +255,7 @@ export class AcquisitionManager {
}

if (response.statusCode !== 200) {
this.handleRequestFailure(response.statusCode)
callback(new CodePushHttpError(response.statusCode + ": " + response.body), /*not used*/ null);
return;
}
Expand Down
47 changes: 47 additions & 0 deletions src/test/acquisition-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,53 @@ describe("Acquisition SDK", () => {
done();
}));
});

it("doesnt disable api calls on successful response", (done: Mocha.Done): void => {
var acquisition = new acquisitionSdk.AcquisitionManager(new mockApi.HttpRequester(), configuration);

acquisition.queryUpdateWithCurrentPackage(templateCurrentPackage, (error: Error, returnPackage: acquisitionSdk.RemotePackage | acquisitionSdk.NativeUpdateNotification) => {
assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, false);
});

acquisition.queryUpdateWithCurrentPackage(templateCurrentPackage, (error: Error, returnPackage: acquisitionSdk.RemotePackage | acquisitionSdk.NativeUpdateNotification) => {
assert.notStrictEqual(returnPackage, null)
DmitriyKirakosyan marked this conversation as resolved.
Show resolved Hide resolved
});

acquisition.reportStatusDeploy(templateCurrentPackage, acquisitionSdk.AcquisitionStatus.DeploymentSucceeded, "1.5.0", mockApi.validDeploymentKey, ((error: Error, parameter: void): void => {
assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, false);
}))

acquisition.reportStatusDownload(templateCurrentPackage, ((error: Error, parameter: void): void => {
DordeDimitrijev marked this conversation as resolved.
Show resolved Hide resolved
assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, false);
}));
done();
})

it("disables api calls on unsuccessful response", (done: Mocha.Done): void => {
var invalidJsonResponse: acquisitionSdk.Http.Response = {
statusCode: 404,
body: "Not found"
};
configuration = { ...configuration, serverUrl: "https://codepush.appcenter.ms" }
var acquisition = new acquisitionSdk.AcquisitionManager(new mockApi.CustomResponseHttpRequester(invalidJsonResponse), configuration);

acquisition.queryUpdateWithCurrentPackage(templateCurrentPackage, (error: Error, returnPackage: acquisitionSdk.RemotePackage | acquisitionSdk.NativeUpdateNotification) => {
DmitriyKirakosyan marked this conversation as resolved.
Show resolved Hide resolved
assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, true);
DmitriyKirakosyan marked this conversation as resolved.
Show resolved Hide resolved
});

acquisition.reportStatusDeploy(templateCurrentPackage, acquisitionSdk.AcquisitionStatus.DeploymentSucceeded, "1.5.0", mockApi.validDeploymentKey, ((error: Error, parameter: void): void => {
DordeDimitrijev marked this conversation as resolved.
Show resolved Hide resolved
assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, true);
}))
acquisition.reportStatusDownload(templateCurrentPackage, ((error: Error, parameter: void): void => {
assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, true);
}));

acquisition.queryUpdateWithCurrentPackage(templateCurrentPackage, (error: Error, returnPackage: acquisitionSdk.RemotePackage | acquisitionSdk.NativeUpdateNotification) => {
assert.strictEqual(returnPackage, null);
})
done();
})

});

DmitriyKirakosyan marked this conversation as resolved.
Show resolved Hide resolved
function clone<T>(initialObject: T): T {
Expand Down
Loading