Skip to content

Commit 25a1cb9

Browse files
authored
Merge pull request #22 from dgraph-io/aelbannan/combine-health
Removed redundant health() function
2 parents 7f473cf + 858b0d1 commit 25a1cb9

File tree

4 files changed

+81
-117
lines changed

4 files changed

+81
-117
lines changed

lib/clientStub.d.ts

-6
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@ export declare class DgraphClientStub {
1414
mutate(mu: Mutation): Promise<Assigned>;
1515
commit(ctx: TxnContext): Promise<TxnContext>;
1616
abort(ctx: TxnContext): Promise<TxnContext>;
17-
health(): Promise<{
18-
health: string;
19-
version: string;
20-
instance?: string;
21-
uptime?: number;
22-
}>;
2317
login(userid?: string, password?: string, refreshToken?: string): Promise<boolean>;
2418
logout(): void;
2519
getAuthTokens(): {

lib/clientStub.js

+32-66
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,4 @@
11
"use strict";
2-
var __assign = (this && this.__assign) || function () {
3-
__assign = Object.assign || function(t) {
4-
for (var s, i = 1, n = arguments.length; i < n; i++) {
5-
s = arguments[i];
6-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7-
t[p] = s[p];
8-
}
9-
return t;
10-
};
11-
return __assign.apply(this, arguments);
12-
};
132
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
143
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
154
return new (P || (P = Promise))(function (resolve, reject) {
@@ -63,14 +52,25 @@ var DgraphClientStub = (function () {
6352
}
6453
DgraphClientStub.prototype.detectApiVersion = function () {
6554
return __awaiter(this, void 0, void 0, function () {
66-
var health;
55+
var response, err_1;
6756
return __generator(this, function (_a) {
6857
switch (_a.label) {
69-
case 0: return [4, this.health()];
58+
case 0:
59+
_a.trys.push([0, 2, , 3]);
60+
return [4, this.getHealth()];
7061
case 1:
71-
health = _a.sent();
72-
this.legacyApi = health.version.startsWith("1.0.");
73-
return [2, health.version];
62+
response = _a.sent();
63+
return [2, response.data["version"]];
64+
case 2:
65+
err_1 = _a.sent();
66+
if (err_1.name === "FetchError") {
67+
if (err_1.response.status === 200) {
68+
this.legacyApi = true;
69+
return [2, "1.0.x"];
70+
}
71+
}
72+
throw new Error("Failed to obtain alpha health.");
73+
case 3: return [2];
7474
}
7575
});
7676
});
@@ -249,33 +249,6 @@ var DgraphClientStub = (function () {
249249
: "/abort/" + ctx.start_ts;
250250
return this.callAPI(url, { method: "POST" });
251251
};
252-
DgraphClientStub.prototype.health = function () {
253-
return __awaiter(this, void 0, void 0, function () {
254-
var response, text;
255-
return __generator(this, function (_a) {
256-
switch (_a.label) {
257-
case 0: return [4, fetch(this.getURL("health"), {
258-
method: "GET",
259-
})];
260-
case 1:
261-
response = _a.sent();
262-
if (response.status >= 300 || response.status < 200) {
263-
throw new Error("Invalid status code = " + response.status);
264-
}
265-
return [4, response.text()];
266-
case 2:
267-
text = _a.sent();
268-
if (text === "OK") {
269-
return [2, {
270-
health: text,
271-
version: "1.0.x",
272-
}];
273-
}
274-
return [2, __assign(__assign({}, JSON.parse(text)), { health: "OK" })];
275-
}
276-
});
277-
});
278-
};
279252
DgraphClientStub.prototype.login = function (userid, password, refreshToken) {
280253
return __awaiter(this, void 0, void 0, function () {
281254
var body, res;
@@ -329,30 +302,14 @@ var DgraphClientStub = (function () {
329302
};
330303
DgraphClientStub.prototype.getHealth = function (all) {
331304
if (all === void 0) { all = false; }
332-
return __awaiter(this, void 0, void 0, function () {
333-
var url;
334-
return __generator(this, function (_a) {
335-
switch (_a.label) {
336-
case 0:
337-
url = "health" + (all ? "?all" : "");
338-
return [4, this.callAPI(url, {
339-
method: "GET",
340-
})];
341-
case 1: return [2, _a.sent()];
342-
}
343-
});
305+
var url = "health" + (all ? "?all" : "");
306+
return this.callAPI(url, {
307+
method: "GET",
344308
});
345309
};
346310
DgraphClientStub.prototype.getState = function () {
347-
return __awaiter(this, void 0, void 0, function () {
348-
return __generator(this, function (_a) {
349-
switch (_a.label) {
350-
case 0: return [4, this.callAPI("state", {
351-
method: "GET",
352-
})];
353-
case 1: return [2, _a.sent()];
354-
}
355-
});
311+
return this.callAPI("state", {
312+
method: "GET",
356313
});
357314
};
358315
DgraphClientStub.prototype.setAutoRefresh = function (val) {
@@ -380,7 +337,7 @@ var DgraphClientStub = (function () {
380337
};
381338
DgraphClientStub.prototype.callAPI = function (path, config) {
382339
return __awaiter(this, void 0, void 0, function () {
383-
var url, response, json, errors;
340+
var url, response, json, err_2, errors;
384341
return __generator(this, function (_a) {
385342
switch (_a.label) {
386343
case 0:
@@ -395,9 +352,18 @@ var DgraphClientStub = (function () {
395352
if (response.status >= 300 || response.status < 200) {
396353
throw new Error("Invalid status code = " + response.status);
397354
}
398-
return [4, response.json()];
355+
_a.label = 2;
399356
case 2:
357+
_a.trys.push([2, 4, , 5]);
358+
return [4, response.json()];
359+
case 3:
400360
json = _a.sent();
361+
return [3, 5];
362+
case 4:
363+
err_2 = _a.sent();
364+
err_2.response = response;
365+
throw err_2;
366+
case 5:
401367
errors = json.errors;
402368
if (errors !== undefined) {
403369
throw new errors_1.APIError(url, errors);

lib/zero.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,12 @@ var DgraphZero = (function () {
6161
return __awaiter(this, void 0, void 0, function () {
6262
var url;
6363
return __generator(this, function (_a) {
64-
url = "health" + (all ? "?all" : "");
65-
return [2, this.callAPI(url, {})];
64+
switch (_a.label) {
65+
case 0:
66+
url = "health" + (all ? "?all" : "");
67+
return [4, this.callAPI(url, {})];
68+
case 1: return [2, _a.sent()];
69+
}
6670
});
6771
});
6872
};

src/clientStub.ts

+43-43
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,21 @@ export class DgraphClientStub {
4040
}
4141

4242
public async detectApiVersion(): Promise<string> {
43-
const health = await this.health();
44-
this.legacyApi = health.version.startsWith("1.0.");
45-
return health.version;
43+
try {
44+
const response = await this.getHealth();
45+
return response.data["version"];
46+
}
47+
catch (err) {
48+
// Check if the the response is OK, but not JSON. If it is, enable the legacy api
49+
if (err.name === "FetchError") {
50+
if (err.response.status === 200) {
51+
this.legacyApi = true;
52+
return "1.0.x";
53+
}
54+
}
55+
56+
throw new Error("Failed to obtain alpha health.");
57+
}
4658
}
4759

4860
public alter(op: Operation): Promise<Payload> {
@@ -232,27 +244,6 @@ export class DgraphClientStub {
232244
return this.callAPI(url, { method: "POST" });
233245
}
234246

235-
public async health(): Promise<{ health: string; version: string; instance?: string; uptime?: number }> {
236-
const response: { status: number; text(): Promise<string> } =
237-
await fetch(this.getURL("health"), { // tslint:disable-line no-unsafe-any
238-
method: "GET",
239-
});
240-
if (response.status >= 300 || response.status < 200) {
241-
throw new Error(`Invalid status code = ${response.status}`);
242-
}
243-
const text = await response.text();
244-
if (text === "OK") {
245-
return {
246-
health: text,
247-
version: "1.0.x",
248-
};
249-
}
250-
return {
251-
...JSON.parse(text),
252-
health: "OK",
253-
};
254-
}
255-
256247
public async login(userid?: string, password?: string, refreshToken?: string): Promise<boolean> {
257248
if (this.legacyApi) {
258249
throw new Error("Pre v1.1 clients do not support Login methods");
@@ -304,10 +295,10 @@ export class DgraphClientStub {
304295
* @returns {Promise<Response>} Health in JSON
305296
* @memberof DgraphClientStub
306297
*/
307-
public async getHealth(all: boolean = false): Promise<Response> {
298+
public getHealth(all: boolean = false): Promise<Response> {
308299
const url = "health" + (all? "?all" : "");
309300

310-
return await this.callAPI(url, {
301+
return this.callAPI(url, {
311302
method: "GET",
312303
});
313304
}
@@ -318,8 +309,8 @@ export class DgraphClientStub {
318309
* @returns {Promise<Response>} State in JSON
319310
* @memberof DgraphClientStub
320311
*/
321-
public async getState(): Promise<Response> {
322-
return await this.callAPI("state", {
312+
public getState(): Promise<Response> {
313+
return this.callAPI("state", {
323314
method: "GET",
324315
});
325316
}
@@ -358,26 +349,35 @@ export class DgraphClientStub {
358349
}
359350

360351
private async callAPI<T>(path: string, config: { method?: string; body?: string; headers?: { [k: string]: string } }): Promise<T> {
361-
const url = this.getURL(path);
362-
if (this.accessToken !== undefined && path !== "login") {
363-
config.headers = config.headers !== undefined ? config.headers : {};
364-
config.headers["X-Dgraph-AccessToken"] = this.accessToken;
365-
}
352+
const url = this.getURL(path);
353+
if (this.accessToken !== undefined && path !== "login") {
354+
config.headers = config.headers !== undefined ? config.headers : {};
355+
config.headers["X-Dgraph-AccessToken"] = this.accessToken;
356+
}
366357

367-
const response = await fetch(url, config);
358+
const response = await fetch(url, config);
368359

369-
if (response.status >= 300 || response.status < 200) {
370-
throw new Error(`Invalid status code = ${response.status}`);
371-
}
360+
if (response.status >= 300 || response.status < 200) {
361+
throw new Error(`Invalid status code = ${response.status}`);
362+
}
372363

373-
const json = await response.json();
374-
const errors = (<{ errors: APIResultError[] }><any>json).errors; // tslint:disable-line no-any
364+
// Include response in err for legacy purposes
365+
let json;
366+
try {
367+
json = await response.json();
368+
}
369+
catch (err) {
370+
err.response = response;
371+
throw err;
372+
}
375373

376-
if (errors !== undefined) {
377-
throw new APIError(url, errors);
378-
}
374+
const errors = (<{ errors: APIResultError[] }><any>json).errors; // tslint:disable-line no-any
375+
376+
if (errors !== undefined) {
377+
throw new APIError(url, errors);
378+
}
379379

380-
return json;
380+
return json;
381381
}
382382

383383
private getURL(path: string): string {

0 commit comments

Comments
 (0)