matrix-dimension/src-ts/scalar/ScalarClient.ts
Travis Ralston 826364e803 Re-implement the Scalar API in typescript
This is part of a rewrite for Dimension to better support integrations. Only the bare minimum scalar APIs are implemented at this point - dimension is non-functional.
2017-12-17 19:22:13 -07:00

32 lines
1.2 KiB
TypeScript

import { OpenId } from "../models/OpenId";
import { ScalarRegisterResponse } from "../models/ScalarResponses";
import * as Promise from "bluebird";
import * as request from "request";
import { LogService } from "matrix-js-snippets";
import Upstream from "../db/models/Upstream";
export class ScalarClient {
constructor(private upstream: Upstream) {
}
public register(openId: OpenId): Promise<ScalarRegisterResponse> {
return new Promise((resolve, reject) => {
request({
method: "POST",
url: this.upstream.scalarUrl + "/register",
json: openId,
}, (err, res, _body) => {
if (err) {
LogService.error("ScalarClient", "Error registering for token");
LogService.error("ScalarClient", err);
reject(err);
} else if (res.statusCode !== 200) {
LogService.error("ScalarClient", "Got status code " + res.statusCode + " while registering for token");
reject(new Error("Could not get token"));
} else {
resolve(res.body);
}
});
});
}
}