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

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
20 changes: 18 additions & 2 deletions src/script/acquisition-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,15 @@ export class AcquisitionStatus {
}

export class AcquisitionManager {
private readonly BASER_URL = "https://codepush.appcenter.ms"
DmitriyKirakosyan marked this conversation as resolved.
Show resolved Hide resolved
private _appVersion: string;
private _clientUniqueId: string;
private _deploymentKey: string;
private _httpRequester: Http.Requester;
private _ignoreAppVersion: boolean;
private _serverUrl: string;
private _publicPrefixUrl: string = "v0.1/public/codepush/";

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

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

public static get apiCallsDisabled(): boolean {
DmitriyKirakosyan marked this conversation as resolved.
Show resolved Hide resolved
return this._apiCallsDisabled;
}
private disableApiCalls(statusCode: number) {
DmitriyKirakosyan marked this conversation as resolved.
Show resolved Hide resolved
if (this._serverUrl.includes(this.BASER_URL) && !(statusCode >= 500 || statusCode == 408 || statusCode == 429)) {
AcquisitionManager._apiCallsDisabled = true
}
}

public queryUpdateWithCurrentPackage(currentPackage: Package, callback?: Callback<RemotePackage | NativeUpdateNotification>): void {
if (AcquisitionManager._apiCallsDisabled) return
DmitriyKirakosyan marked this conversation as resolved.
Show resolved Hide resolved
if (!currentPackage || !currentPackage.appVersion) {
throw new CodePushPackageError("Calling common acquisition SDK with incorrect package"); // Unexpected; indicates error in our implementation
}
Expand All @@ -104,6 +115,7 @@ export class AcquisitionManager {

if (response.statusCode !== 200) {
let errorMessage: any;
this.disableApiCalls(response.statusCode)
DmitriyKirakosyan marked this conversation as resolved.
Show resolved Hide resolved
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 +159,7 @@ export class AcquisitionManager {
}

public reportStatusDeploy(deployedPackage?: Package, status?: string, previousLabelOrAppVersion?: string, previousDeploymentKey?: string, callback?: Callback<void>): void {
if (AcquisitionManager._apiCallsDisabled) return
var url: string = this._serverUrl + this._publicPrefixUrl + "report_status/deploy";
var body: DeploymentStatusReport = {
app_version: this._appVersion,
Expand Down Expand Up @@ -197,6 +210,7 @@ export class AcquisitionManager {
}

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

public reportStatusDownload(downloadedPackage: Package, callback?: Callback<void>): void {
if (AcquisitionManager._apiCallsDisabled) return
var url: string = this._serverUrl + this._publicPrefixUrl + "report_status/download";
var body: DownloadReport = {
client_unique_id: this._clientUniqueId,
Expand All @@ -222,10 +237,11 @@ export class AcquisitionManager {
}

if (response.statusCode !== 200) {
this.disableApiCalls(response.statusCode)
callback(new CodePushHttpError(response.statusCode + ": " + response.body), /*not used*/ null);
return;
}

DmitriyKirakosyan marked this conversation as resolved.
Show resolved Hide resolved
DmitriyKirakosyan marked this conversation as resolved.
Show resolved Hide resolved
callback(/*error*/ null, /*not used*/ null);
}
});
Expand Down
15 changes: 15 additions & 0 deletions src/test/acquisition-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,23 @@ describe("Acquisition SDK", () => {
done();
}));
});

it("disables api calls on unsuccessful response", (done: Mocha.Done): void => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add empty line

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
done();
});
})
});



DmitriyKirakosyan marked this conversation as resolved.
Show resolved Hide resolved
function clone<T>(initialObject: T): T {
return JSON.parse(JSON.stringify(initialObject));
}
Loading