-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Alexandre-Monney <[email protected]>
- Loading branch information
1 parent
4bf51aa
commit a2b1106
Showing
5 changed files
with
215 additions
and
0 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
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,64 @@ | ||
import jsonwebtoken from 'jsonwebtoken'; | ||
|
||
import { httpAgent } from '../../../../lib/infrastructure/http/http-agent.js'; | ||
import { config } from '../../config.js'; | ||
|
||
export const STATUS = { | ||
SUCCESS: 'success', | ||
FAILURE: 'failure', | ||
}; | ||
|
||
export const API_DATA_QUERIES = config.apiData.queries || {}; | ||
|
||
export class ApiData { | ||
#token; | ||
|
||
constructor(apiDataUrl, apiDataCredentials) { | ||
this.apiDataUrl = apiDataUrl; | ||
this.apiDataCredentials = apiDataCredentials; | ||
} | ||
|
||
set token(token) { | ||
this.#token = token; | ||
} | ||
|
||
async getToken() { | ||
if (!this.#token) { | ||
this.#token = await this.#fetchToken(); | ||
return this.#token; | ||
} | ||
|
||
const decodedToken = jsonwebtoken.decode(this.#token); | ||
const preventCloseExpiration = 10; | ||
const isTokenExpired = decodedToken.exp < Date.now() / 1000 + preventCloseExpiration; | ||
|
||
if (isTokenExpired) { | ||
this.#token = await this.#fetchToken(); | ||
} | ||
|
||
return this.#token; | ||
} | ||
|
||
async #fetchToken() { | ||
if (!this.apiDataCredentials?.username || !this.apiDataCredentials?.password) { | ||
throw new Error('ApiData credentials are not set'); | ||
} | ||
const result = await httpAgent.post({ | ||
url: `${this.apiDataUrl}/token`, | ||
payload: this.apiDataCredentials, | ||
}); | ||
return result.data.data.access_token; | ||
} | ||
|
||
async get(queryId, params = []) { | ||
const token = await this.getToken(); | ||
const result = await httpAgent.post({ | ||
url: `${this.apiDataUrl}/query`, | ||
payload: { queryId, params }, | ||
headers: { Authorization: `Bearer ${token}` }, | ||
}); | ||
return result.data; | ||
} | ||
} | ||
|
||
export const apiData = new ApiData(config.apiData.url, config.apiData.credentials); |
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
108 changes: 108 additions & 0 deletions
108
api/tests/unit/infrastructure/datasources/ApiData_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,108 @@ | ||
import jsonwebtoken from 'jsonwebtoken'; | ||
|
||
import { ApiData } from '../../../../src/shared/infrastructure/datasources/ApiData.js'; | ||
import { expect, nock } from '../../../test-helper.js'; | ||
|
||
describe('Unit | Infrastructure | Datasources | ApiData', function () { | ||
describe('#getToken', function () { | ||
context('when they are no token', function () { | ||
it('should fetch a token and return it', async function () { | ||
// given | ||
const apiDataCredentials = { username: 'username', password: 'password' }; | ||
const apiDataUrl = 'http://example.net'; | ||
const apiData = new ApiData(apiDataUrl, apiDataCredentials); | ||
|
||
const fetchTokenMock = nock(apiDataUrl) | ||
.post('/token', apiDataCredentials) | ||
.reply(200, { test: 'test', data: { access_token: 'returned-token' } }); | ||
|
||
// when | ||
const token = await apiData.getToken(); | ||
|
||
// then | ||
expect(fetchTokenMock.isDone()).to.be.true; | ||
expect(token).to.equal('returned-token'); | ||
}); | ||
}); | ||
|
||
context('when the token is expired', function () { | ||
it('should fetch a new token and return it', async function () { | ||
// given | ||
const apiDataCredentials = { username: 'username', password: 'password' }; | ||
const apiDataUrl = 'http://example.net'; | ||
const apiData = new ApiData(apiDataUrl, apiDataCredentials); | ||
|
||
const invalidToken = jsonwebtoken.sign({}, 'test-secret', { expiresIn: '1sec' }); | ||
apiData.token = invalidToken; | ||
|
||
const fetchTokenMock = nock(apiDataUrl) | ||
.post('/token', apiDataCredentials) | ||
.reply(200, { test: 'test', data: { access_token: 'returned-token' } }); | ||
|
||
// when | ||
setTimeout(async () => { | ||
return; | ||
}, 100); | ||
|
||
const token = await apiData.getToken(); | ||
|
||
// then | ||
expect(fetchTokenMock.isDone()).to.be.true; | ||
expect(token).to.equal('returned-token'); | ||
}); | ||
}); | ||
|
||
context('when the token is not expired', function () { | ||
it('should return the token', async function () { | ||
// given | ||
const apiDataCredentials = { username: 'username', password: 'password' }; | ||
const apiDataUrl = 'http://example.net'; | ||
const apiData = new ApiData(apiDataUrl, apiDataCredentials); | ||
|
||
const validToken = jsonwebtoken.sign({}, 'test-secret', { expiresIn: '30d' }); | ||
apiData.token = validToken; | ||
|
||
const fetchTokenMock = nock(apiDataUrl) | ||
.post('/token', apiDataCredentials) | ||
.reply(200, { test: 'test', data: { access_token: 'returned-token' } }); | ||
|
||
// when | ||
const token = await apiData.getToken(); | ||
|
||
// then | ||
expect(fetchTokenMock.isDone()).to.be.false; | ||
expect(token).to.equal(validToken); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('#get', function () { | ||
it('should use the token to fetch data', async function () { | ||
// given | ||
const apiDataCredentials = { username: 'username', password: 'password' }; | ||
const apiDataUrl = 'http://example.net'; | ||
|
||
const apiData = new ApiData(apiDataUrl, apiDataCredentials); | ||
|
||
const validToken = jsonwebtoken.sign({}, 'test-secret', { expiresIn: '30d' }); | ||
apiData.token = validToken; | ||
|
||
const queryId = 'queryId'; | ||
const params = [{ param: 'value' }]; | ||
|
||
const expectedData = [{ result: 'result' }]; | ||
const fetchMock = nock(apiDataUrl) | ||
.post('/query', { queryId, params }) | ||
.matchHeader('Content-Type', 'application/json') | ||
.matchHeader('Authorization', `Bearer ${validToken}`) | ||
.reply(200, { status: 'success', data: expectedData }); | ||
|
||
// when | ||
const result = await apiData.get(queryId, params); | ||
|
||
// then | ||
expect(fetchMock.isDone()).to.be.true; | ||
expect(result.data).to.deep.equal(expectedData); | ||
}); | ||
}); | ||
}); |