2018-12-21 14:35:24 +01:00
|
|
|
class Request {
|
|
|
|
constructor(promise, controller) {
|
|
|
|
this._promise = promise;
|
|
|
|
this._controller = controller;
|
|
|
|
}
|
|
|
|
|
|
|
|
abort() {
|
|
|
|
this._controller.abort();
|
|
|
|
}
|
|
|
|
|
2019-01-09 11:06:09 +01:00
|
|
|
get response() {
|
2018-12-21 14:35:24 +01:00
|
|
|
return this._promise;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Network {
|
|
|
|
constructor(homeserver, accessToken) {
|
|
|
|
this._homeserver = homeserver;
|
|
|
|
this._accessToken = accessToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
_url(csPath) {
|
|
|
|
return `${this._homeserver}/_matrix/client/r0/${csPath}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
_request(method, csPath, queryParams = {}) {
|
|
|
|
const queryString = Object.entries(queryParams)
|
|
|
|
.filter(([name, value]) => value !== undefined)
|
|
|
|
.map(([name, value]) => `${encodeURIComponent(name)}=${encodeURIComponent(value)}`);
|
|
|
|
.join("&");
|
|
|
|
const url = this._url(`${csPath}?${queryString}`);
|
|
|
|
const request = new Request(url);
|
|
|
|
const headers = request.headers;
|
|
|
|
headers.append("Authorization", `Bearer ${this._accessToken}`);
|
|
|
|
headers.append("Accept", "application/json");
|
|
|
|
if (false/* body */) {
|
|
|
|
headers.append("Content-Type", "application/json");
|
|
|
|
}
|
|
|
|
const controller = new AbortController();
|
|
|
|
// TODO: set authenticated headers with second arguments, cache them
|
|
|
|
let promise = fetch(request, {signal: controller.signal});
|
|
|
|
promise = promise.then(response => {
|
|
|
|
if (response.ok) {
|
|
|
|
return response.json();
|
|
|
|
} else {
|
|
|
|
switch (response.status) {
|
|
|
|
default:
|
|
|
|
throw new HomeServerError(response.json())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return new Request(promise, controller);
|
|
|
|
}
|
|
|
|
|
2019-01-09 11:06:09 +01:00
|
|
|
sync(timeout = 0, since = undefined) {
|
2018-12-21 14:35:24 +01:00
|
|
|
return this._request("GET", "/sync", {since, timeout});
|
|
|
|
}
|
|
|
|
}
|