Skip to content

Commit

Permalink
feat: add method getHttpClient to get underlying axios instance of …
Browse files Browse the repository at this point in the history
…base service (#155)

This new method is added to the base service class so that all initialized SDK client objects
have access to the underlying axios instance used to make requests. This instance is currently
hidden as a private class member. This functionality is already supported in the other language
cores, so this brings feature parity
  • Loading branch information
dpopp07 authored Aug 18, 2021
1 parent 0f708f2 commit 2aede01
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"files": "package-lock.json|^.secrets.baseline$",
"lines": null
},
"generated_at": "2021-08-13T17:45:41Z",
"generated_at": "2021-08-17T20:14:30Z",
"plugins_used": [
{
"name": "AWSKeyDetector"
Expand Down Expand Up @@ -522,7 +522,7 @@
}
]
},
"version": "0.13.1+ibm.43.dss",
"version": "0.13.1+ibm.40.dss",
"word_list": {
"file": null,
"hash": null
Expand Down
8 changes: 8 additions & 0 deletions lib/base-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ export class BaseService {
this.baseOptions.enableGzipCompression = setting;
}

/**
* Get the Axios instance set on the service.
* All requests will be made using this instance.
*/
public getHttpClient() {
return this.requestWrapperInstance.getHttpClient();
}

/**
* Configure the service using external configuration
*
Expand Down
4 changes: 4 additions & 0 deletions lib/request-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,10 @@ export class RequestWrapper {
return error;
}

public getHttpClient() {
return this.axiosInstance;
}

private async gzipRequestBody(data: any, headers: OutgoingHttpHeaders): Promise<Buffer | any> {
// skip compression if user has set the encoding header to gzip
const contentSetToGzip =
Expand Down
15 changes: 15 additions & 0 deletions test/unit/base-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ const requestWrapperLocation = '../../dist/lib/request-wrapper';
jest.mock(requestWrapperLocation);
const { RequestWrapper } = require(requestWrapperLocation);
const sendRequestMock = jest.fn();
const getHttpClientMock = jest.fn().mockImplementation(() => 'axios');

RequestWrapper.mockImplementation(() => ({
sendRequest: sendRequestMock,
getHttpClient: getHttpClientMock,
}));

// mock the authenticator
Expand Down Expand Up @@ -48,6 +50,7 @@ describe('Base Service', () => {
afterEach(() => {
// clear and the metadata attached to the mocks
sendRequestMock.mockClear();
getHttpClientMock.mockClear();
RequestWrapper.mockClear();
// also, reset the implementation of the readExternalSourcesMock
readExternalSourcesMock.mockReset();
Expand Down Expand Up @@ -129,6 +132,18 @@ describe('Base Service', () => {
expect(testService.getAuthenticator()).toEqual(AUTHENTICATOR);
});

it('should return the stored, underlying axios instance with getHttpClient', () => {
const testService = new TestService({
authenticator: AUTHENTICATOR,
});

const httpClient = testService.getHttpClient();

// the request wrapper instance is mocked to return the literal string 'axios'
expect(httpClient).toEqual('axios');
expect(getHttpClientMock).toHaveBeenCalled();
});

it('should store disableSslVerification when set', () => {
const testService = new TestService({
authenticator: AUTHENTICATOR,
Expand Down
7 changes: 7 additions & 0 deletions test/unit/request-wrapper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,13 @@ describe('formatError', () => {
});
});

describe('getHttpClient', () => {
it('should return the axios instance', () => {
const client = requestWrapperInstance.getHttpClient();
expect(client).toEqual(mockAxiosInstance);
});
});

describe('gzipRequestBody', () => {
const gzipSpy = jest.spyOn(zlib, 'gzipSync');
const errorLogSpy = jest.spyOn(logger, 'error').mockImplementation(() => {});
Expand Down

0 comments on commit 2aede01

Please sign in to comment.