-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5391 from LiskHQ/5357-allow-mainchain-to-switch-t…
…o-different-service-url Allow mainchain to switch to different service url
- Loading branch information
Showing
6 changed files
with
122 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/modules/blockchainApplication/manage/hooks/useValidServiceUrl.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { isEmpty } from 'src/utils/helpers'; | ||
import { Client } from 'src/utils/api/client'; | ||
|
||
async function isNetworkUrlSuccess(fetchUrl, successBaseUrlToReturn) { | ||
try { | ||
const client = new Client({ http: fetchUrl }); | ||
await client.rest(); | ||
return successBaseUrlToReturn; | ||
} catch (error) { | ||
return false; | ||
} | ||
} | ||
|
||
export async function resolveApiValidity(serviceURLs) { | ||
const promises = []; | ||
for (let i = 0; i < serviceURLs.length; i++) { | ||
const baseServiceUrl = serviceURLs[i]?.http; | ||
promises.push(isNetworkUrlSuccess(`${baseServiceUrl}/api/v3/index/status`, baseServiceUrl)); | ||
} | ||
const responses = await Promise.all(promises); | ||
return responses.find((response) => response); | ||
} | ||
|
||
/* istanbul ignore next */ | ||
export function useValidServiceUrl(serviceURLs) { | ||
const [validServiceUrl, setValidServiceUrl] = useState(''); | ||
const [isLoading, setIsLoading] = useState(!!serviceURLs); | ||
|
||
useEffect(() => { | ||
(async () => { | ||
if (!isEmpty(serviceURLs)) { | ||
setIsLoading(true); | ||
const serviceUrl = await resolveApiValidity(serviceURLs); | ||
setValidServiceUrl(serviceUrl); | ||
setIsLoading(false); | ||
} | ||
})(); | ||
}, [serviceURLs]); | ||
|
||
return { validServiceUrl, isLoading }; | ||
} |
39 changes: 39 additions & 0 deletions
39
src/modules/blockchainApplication/manage/hooks/useValidServiceUrl.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import * as client from 'src/utils/api/client'; | ||
import { resolveApiValidity } from './useValidServiceUrl'; | ||
|
||
describe('useValidServiceurl', () => { | ||
it('returns valid service url', async () => { | ||
jest.spyOn(client, 'Client').mockReturnValue({ | ||
rest: jest.fn().mockResolvedValue(), | ||
}); | ||
const serviceUrls = [{ http: 'http://localhost:9901' }, { http: 'http://localhost-2:9901' }]; | ||
const validServiceUrl = await resolveApiValidity(serviceUrls); | ||
|
||
expect(validServiceUrl).toBe(serviceUrls[0].http); | ||
}); | ||
|
||
it('should not return a url if serviceUrls are invalid', async () => { | ||
jest.spyOn(client, 'Client').mockReturnValue({ | ||
rest: jest.fn().mockRejectedValue(), | ||
}); | ||
|
||
const serviceUrls = [{ http: 'http://localhost:9901' }, { http: 'http://localhost-2:9901' }]; | ||
const validServiceUrl = await resolveApiValidity(serviceUrls); | ||
|
||
expect(validServiceUrl).toBeFalsy(); | ||
}); | ||
|
||
it('should return the second url if the first is invalid', async () => { | ||
const serviceUrls = [{ http: 'http://localhost:9901' }, { http: 'http://localhost-2:9901' }]; | ||
|
||
jest.spyOn(client, 'Client').mockImplementation(({ http }) => ({ | ||
rest: | ||
http === `${serviceUrls[0].http}/api/v3/index/status` | ||
? jest.fn().mockRejectedValue() | ||
: jest.fn().mockReturnValue(), | ||
})); | ||
const validServiceUrl = await resolveApiValidity(serviceUrls); | ||
|
||
expect(validServiceUrl).toBe(serviceUrls[1].http); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters