Skip to content

Commit 7b0fc3f

Browse files
author
Tomas Rutkauskas
authored
Merge pull request #77 from ConvertAPI/feature/keepalive
Set keepalive for all connections
2 parents a7bf844 + f1e4c3e commit 7b0fc3f

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

src/client.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,47 +13,41 @@ export default class Client {
1313
'Content-Type': 'application/x-www-form-urlencoded',
1414
Accept: 'application/json',
1515
};
16+
this.httpsAgent = new https.Agent({ keepAlive: api.keepAlive });
1617
}
1718

1819
get(path, params = {}, timeout = null) {
19-
const options = {
20+
const options = this.buildOptions({
2021
method: 'get',
2122
url: this.url(path),
22-
headers: this.defaultHeader,
2323
params,
2424
timeout: timeout * 1000,
25-
proxy: this.api.proxy,
26-
};
25+
});
2726

2827
return axios(options)
2928
.then(response => response.data)
3029
.catch(error => Client.handleError(error));
3130
}
3231

3332
post(path, params, timeout = null) {
34-
const options = {
33+
const options = this.buildOptions({
3534
method: 'post',
3635
url: this.url(path),
37-
headers: this.defaultHeader,
3836
data: buildQueryString(params),
39-
maxContentLength: Infinity,
40-
maxBodyLength: Infinity,
4137
timeout: timeout * 1000,
42-
proxy: this.api.proxy,
43-
};
38+
});
4439

4540
return axios(options)
4641
.then(response => response.data)
4742
.catch(error => Client.handleError(error));
4843
}
4944

5045
async download(url, path) {
51-
const options = {
46+
const options = this.buildOptions({
5247
url,
5348
timeout: this.api.downloadTimeout * 1000,
54-
proxy: this.api.proxy,
5549
responseType: 'stream',
56-
};
50+
});
5751

5852
const response = await axios(options)
5953
.catch(error => Client.handleError(error));
@@ -85,17 +79,13 @@ export default class Client {
8579
'Content-Disposition': `attachment; filename*=UTF-8''${encodedFileName}`
8680
}, this.defaultHeader);
8781

88-
const options = {
82+
const options = this.buildOptions({
8983
method: 'post',
9084
url: this.url('upload'),
9185
headers,
9286
data: stream,
93-
maxContentLength: Infinity,
94-
maxBodyLength: Infinity,
9587
timeout: this.api.uploadTimeout * 1000,
96-
proxy: this.api.proxy,
97-
httpsAgent: new https.Agent({ keepAlive: this.api.keepAlive }),
98-
};
88+
});
9989

10090
return axios(options)
10191
.then(response => new UploadResult(response.data))
@@ -106,6 +96,16 @@ export default class Client {
10696
return `${this.api.baseUri}${path}?secret=${this.api.secret}`;
10797
}
10898

99+
buildOptions(options) {
100+
return Object.assign({
101+
headers: this.defaultHeader,
102+
maxContentLength: Infinity,
103+
maxBodyLength: Infinity,
104+
proxy: this.api.proxy,
105+
httpsAgent: this.httpsAgent,
106+
}, options);
107+
}
108+
109109
static handleError(error) {
110110
let data;
111111

0 commit comments

Comments
 (0)