Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,41 @@ export default class Client {
'Content-Type': 'application/x-www-form-urlencoded',
Accept: 'application/json',
};
this.httpsAgent = new https.Agent({ keepAlive: api.keepAlive });
}

get(path, params = {}, timeout = null) {
const options = {
const options = this.buildOptions({
method: 'get',
url: this.url(path),
headers: this.defaultHeader,
params,
timeout: timeout * 1000,
proxy: this.api.proxy,
};
});

return axios(options)
.then(response => response.data)
.catch(error => Client.handleError(error));
}

post(path, params, timeout = null) {
const options = {
const options = this.buildOptions({
method: 'post',
url: this.url(path),
headers: this.defaultHeader,
data: buildQueryString(params),
maxContentLength: Infinity,
maxBodyLength: Infinity,
timeout: timeout * 1000,
proxy: this.api.proxy,
};
});

return axios(options)
.then(response => response.data)
.catch(error => Client.handleError(error));
}

async download(url, path) {
const options = {
const options = this.buildOptions({
url,
timeout: this.api.downloadTimeout * 1000,
proxy: this.api.proxy,
responseType: 'stream',
};
});

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

const options = {
const options = this.buildOptions({
method: 'post',
url: this.url('upload'),
headers,
data: stream,
maxContentLength: Infinity,
maxBodyLength: Infinity,
timeout: this.api.uploadTimeout * 1000,
proxy: this.api.proxy,
httpsAgent: new https.Agent({ keepAlive: this.api.keepAlive }),
};
});

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

buildOptions(options) {
return Object.assign({
headers: this.defaultHeader,
maxContentLength: Infinity,
maxBodyLength: Infinity,
proxy: this.api.proxy,
httpsAgent: this.httpsAgent,
}, options);
}

static handleError(error) {
let data;

Expand Down