2019-02-04 23:26:24 +01:00
|
|
|
class RequestWrapper {
|
2018-12-21 14:35:24 +01:00
|
|
|
constructor(promise, controller) {
|
|
|
|
this._promise = promise;
|
|
|
|
this._controller = controller;
|
|
|
|
}
|
|
|
|
|
|
|
|
abort() {
|
|
|
|
this._controller.abort();
|
|
|
|
}
|
|
|
|
|
2019-02-04 23:26:24 +01:00
|
|
|
response() {
|
2018-12-21 14:35:24 +01:00
|
|
|
return this._promise;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 01:25:12 +01:00
|
|
|
export default class HomeServerApi {
|
2018-12-21 14:35:24 +01:00
|
|
|
constructor(homeserver, accessToken) {
|
|
|
|
this._homeserver = homeserver;
|
|
|
|
this._accessToken = accessToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
_url(csPath) {
|
2019-02-04 23:26:24 +01:00
|
|
|
return `${this._homeserver}/_matrix/client/r0${csPath}`;
|
2018-12-21 14:35:24 +01:00
|
|
|
}
|
|
|
|
|
2019-02-04 23:26:24 +01:00
|
|
|
_request(method, csPath, queryParams = {}, body) {
|
2018-12-21 14:35:24 +01:00
|
|
|
const queryString = Object.entries(queryParams)
|
|
|
|
.filter(([name, value]) => value !== undefined)
|
2019-02-04 23:26:24 +01:00
|
|
|
.map(([name, value]) => `${encodeURIComponent(name)}=${encodeURIComponent(value)}`)
|
2018-12-21 14:35:24 +01:00
|
|
|
.join("&");
|
|
|
|
const url = this._url(`${csPath}?${queryString}`);
|
2019-02-04 23:26:24 +01:00
|
|
|
let bodyString;
|
|
|
|
const headers = new Headers();
|
|
|
|
if (this._accessToken) {
|
|
|
|
headers.append("Authorization", `Bearer ${this._accessToken}`);
|
|
|
|
}
|
2018-12-21 14:35:24 +01:00
|
|
|
headers.append("Accept", "application/json");
|
2019-02-04 23:26:24 +01:00
|
|
|
if (body) {
|
2018-12-21 14:35:24 +01:00
|
|
|
headers.append("Content-Type", "application/json");
|
2019-02-04 23:26:24 +01:00
|
|
|
bodyString = JSON.stringify(body);
|
2018-12-21 14:35:24 +01:00
|
|
|
}
|
|
|
|
const controller = new AbortController();
|
|
|
|
// TODO: set authenticated headers with second arguments, cache them
|
2019-02-04 23:26:24 +01:00
|
|
|
let promise = fetch(url, {
|
|
|
|
method,
|
|
|
|
headers,
|
|
|
|
body: bodyString,
|
|
|
|
signal: controller.signal
|
|
|
|
});
|
2018-12-21 14:35:24 +01:00
|
|
|
promise = promise.then(response => {
|
|
|
|
if (response.ok) {
|
|
|
|
return response.json();
|
|
|
|
} else {
|
|
|
|
switch (response.status) {
|
|
|
|
default:
|
|
|
|
throw new HomeServerError(response.json())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2019-02-04 23:26:24 +01:00
|
|
|
return new RequestWrapper(promise, controller);
|
|
|
|
}
|
|
|
|
|
|
|
|
_post(csPath, queryParams, body) {
|
|
|
|
return this._request("POST", csPath, queryParams, body);
|
|
|
|
}
|
|
|
|
|
|
|
|
_get(csPath, queryParams, body) {
|
|
|
|
return this._request("GET", csPath, queryParams, body);
|
2018-12-21 14:35:24 +01:00
|
|
|
}
|
|
|
|
|
2019-01-09 11:06:09 +01:00
|
|
|
sync(timeout = 0, since = undefined) {
|
2019-02-04 23:26:24 +01:00
|
|
|
return this._get("/sync", {since, timeout});
|
|
|
|
}
|
|
|
|
|
|
|
|
passwordLogin(username, password) {
|
|
|
|
return this._post("/login", undefined, {
|
|
|
|
"type": "m.login.password",
|
|
|
|
"identifier": {
|
|
|
|
"type": "m.id.user",
|
|
|
|
"user": username
|
|
|
|
},
|
|
|
|
"password": password
|
|
|
|
});
|
2018-12-21 14:35:24 +01:00
|
|
|
}
|
|
|
|
}
|