Skip to content

Commit b51f7a0

Browse files
improve upload handling
1 parent f81f08b commit b51f7a0

File tree

8 files changed

+546
-168
lines changed

8 files changed

+546
-168
lines changed

src/providers/espresso.ts

Lines changed: 17 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@ import logger from '../logger';
33
import Credentials from '../models/credentials';
44
import axios from 'axios';
55
import fs from 'node:fs';
6-
import path from 'node:path';
7-
import FormData from 'form-data';
86
import TestingBotError from '../models/testingbot_error';
97
import utils from '../utils';
8+
import Upload from '../upload';
109

1110
export default class Espresso {
1211
private readonly URL = 'https://api.testingbot.com/v1/app-automate/espresso';
1312
private credentials: Credentials;
1413
private options: EspressoOptions;
14+
private upload: Upload;
1515

1616
private appId: number | undefined = undefined;
1717

1818
public constructor(credentials: Credentials, options: EspressoOptions) {
1919
this.credentials = credentials;
2020
this.options = options;
21+
this.upload = new Upload();
2122
}
2223

2324
private async validate(): Promise<boolean> {
@@ -74,59 +75,26 @@ export default class Espresso {
7475
}
7576

7677
private async uploadApp() {
77-
const fileName = path.basename(this.options.app);
78-
const fileStream = fs.createReadStream(this.options.app);
79-
80-
const formData = new FormData();
81-
formData.append('file', fileStream);
82-
const response = await axios.post(`${this.URL}/app`, formData, {
83-
headers: {
84-
'Content-Type': 'application/vnd.android.package-archive',
85-
'Content-Disposition': `attachment; filename=${fileName}`,
86-
'User-Agent': utils.getUserAgent(),
87-
},
88-
auth: {
89-
username: this.credentials.userName,
90-
password: this.credentials.accessKey,
91-
},
78+
const result = await this.upload.upload({
79+
filePath: this.options.app,
80+
url: `${this.URL}/app`,
81+
credentials: this.credentials,
82+
contentType: 'application/vnd.android.package-archive',
83+
showProgress: true,
9284
});
9385

94-
const result = response.data;
95-
if (result.id) {
96-
this.appId = result.id;
97-
} else {
98-
throw new TestingBotError(`Uploading app failed: ${result.error}`);
99-
}
100-
86+
this.appId = result.id;
10187
return true;
10288
}
10389

10490
private async uploadTestApp() {
105-
const fileName = path.basename(this.options.testApp);
106-
const fileStream = fs.createReadStream(this.options.testApp);
107-
108-
const formData = new FormData();
109-
formData.append('file', fileStream);
110-
const response = await axios.post(
111-
`${this.URL}/${this.appId}/tests`,
112-
formData,
113-
{
114-
headers: {
115-
'Content-Type': 'application/zip',
116-
'Content-Disposition': `attachment; filename=${fileName}`,
117-
'User-Agent': utils.getUserAgent(),
118-
},
119-
auth: {
120-
username: this.credentials.userName,
121-
password: this.credentials.accessKey,
122-
},
123-
},
124-
);
125-
126-
const result = response.data;
127-
if (!result.id) {
128-
throw new TestingBotError(`Uploading test app failed: ${result.error}`);
129-
}
91+
await this.upload.upload({
92+
filePath: this.options.testApp,
93+
url: `${this.URL}/${this.appId}/tests`,
94+
credentials: this.credentials,
95+
contentType: 'application/zip',
96+
showProgress: true,
97+
});
13098

13199
return true;
132100
}

src/providers/maestro.ts

Lines changed: 17 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@ import logger from '../logger';
33
import Credentials from '../models/credentials';
44
import axios from 'axios';
55
import fs from 'node:fs';
6-
import path from 'node:path';
7-
import FormData from 'form-data';
86
import TestingBotError from '../models/testingbot_error';
97
import utils from '../utils';
8+
import Upload from '../upload';
109

1110
export default class Maestro {
1211
private readonly URL = 'https://api.testingbot.com/v1/app-automate/maestro';
1312
private credentials: Credentials;
1413
private options: MaestroOptions;
14+
private upload: Upload;
1515

1616
private appId: number | undefined = undefined;
1717

1818
public constructor(credentials: Credentials, options: MaestroOptions) {
1919
this.credentials = credentials;
2020
this.options = options;
21+
this.upload = new Upload();
2122
}
2223

2324
private async validate(): Promise<boolean> {
@@ -74,59 +75,26 @@ export default class Maestro {
7475
}
7576

7677
private async uploadApp() {
77-
const fileName = path.basename(this.options.app);
78-
const fileStream = fs.createReadStream(this.options.app);
79-
80-
const formData = new FormData();
81-
formData.append('file', fileStream);
82-
const response = await axios.post(`${this.URL}/app`, formData, {
83-
headers: {
84-
'Content-Type': 'application/vnd.android.package-archive',
85-
'Content-Disposition': `attachment; filename=${fileName}`,
86-
'User-Agent': utils.getUserAgent(),
87-
},
88-
auth: {
89-
username: this.credentials.userName,
90-
password: this.credentials.accessKey,
91-
},
78+
const result = await this.upload.upload({
79+
filePath: this.options.app,
80+
url: `${this.URL}/app`,
81+
credentials: this.credentials,
82+
contentType: 'application/vnd.android.package-archive',
83+
showProgress: true,
9284
});
9385

94-
const result = response.data;
95-
if (result.id) {
96-
this.appId = result.id;
97-
} else {
98-
throw new TestingBotError(`Uploading app failed: ${result.error}`);
99-
}
100-
86+
this.appId = result.id;
10187
return true;
10288
}
10389

10490
private async uploadTestApp() {
105-
const fileName = path.basename(this.options.testApp);
106-
const fileStream = fs.createReadStream(this.options.testApp);
107-
108-
const formData = new FormData();
109-
formData.append('file', fileStream);
110-
const response = await axios.post(
111-
`${this.URL}/${this.appId}/tests`,
112-
formData,
113-
{
114-
headers: {
115-
'Content-Type': 'application/zip',
116-
'Content-Disposition': `attachment; filename=${fileName}`,
117-
'User-Agent': utils.getUserAgent(),
118-
},
119-
auth: {
120-
username: this.credentials.userName,
121-
password: this.credentials.accessKey,
122-
},
123-
},
124-
);
125-
126-
const result = response.data;
127-
if (!result.id) {
128-
throw new TestingBotError(`Uploading test app failed: ${result.error}`);
129-
}
91+
await this.upload.upload({
92+
filePath: this.options.testApp,
93+
url: `${this.URL}/${this.appId}/tests`,
94+
credentials: this.credentials,
95+
contentType: 'application/zip',
96+
showProgress: true,
97+
});
13098

13199
return true;
132100
}

src/providers/xcuitest.ts

Lines changed: 17 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,23 @@ import logger from '../logger';
22
import Credentials from '../models/credentials';
33
import axios from 'axios';
44
import fs from 'node:fs';
5-
import path from 'node:path';
6-
import FormData from 'form-data';
75
import TestingBotError from '../models/testingbot_error';
86
import XCUITestOptions from '../models/xcuitest_options';
97
import utils from '../utils';
8+
import Upload from '../upload';
109

1110
export default class XCUITest {
1211
private readonly URL = 'https://api.testingbot.com/v1/app-automate/xcuitest';
1312
private credentials: Credentials;
1413
private options: XCUITestOptions;
14+
private upload: Upload;
1515

1616
private appId: number | undefined = undefined;
1717

1818
public constructor(credentials: Credentials, options: XCUITestOptions) {
1919
this.credentials = credentials;
2020
this.options = options;
21+
this.upload = new Upload();
2122
}
2223

2324
private async validate(): Promise<boolean> {
@@ -70,59 +71,26 @@ export default class XCUITest {
7071
}
7172

7273
private async uploadApp() {
73-
const fileName = path.basename(this.options.app);
74-
const fileStream = fs.createReadStream(this.options.app);
75-
76-
const formData = new FormData();
77-
formData.append('file', fileStream);
78-
const response = await axios.post(`${this.URL}/app`, formData, {
79-
headers: {
80-
'Content-Type': 'application/octet-stream',
81-
'Content-Disposition': `attachment; filename=${fileName}`,
82-
'User-Agent': utils.getUserAgent(),
83-
},
84-
auth: {
85-
username: this.credentials.userName,
86-
password: this.credentials.accessKey,
87-
},
74+
const result = await this.upload.upload({
75+
filePath: this.options.app,
76+
url: `${this.URL}/app`,
77+
credentials: this.credentials,
78+
contentType: 'application/octet-stream',
79+
showProgress: true,
8880
});
8981

90-
const result = response.data;
91-
if (result.id) {
92-
this.appId = result.id;
93-
} else {
94-
throw new TestingBotError(`Uploading app failed: ${result.error}`);
95-
}
96-
82+
this.appId = result.id;
9783
return true;
9884
}
9985

10086
private async uploadTestApp() {
101-
const fileName = path.basename(this.options.testApp);
102-
const fileStream = fs.createReadStream(this.options.testApp);
103-
104-
const formData = new FormData();
105-
formData.append('file', fileStream);
106-
const response = await axios.post(
107-
`${this.URL}/${this.appId}/tests`,
108-
formData,
109-
{
110-
headers: {
111-
'Content-Type': 'application/zip',
112-
'Content-Disposition': `attachment; filename=${fileName}`,
113-
'User-Agent': utils.getUserAgent(),
114-
},
115-
auth: {
116-
username: this.credentials.userName,
117-
password: this.credentials.accessKey,
118-
},
119-
},
120-
);
121-
122-
const result = response.data;
123-
if (!result.id) {
124-
throw new TestingBotError(`Uploading test app failed: ${result.error}`);
125-
}
87+
await this.upload.upload({
88+
filePath: this.options.testApp,
89+
url: `${this.URL}/${this.appId}/tests`,
90+
credentials: this.credentials,
91+
contentType: 'application/zip',
92+
showProgress: true,
93+
});
12694

12795
return true;
12896
}

0 commit comments

Comments
 (0)