-
Notifications
You must be signed in to change notification settings - Fork 0
/
aria2xml.js
22 lines (22 loc) · 946 Bytes
/
aria2xml.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Aria2XMLRequest {
constructor (...args) {
let path = args.join('#').match(/^(https?:\/\/[^#]+)#?(.*)$/);
if (!path) { throw new Error('Invalid JSON-RPC entry: "' + args.join('", "') + '"'); }
this.jsonrpc = path[1];
this.secret = path[2];
this.params = this.secret ? ['token:' + this.secret] : [];
}
version = '0.4.0';
get (...args) {
return fetch(this.jsonrpc + '?params=' + btoa( unescape( encodeURIComponent(this.json(args)) ) )).then(this.result);
}
post (...args) {
return fetch(this.jsonrpc, {method: 'POST', body: this.json(args)}).then(this.result);
}
result (response) {
if (response.ok) { return response.json(); } throw new Error(response.statusText);
}
json (args) {
return JSON.stringify( args.map( ({ method, params = [] }) => ({ id: '', jsonrpc: '2.0', method, params: [...this.params, ...params] }) ) );
}
}