Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2924fa7

Browse files
Add singleton principle to RestService
1 parent d1e7c67 commit 2924fa7

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

‎schemaregistry/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"scripts": {
6060
"lint": "make lint",
6161
"test": "make test",
62+
"integtest": "make integtest",
6263
"build": "rm -rf ./dist && tsc -p tsconfig-build.json"
6364
},
6465
"keywords": [

‎schemaregistry/rest-service.ts‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,15 @@ export interface ClientConfig {
5151
const toBase64 = (str: string): string => Buffer.from(str).toString('base64');
5252

5353
export class RestService {
54+
private static instance: RestService;
5455
private client: AxiosInstance;
5556
private baseURLs: string[];
5657
private oauthClient?: OAuthClient;
5758
private oauthBearer: boolean = false;
5859

5960
constructor(baseURLs: string[], isForward?: boolean, axiosDefaults?: CreateAxiosDefaults,
60-
basicAuthCredentials?: BasicAuthCredentials, bearerAuthCredentials?: BearerAuthCredentials,
61-
maxRetries?: number, retriesWaitMs?: number, retriesMaxWaitMs?: number) {
61+
basicAuthCredentials?: BasicAuthCredentials, bearerAuthCredentials?: BearerAuthCredentials,
62+
maxRetries?: number, retriesWaitMs?: number, retriesMaxWaitMs?: number) {
6263
this.client = axios.create(axiosDefaults);
6364
axiosRetry(this.client, {
6465
retries: maxRetries ?? 2,
@@ -80,6 +81,16 @@ export class RestService {
8081
this.handleBearerAuth(maxRetries ?? 2, retriesWaitMs ?? 1000, retriesMaxWaitMs ?? 20000, bearerAuthCredentials);
8182
}
8283

84+
static getInstance(baseURLs: string[], isForward?: boolean, axiosDefaults?: CreateAxiosDefaults,
85+
basicAuthCredentials?: BasicAuthCredentials, bearerAuthCredentials?: BearerAuthCredentials,
86+
maxRetries?: number, retriesWaitMs?: number, retriesMaxWaitMs?: number): RestService {
87+
if (!this.instance) {
88+
this.instance = new RestService(baseURLs, isForward, axiosDefaults, basicAuthCredentials, bearerAuthCredentials,
89+
maxRetries, retriesWaitMs, retriesMaxWaitMs);
90+
}
91+
return this.instance;
92+
}
93+
8394
handleBasicAuth(basicAuthCredentials?: BasicAuthCredentials): void {
8495
if (basicAuthCredentials) {
8596
switch (basicAuthCredentials.credentialsSource) {
@@ -111,7 +122,7 @@ export class RestService {
111122
}
112123
}
113124

114-
handleBearerAuth(maxRetries: number,
125+
handleBearerAuth(maxRetries: number,
115126
retriesWaitMs: number, retriesMaxWaitMs: number, bearerAuthCredentials?: BearerAuthCredentials): void {
116127
if (bearerAuthCredentials) {
117128
delete this.client.defaults.auth;
@@ -150,7 +161,7 @@ export class RestService {
150161
}
151162
const issuerEndPointUrl = new URL(bearerAuthCredentials.issuerEndpointUrl!);
152163
this.oauthClient = new OAuthClient(bearerAuthCredentials.clientId!, bearerAuthCredentials.clientSecret!,
153-
issuerEndPointUrl.origin, issuerEndPointUrl.pathname, bearerAuthCredentials.scope!,
164+
issuerEndPointUrl.origin, issuerEndPointUrl.pathname, bearerAuthCredentials.scope!,
154165
maxRetries, retriesWaitMs, retriesMaxWaitMs);
155166
break;
156167
default:

‎schemaregistry/rules/encryption/dekregistry/dekregistry-client.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class DekRegistryClient implements DekClient {
7070
};
7171

7272

73-
this.restService = newRestService(config.baseURLs, config.isForward, config.createAxiosDefaults,
73+
this.restService = RestService.getInstance(config.baseURLs, config.isForward, config.createAxiosDefaults,
7474
config.basicAuthCredentials, config.bearerAuthCredentials,
7575
config.maxRetries, config.retriesWaitMs, config.retriesMaxWaitMs);
7676
this.kekCache = new LRUCache<string, Kek>(cacheOptions);

‎schemaregistry/schemaregistry-client.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ export class SchemaRegistryClient implements Client {
201201
...(config.cacheLatestTtlSecs !== undefined && { ttl: config.cacheLatestTtlSecs * 1000 })
202202
};
203203

204-
this.restService = newRestService(config.baseURLs, config.isForward, config.createAxiosDefaults,
204+
this.restService = RestService.getInstance(config.baseURLs, config.isForward, config.createAxiosDefaults,
205205
config.basicAuthCredentials, config.bearerAuthCredentials,
206206
config.maxRetries, config.retriesWaitMs, config.retriesMaxWaitMs);
207207

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /