Skip to content

Commit 520dba8

Browse files
committed
added astClient
1 parent 914f9ff commit 520dba8

File tree

6 files changed

+62
-47
lines changed

6 files changed

+62
-47
lines changed

src/main/client/AstClient.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {logger} from '../wrapper/loggerConfig';
2+
import * as fs from 'fs';
3+
import {finished} from 'stream/promises';
4+
import {Client} from "./Client";
5+
6+
export class AstClient {
7+
private client: Client;
8+
9+
constructor(client: Client) {
10+
this.client = client;
11+
}
12+
13+
public async downloadFile(url: string, outputPath: string): Promise<void> {
14+
logger.info(`Starting download from URL: ${url}`);
15+
const writer = fs.createWriteStream(outputPath);
16+
try {
17+
const response = await this.client.request(url, 'GET', null);
18+
response.data.pipe(writer);
19+
await finished(writer);
20+
logger.info(`Download completed successfully. File saved to: ${outputPath}`);
21+
} catch (error) {
22+
logger.error(`Error downloading file from ${url}: ${error.message || error}`);
23+
throw error;
24+
} finally {
25+
writer.close();
26+
logger.info('Write stream closed.');
27+
}
28+
}
29+
}

src/main/client/Client.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1+
import {AxiosResponse} from "axios";
2+
13
export interface Client {
2-
/**
3-
* Downloads a file from the given URL and saves it to the specified output path.
4-
*
5-
* @param url - The URL to download the file from.
6-
* @param outputPath - The path where the downloaded file will be saved.
7-
* @throws An error if the download fails.
8-
*/
9-
downloadFile(url: string, outputPath: string): Promise<void>;
104
getProxyConfig(): any;
5+
request(url: string, method: string, data: any): Promise<AxiosResponse<any, any>>;
116
}

src/main/client/HttpClient.ts

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import axios, {AxiosRequestConfig} from 'axios';
1+
import axios, {AxiosRequestConfig, AxiosResponse} from 'axios';
22
import {logger} from '../wrapper/loggerConfig';
3-
import * as fs from 'fs';
4-
import {finished} from 'stream/promises';
53
import {Client} from "./Client";
64

75
export class HttpClient implements Client {
@@ -32,28 +30,22 @@ export class HttpClient implements Client {
3230
logger.info('No proxy configuration detected.');
3331
return undefined;
3432
}
35-
36-
public async downloadFile(url: string, outputPath: string): Promise<void> {
37-
logger.info(`Starting download from URL: ${url}`);
38-
const writer = fs.createWriteStream(outputPath);
39-
33+
34+
public async request(url: string, method: string, data: any): Promise<AxiosResponse<any, any>> {
35+
logger.info(`Sending ${method} request to URL: ${url}`);
36+
if (this.axiosConfig.proxy) {
37+
logger.info(
38+
`Using proxy - Host: ${this.axiosConfig.proxy.host}, Port: ${this.axiosConfig.proxy.port},` +
39+
`Protocol: ${this.axiosConfig.proxy.protocol}, Auth: ${this.axiosConfig.proxy.auth ? 'Yes' : 'No'}`
40+
);
41+
}
4042
try {
41-
if (this.axiosConfig.proxy) {
42-
logger.info(
43-
`Using proxy - Host: ${this.axiosConfig.proxy.host}, Port: ${this.axiosConfig.proxy.port},` +
44-
`Protocol: ${this.axiosConfig.proxy.protocol}, Auth: ${this.axiosConfig.proxy.auth ? 'Yes' : 'No'}`
45-
);
46-
}
47-
const response = await axios({...this.axiosConfig, url});
48-
response.data.pipe(writer);
49-
await finished(writer);
50-
logger.info(`Download completed successfully. File saved to: ${outputPath}`);
43+
const response = await axios({...this.axiosConfig, url, method, data});
44+
logger.info(`Request completed successfully.`);
45+
return response;
5146
} catch (error) {
52-
logger.error(`Error downloading file from ${url}: ${error.message || error}`);
47+
logger.error(`Error sending ${method} request to ${url}: ${error.message || error}`);
5348
throw error;
54-
} finally {
55-
writer.close();
56-
logger.info('Write stream closed.');
5749
}
5850
}
5951
}

src/main/osinstaller/CxInstaller.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as path from 'path';
44
import * as tar from 'tar';
55
import * as unzipper from 'unzipper';
66
import {logger} from "../wrapper/loggerConfig";
7-
import {Client} from "../client/Client";
7+
import {AstClient} from "../client/AstClient";
88

99
const linuxOS = 'linux';
1010
const macOS = 'darwin';
@@ -22,15 +22,15 @@ export class CxInstaller {
2222
private readonly resourceDirPath: string;
2323
private readonly installedCLIVersionFileName = 'cli-version';
2424
private readonly cliDefaultVersion = '2.2.5'; // Update this with the latest version.
25-
private readonly client: Client;
25+
private readonly client: AstClient;
2626

2727
private static readonly PLATFORMS: Record<SupportedPlatforms, PlatformData> = {
2828
win32: { platform: 'windows', extension: 'zip' },
2929
darwin: { platform: macOS, extension: 'tar.gz' },
3030
linux: { platform: linuxOS, extension: 'tar.gz' }
3131
};
3232

33-
constructor(platform: string, client: Client) {
33+
constructor(platform: string, client: AstClient) {
3434
this.platform = platform as SupportedPlatforms;
3535
this.resourceDirPath = path.join(__dirname, '../wrapper/resources');
3636
this.client = client;

src/main/wrapper/CxWrapper.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import CxBFL from "../bfl/CxBFL";
99
import {CxInstaller} from "../osinstaller/CxInstaller";
1010
import {Semaphore} from "async-mutex";
1111
import {HttpClient} from "../client/HttpClient";
12+
import {AstClient} from "../client/AstClient";
1213

1314

1415
type ParamTypeMap = Map<CxParamType, string>;
@@ -19,7 +20,7 @@ export class CxWrapper {
1920
config: CxConfig;
2021
cxInstaller: CxInstaller;
2122
private constructor(cxScanConfig: CxConfig, logFilePath?: string) {
22-
this.cxInstaller = new CxInstaller(process.platform, new HttpClient());
23+
this.cxInstaller = new CxInstaller(process.platform, new AstClient(new HttpClient()));
2324
this.config = new CxConfig();
2425
getLoggerWithFilePath(logFilePath)
2526
if (cxScanConfig.apiKey) {

src/tests/CxInstallerTest.test.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { CxInstaller } from "../main/osinstaller/CxInstaller";
22
import { anyString, mock, instance, when, verify } from "ts-mockito";
3-
import { HttpClient } from "../main/client/HttpClient";
3+
import { AstClient } from "../main/client/AstClient";
44

5-
const clientMock = mock(HttpClient);
6-
const clientMockInstance = instance(clientMock);
5+
// Mock AstClient and set up an instance from it
6+
const astClientMock = mock(AstClient);
7+
const astClientInstance = instance(astClientMock);
78

8-
const cxInstallerLinux = new CxInstaller("linux", clientMockInstance);
9-
const cxInstallerMac = new CxInstaller("darwin", clientMockInstance);
10-
const cxInstallerWindows = new CxInstaller("win32", clientMockInstance);
9+
// Create CxInstaller instances with the mocked AstClient
10+
const cxInstallerLinux = new CxInstaller("linux", astClientInstance);
11+
const cxInstallerMac = new CxInstaller("darwin", astClientInstance);
12+
const cxInstallerWindows = new CxInstaller("win32", astClientInstance);
1113

1214
describe("CxInstaller cases", () => {
1315
it('CxInstaller getDownloadURL Linux Successful case', async () => {
@@ -48,16 +50,12 @@ describe("CxInstaller getExecutablePath cases", () => {
4850

4951
describe("CxInstaller checkExecutableExists cases", () => {
5052
beforeAll(async () => {
51-
// Set up mock behavior
52-
when(clientMock.downloadFile(anyString(), anyString())).thenResolve();
53-
54-
// Trigger the download
53+
when(astClientMock.downloadFile(anyString(), anyString())).thenResolve(); // Set up mock behavior here
5554
await cxInstallerWindows.downloadIfNotInstalledCLI();
5655
});
5756

5857
it('CxInstaller checkExecutableExists Windows Successful case', () => {
59-
// Verify if downloadFile was called with the expected arguments
60-
verify(clientMock.downloadFile(anyString(), anyString())).called();
58+
verify(astClientMock.downloadFile(anyString(), anyString())).called();
6159
});
6260
});
6361

@@ -72,4 +70,4 @@ function getArchitecture(platform: string): string {
7270
};
7371

7472
return archMap[process.arch] || 'x64';
75-
}
73+
}

0 commit comments

Comments
 (0)