Skip to content

Commit

Permalink
Add singleton principle to RestService
Browse files Browse the repository at this point in the history
  • Loading branch information
Claimundefine committed Dec 4, 2024
1 parent d1e7c67 commit 2924fa7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions schemaregistry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"scripts": {
"lint": "make lint",
"test": "make test",
"integtest": "make integtest",
"build": "rm -rf ./dist && tsc -p tsconfig-build.json"
},
"keywords": [
Expand Down
19 changes: 15 additions & 4 deletions schemaregistry/rest-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@ export interface ClientConfig {
const toBase64 = (str: string): string => Buffer.from(str).toString('base64');

export class RestService {
private static instance: RestService;
private client: AxiosInstance;
private baseURLs: string[];
private oauthClient?: OAuthClient;
private oauthBearer: boolean = false;

constructor(baseURLs: string[], isForward?: boolean, axiosDefaults?: CreateAxiosDefaults,
basicAuthCredentials?: BasicAuthCredentials, bearerAuthCredentials?: BearerAuthCredentials,
maxRetries?: number, retriesWaitMs?: number, retriesMaxWaitMs?: number) {
basicAuthCredentials?: BasicAuthCredentials, bearerAuthCredentials?: BearerAuthCredentials,
maxRetries?: number, retriesWaitMs?: number, retriesMaxWaitMs?: number) {
this.client = axios.create(axiosDefaults);
axiosRetry(this.client, {
retries: maxRetries ?? 2,
Expand All @@ -80,6 +81,16 @@ export class RestService {
this.handleBearerAuth(maxRetries ?? 2, retriesWaitMs ?? 1000, retriesMaxWaitMs ?? 20000, bearerAuthCredentials);
}

static getInstance(baseURLs: string[], isForward?: boolean, axiosDefaults?: CreateAxiosDefaults,
basicAuthCredentials?: BasicAuthCredentials, bearerAuthCredentials?: BearerAuthCredentials,
maxRetries?: number, retriesWaitMs?: number, retriesMaxWaitMs?: number): RestService {
if (!this.instance) {
this.instance = new RestService(baseURLs, isForward, axiosDefaults, basicAuthCredentials, bearerAuthCredentials,
maxRetries, retriesWaitMs, retriesMaxWaitMs);
}
return this.instance;
}

handleBasicAuth(basicAuthCredentials?: BasicAuthCredentials): void {
if (basicAuthCredentials) {
switch (basicAuthCredentials.credentialsSource) {
Expand Down Expand Up @@ -111,7 +122,7 @@ export class RestService {
}
}

handleBearerAuth(maxRetries: number,
handleBearerAuth(maxRetries: number,
retriesWaitMs: number, retriesMaxWaitMs: number, bearerAuthCredentials?: BearerAuthCredentials): void {
if (bearerAuthCredentials) {
delete this.client.defaults.auth;
Expand Down Expand Up @@ -150,7 +161,7 @@ export class RestService {
}
const issuerEndPointUrl = new URL(bearerAuthCredentials.issuerEndpointUrl!);
this.oauthClient = new OAuthClient(bearerAuthCredentials.clientId!, bearerAuthCredentials.clientSecret!,
issuerEndPointUrl.origin, issuerEndPointUrl.pathname, bearerAuthCredentials.scope!,
issuerEndPointUrl.origin, issuerEndPointUrl.pathname, bearerAuthCredentials.scope!,
maxRetries, retriesWaitMs, retriesMaxWaitMs);
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class DekRegistryClient implements DekClient {
};


this.restService = new RestService(config.baseURLs, config.isForward, config.createAxiosDefaults,
this.restService = RestService.getInstance(config.baseURLs, config.isForward, config.createAxiosDefaults,
config.basicAuthCredentials, config.bearerAuthCredentials,
config.maxRetries, config.retriesWaitMs, config.retriesMaxWaitMs);
this.kekCache = new LRUCache<string, Kek>(cacheOptions);
Expand Down
2 changes: 1 addition & 1 deletion schemaregistry/schemaregistry-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export class SchemaRegistryClient implements Client {
...(config.cacheLatestTtlSecs !== undefined && { ttl: config.cacheLatestTtlSecs * 1000 })
};

this.restService = new RestService(config.baseURLs, config.isForward, config.createAxiosDefaults,
this.restService = RestService.getInstance(config.baseURLs, config.isForward, config.createAxiosDefaults,
config.basicAuthCredentials, config.bearerAuthCredentials,
config.maxRetries, config.retriesWaitMs, config.retriesMaxWaitMs);

Expand Down

0 comments on commit 2924fa7

Please sign in to comment.