@@ -7,6 +7,7 @@ const { WebhookRegistry } = require('./webhook');
77const logger = require ( './logger' ) ;
88const { fdkAxios } = require ( '@gofynd/fdk-client-javascript/sdk/common/AxiosHelper' ) ;
99const { version } = require ( './../package.json' ) ;
10+ const { RetryManger } = require ( "./retry_manager" )
1011
1112class Extension {
1213 constructor ( ) {
@@ -19,11 +20,17 @@ class Extension {
1920 this . cluster = "https://api.fynd.com" ;
2021 this . webhookRegistry = null ;
2122 this . _isInitialized = false ;
23+ this . _retryManager = new RetryManger ( ) ;
2224 }
2325
2426 async initialize ( data ) {
2527
28+ if ( this . _isInitialized ) {
29+ return ;
30+ }
31+
2632 this . _isInitialized = false ;
33+ this . configData = data ;
2734
2835 this . storage = data . storage ;
2936
@@ -50,22 +57,22 @@ class Extension {
5057 }
5158 this . cluster = data . cluster ;
5259 }
53- this . webhookRegistry = new WebhookRegistry ( ) ;
60+ this . webhookRegistry = new WebhookRegistry ( this . _retryManager ) ;
5461
55- let extensionData = await this . getExtensionDetails ( ) ;
62+ await this . getExtensionDetails ( ) ;
5663
5764 if ( data . base_url && ! validator . isURL ( data . base_url ) ) {
5865 throw new FdkInvalidExtensionConfig ( "Invalid base_url value. Invalid value: " + data . base_url ) ;
5966 }
6067 else if ( ! data . base_url ) {
61- data . base_url = extensionData . base_url ;
68+ data . base_url = this . extensionData . base_url ;
6269 }
6370 this . base_url = data . base_url ;
6471
6572 if ( data . scopes ) {
66- data . scopes = this . verifyScopes ( data . scopes , extensionData ) ;
73+ data . scopes = this . verifyScopes ( data . scopes , this . extensionData ) ;
6774 }
68- this . scopes = data . scopes || extensionData . scope ;
75+ this . scopes = data . scopes || this . extensionData . scope ;
6976
7077 logger . debug ( `Extension initialized` ) ;
7178
@@ -96,9 +103,9 @@ class Extension {
96103 return this . access_mode === 'online' ;
97104 }
98105
99- getPlatformConfig ( companyId ) {
106+ async getPlatformConfig ( companyId ) {
100107 if ( ! this . _isInitialized ) {
101- throw new FdkInvalidExtensionConfig ( 'Extension not initialized due to invalid data' )
108+ await this . initialize ( this . configData ) ;
102109 }
103110 let platformConfig = new PlatformConfig ( {
104111 companyId : parseInt ( companyId ) ,
@@ -113,11 +120,11 @@ class Extension {
113120
114121 async getPlatformClient ( companyId , session ) {
115122 if ( ! this . _isInitialized ) {
116- throw new FdkInvalidExtensionConfig ( 'Extension not initialized due to invalid data' )
123+ await this . initialize ( this . configData ) ;
117124 }
118125 const SessionStorage = require ( './session/session_storage' ) ;
119126
120- let platformConfig = this . getPlatformConfig ( companyId ) ;
127+ let platformConfig = await this . getPlatformConfig ( companyId ) ;
121128 platformConfig . oauthClient . setToken ( session ) ;
122129 platformConfig . oauthClient . token_expires_at = session . access_token_validity ;
123130
@@ -140,14 +147,22 @@ class Extension {
140147 }
141148
142149 async getExtensionDetails ( ) {
150+
151+ let url = `${ this . cluster } /service/panel/partners/v1.0/extensions/details/${ this . api_key } ` ;
152+ const uniqueKey = `${ url } ` ;
153+
154+ const retryInfo = this . _retryManager . retryInfoMap . get ( uniqueKey ) ;
155+ if ( retryInfo && ! retryInfo . isRetry ) {
156+ this . _retryManager . resetRetryState ( uniqueKey ) ;
157+ }
158+
143159 try {
144- let url = `${ this . cluster } /service/panel/partners/v1.0/extensions/details/${ this . api_key } ` ;
145160 const token = Buffer . from (
146161 `${ this . api_key } :${ this . api_secret } ` ,
147162 "utf8"
148163 ) . toString ( "base64" ) ;
149164 const rawRequest = {
150- method : "get " ,
165+ method : "GET " ,
151166 url : url ,
152167 headers : {
153168 Authorization : `Basic ${ token } ` ,
@@ -157,8 +172,17 @@ class Extension {
157172 } ;
158173 let extensionData = await fdkAxios . request ( rawRequest ) ;
159174 logger . debug ( `Extension details received: ${ logger . safeStringify ( extensionData ) } ` ) ;
160- return extensionData ;
175+ this . extensionData = extensionData ;
161176 } catch ( err ) {
177+
178+ if (
179+ RetryManger . shouldRetryOnError ( err )
180+ && ! this . _retryManager . isRetryInProgress ( uniqueKey )
181+ ) {
182+ logger . debug ( `API call failed. Starting retry for ${ uniqueKey } ` )
183+ return await this . _retryManager . retry ( uniqueKey , this . getExtensionDetails . bind ( this ) ) ;
184+ }
185+
162186 throw new FdkInvalidExtensionConfig ( "Invalid api_key or api_secret. Reason:" + err . message ) ;
163187 }
164188 }
0 commit comments