@@ -8,6 +8,7 @@ const logger = require('./logger');
88const { fdkAxios } = require ( '@gofynd/fdk-client-javascript/sdk/common/AxiosHelper' ) ;
99const { version } = require ( './../package.json' ) ;
1010const SessionStorage = require ( "./session/session_storage" ) ;
11+ const { RetryManger } = require ( "./retry_manager" )
1112
1213class Extension {
1314 constructor ( ) {
@@ -22,11 +23,17 @@ class Extension {
2223 this . sessionStore = null ;
2324 this . _isInitialized = false ;
2425 this . _clusterId = null ;
26+ this . _retryManager = new RetryManger ( ) ;
2527 }
2628
2729 async initialize ( data ) {
2830
31+ if ( this . _isInitialized ) {
32+ return ;
33+ }
34+
2935 this . _isInitialized = false ;
36+ this . configData = data ;
3037
3138 this . storage = data . storage ;
3239
@@ -55,22 +62,22 @@ class Extension {
5562 this . cluster = data . cluster ;
5663 this . _clusterId = this . cluster . replace ( "https://" , "" ) ;
5764 }
58- this . webhookRegistry = new WebhookRegistry ( ) ;
65+ this . webhookRegistry = new WebhookRegistry ( this . _retryManager ) ;
5966
60- let extensionData = await this . getExtensionDetails ( ) ;
67+ await this . getExtensionDetails ( ) ;
6168
6269 if ( data . base_url && ! validator . isURL ( data . base_url ) ) {
6370 throw new FdkInvalidExtensionConfig ( "Invalid base_url value. Invalid value: " + data . base_url ) ;
6471 }
6572 else if ( ! data . base_url ) {
66- data . base_url = extensionData . base_url ;
73+ data . base_url = this . extensionData . base_url ;
6774 }
6875 this . base_url = data . base_url ;
6976
7077 if ( data . scopes ) {
71- data . scopes = this . verifyScopes ( data . scopes , extensionData ) ;
78+ data . scopes = this . verifyScopes ( data . scopes , this . extensionData ) ;
7279 }
73- this . scopes = data . scopes || extensionData . scope ;
80+ this . scopes = data . scopes || this . extensionData . scope ;
7481
7582 logger . debug ( `Extension initialized` ) ;
7683
@@ -105,9 +112,9 @@ class Extension {
105112 return this . access_mode === 'online' ;
106113 }
107114
108- getPlatformConfig ( companyId ) {
115+ async getPlatformConfig ( companyId ) {
109116 if ( ! this . _isInitialized ) {
110- throw new FdkInvalidExtensionConfig ( 'Extension not initialized due to invalid data' )
117+ await this . initialize ( this . configData ) ;
111118 }
112119 let platformConfig = new PlatformConfig ( {
113120 companyId : parseInt ( companyId ) ,
@@ -122,10 +129,10 @@ class Extension {
122129
123130 async getPlatformClient ( companyId , session ) {
124131 if ( ! this . _isInitialized ) {
125- throw new FdkInvalidExtensionConfig ( 'Extension not initialized due to invalid data' )
132+ await this . initialize ( this . configData ) ;
126133 }
127134
128- let platformConfig = this . getPlatformConfig ( companyId ) ;
135+ let platformConfig = await this . getPlatformConfig ( companyId ) ;
129136 platformConfig . oauthClient . setToken ( session ) ;
130137 platformConfig . oauthClient . token_expires_at = session . access_token_validity ;
131138
@@ -148,14 +155,22 @@ class Extension {
148155 }
149156
150157 async getExtensionDetails ( ) {
158+
159+ let url = `${ this . cluster } /service/panel/partners/v1.0/extensions/details/${ this . api_key } ` ;
160+ const uniqueKey = `${ url } ` ;
161+
162+ const retryInfo = this . _retryManager . retryInfoMap . get ( uniqueKey ) ;
163+ if ( retryInfo && ! retryInfo . isRetry ) {
164+ this . _retryManager . resetRetryState ( uniqueKey ) ;
165+ }
166+
151167 try {
152- let url = `${ this . cluster } /service/panel/partners/v1.0/extensions/details/${ this . api_key } ` ;
153168 const token = Buffer . from (
154169 `${ this . api_key } :${ this . api_secret } ` ,
155170 "utf8"
156171 ) . toString ( "base64" ) ;
157172 const rawRequest = {
158- method : "get " ,
173+ method : "GET " ,
159174 url : url ,
160175 headers : {
161176 Authorization : `Basic ${ token } ` ,
@@ -165,9 +180,18 @@ class Extension {
165180 } ;
166181 let extensionData = await fdkAxios . request ( rawRequest ) ;
167182 logger . debug ( `Extension details received: ${ logger . safeStringify ( extensionData ) } ` ) ;
168- return extensionData ;
183+ this . extensionData = extensionData ;
169184 } catch ( err ) {
170- throw new FdkInvalidExtensionConfig ( "Invalid api_key or api_secret. Reason: " + err . message ) ;
185+
186+ if (
187+ RetryManger . shouldRetryOnError ( err )
188+ && ! this . _retryManager . isRetryInProgress ( uniqueKey )
189+ ) {
190+ logger . debug ( `API call failed. Starting retry for ${ uniqueKey } ` )
191+ return await this . _retryManager . retry ( uniqueKey , this . getExtensionDetails . bind ( this ) ) ;
192+ }
193+
194+ throw new FdkInvalidExtensionConfig ( "Invalid api_key or api_secret. Reason:" + err . message ) ;
171195 }
172196 }
173197}
0 commit comments