Skip to content

Commit ca112b2

Browse files
Add Health API endpoint
1 parent 9dbad97 commit ca112b2

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

src/implementation/Client/DaprClient.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import HTTPClient from './HTTPClient/HTTPClient';
2121

2222
import CommunicationProtocolEnum from '../../enum/CommunicationProtocol.enum';
2323
import { DaprClientOptions } from '../../types/DaprClientOptions';
24+
import HTTPClientHealth from './HTTPClient/health';
25+
import IClientHealth from '../../interfaces/Client/IClientHealth';
26+
import GRPCClientHealth from './GRPCClient/health';
2427

2528
export default class DaprClient {
2629
readonly daprHost: string;
@@ -34,6 +37,7 @@ export default class DaprClient {
3437
readonly binding: IClientBinding;
3538
readonly invoker: IClientInvoker;
3639
readonly secret: IClientSecret;
40+
readonly health: IClientHealth;
3741

3842
constructor(
3943
daprHost: string
@@ -64,6 +68,7 @@ export default class DaprClient {
6468
this.binding = new GRPCClientBinding(client);
6569
this.invoker = new GRPCClientInvoker(client);
6670
this.secret = new GRPCClientSecret(client);
71+
this.health = new GRPCClientHealth(client);
6772
break;
6873
}
6974
case CommunicationProtocolEnum.HTTP:
@@ -76,6 +81,7 @@ export default class DaprClient {
7681
this.binding = new HTTPClientBinding(client);
7782
this.invoker = new HTTPClientInvoker(client);
7883
this.secret = new HTTPClientSecret(client);
84+
this.health = new HTTPClientHealth(client);
7985
break;
8086
}
8187
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import GRPCClient from './GRPCClient';
2+
import IClientHealth from '../../../interfaces/Client/IClientHealth';
3+
4+
// https://docs.dapr.io/reference/api/health_api/
5+
export default class GRPCClientHealth implements IClientHealth {
6+
client: GRPCClient;
7+
8+
constructor(client: GRPCClient) {
9+
this.client = client;
10+
}
11+
12+
async isHealthy(): Promise<boolean> {
13+
return true;
14+
}
15+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import HTTPClient from './HTTPClient';
2+
import IClientHealth from '../../../interfaces/Client/IClientHealth';
3+
4+
// https://docs.dapr.io/reference/api/health_api/
5+
export default class HTTPClientHealth implements IClientHealth {
6+
client: HTTPClient;
7+
8+
constructor(client: HTTPClient) {
9+
this.client = client;
10+
}
11+
12+
// Send an event to an external system
13+
async isHealthy(): Promise<boolean> {
14+
try {
15+
const result = await this.client.execute(`/healthz`, {
16+
method: 'GET',
17+
headers: {
18+
'Content-Type': 'application/json',
19+
}
20+
});
21+
22+
return true;
23+
} catch (e) {
24+
return false;
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)