-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathclient.js
More file actions
119 lines (98 loc) · 2.84 KB
/
Copy pathclient.js
File metadata and controls
119 lines (98 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import axios from 'axios';
import fs from 'fs';
import https from 'https';
import { buildQueryString, encodeFileName } from './utils';
import UploadResult from './upload_result';
import Error from './error';
export default class Client {
constructor(api) {
this.api = api;
this.defaultHeader = {
'User-Agent': api.userAgent,
'Content-Type': 'application/x-www-form-urlencoded',
Authorization: `Bearer ${api.credentials}`,
Accept: 'application/json',
};
this.httpsAgent = new https.Agent({ keepAlive: api.keepAlive });
}
get(path, params = {}, timeout = null) {
const options = this.buildOptions({
method: 'get',
url: this.url(path),
params,
timeout: timeout * 1000,
});
return axios(options)
.then(response => response.data)
.catch(error => Client.handleError(error));
}
post(path, params, timeout = null) {
const options = this.buildOptions({
method: 'post',
url: this.url(path),
data: buildQueryString(params),
timeout: timeout * 1000,
});
return axios(options)
.then(response => response.data)
.catch(error => Client.handleError(error));
}
async download(url, path) {
const options = this.buildOptions({
url,
timeout: this.api.downloadTimeout * 1000,
responseType: 'stream',
});
const response = await axios(options)
.catch(error => Client.handleError(error));
const writer = fs.createWriteStream(path);
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on('finish', () => {
resolve(path);
});
writer.on('error', (error) => {
reject(new Error(error));
});
response.data.on('error', (error) => {
reject(new Error(error));
});
});
}
upload(stream, fileName) {
const encodedFileName = encodeFileName(fileName);
const headers = Object.assign({
'Content-Type': 'application/octet-stream',
'Content-Disposition': `attachment; filename*=UTF-8''${encodedFileName}`
}, this.defaultHeader);
const options = this.buildOptions({
method: 'post',
url: this.url('upload'),
headers,
data: stream,
timeout: this.api.uploadTimeout * 1000,
});
return axios(options)
.then(response => new UploadResult(response.data))
.catch(error => Client.handleError(error));
}
url(path) {
return `${this.api.baseUri}${path}`;
}
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;
if (error.response && error.response.data) {
({ data } = error.response);
}
throw new Error(error, data);
}
}