Skip to content

Commit eaf6511

Browse files
committed
Add ConnectionStatusListener to RestClient
Clients can add a connection status listener to the RestClient to be notified when the connection goes up or down. The connection status is detected based on whether any HTTP request can be completed or not, or based on the returned HTTP status code. When a listener is added it will immediately receive the current status through the listener interface. Change-Id: I5f7fcbf4b94380b2ea349755a400bd61bbadc7d2 Signed-off-by: Patrick Tasse <patrick.tasse@ericsson.com>
1 parent c53f0da commit eaf6511

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

tsp-typescript-client/src/protocol/rest-client.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,27 @@ export interface HttpResponse {
2121
headers?: Headers
2222
}
2323

24+
/**
25+
* A listener that is called when the connection status is changed.
26+
* The status 'true' indicates that the server was last reached successfully.
27+
* The status 'false' indicates that the server could not be reached or
28+
* returned a status code indicating that it cannot service requests (>= 502).
29+
*/
30+
export type ConnectionStatusListener = ((status: boolean) => void);
31+
2432
/**
2533
* Rest client helper to make request.
2634
* The request response status code indicates if the request is successful.
2735
* The json object in the response may be undefined when an error occurs.
2836
*/
2937
export class RestClient {
3038

39+
static status = false;
40+
static connectionStatusListeners: ConnectionStatusListener[] = [];
41+
3142
static get<T>(url: string, parameters?: Map<string, string>): Promise<TspClientResponse<Deserialized<T>>>;
3243
static get<T>(url: string, parameters: Map<string, string> | undefined, normalizer: Normalizer<T>): Promise<TspClientResponse<T>>;
44+
3345
/**
3446
* Perform GET
3547
* @template T is the expected type of the json object returned by this request
@@ -103,7 +115,10 @@ export class RestClient {
103115
},
104116
body: this.jsonStringify(body)
105117
});
118+
const status = response.status <= 501;
119+
this.updateConnectionStatus(status);
106120
} catch (err) {
121+
this.updateConnectionStatus(false);
107122
return new TspClientResponse(err.toString(), 503, 'Service Unavailable');
108123
}
109124

@@ -148,7 +163,7 @@ export class RestClient {
148163
/**
149164
* Stringify JS objects. Can stringify `BigInt` values.
150165
*/
151-
protected static jsonStringify(data: any): string {
166+
protected static jsonStringify(data: any): string {
152167
return JSONBig.stringify(data);
153168
}
154169

@@ -159,4 +174,36 @@ export class RestClient {
159174
protected static jsonParse(text: string): any {
160175
return JSONBig.parse(text);
161176
}
177+
178+
/**
179+
* Adds a connection status listener.
180+
* The listener will immediately be called with the current status.
181+
*
182+
* @param listener The listener to add
183+
*/
184+
public static addConnectionStatusListener(listener: ConnectionStatusListener): void {
185+
if (!this.connectionStatusListeners.includes(listener)) {
186+
this.connectionStatusListeners.push(listener);
187+
}
188+
listener(this.status);
189+
}
190+
191+
/**
192+
* Removes a connection status listener.
193+
*
194+
* @param listener The listener to remove
195+
*/
196+
public static removeConnectionStatusListener(listener: ConnectionStatusListener): void {
197+
this.connectionStatusListeners = this.connectionStatusListeners.filter(element => element !== listener);
198+
}
199+
200+
private static updateConnectionStatus(status: boolean) {
201+
if (this.status === status) {
202+
return;
203+
}
204+
for (const listener of this.connectionStatusListeners) {
205+
listener(status);
206+
}
207+
this.status = status;
208+
}
162209
}

0 commit comments

Comments
 (0)