Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add singleton principle to RestService #198

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would normally be private, but this would break the jest mocks and RestService is not exposed to the user regardless.

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