1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const Service_1 = require("frontblock-generic/Service");
|
4 | const Logger = require("log4js");
|
5 | const logger = Logger.getLogger();
|
6 | logger.level = 'debug';
|
7 | const bsock = require('bsock');
|
8 | class FrontblockApiClient {
|
9 | constructor(conf) {
|
10 | this.apikey = "";
|
11 | this.started = false;
|
12 | if (conf == null) {
|
13 | conf = {
|
14 | apiHost: "localhost",
|
15 | apiKey: "",
|
16 | apiPort: 10000,
|
17 | tls: false
|
18 | };
|
19 | this.conf = conf;
|
20 | }
|
21 | else {
|
22 | if (conf.apiKey != null)
|
23 | this.apikey = conf.apiKey;
|
24 | this.conf = conf;
|
25 | this.start();
|
26 | }
|
27 | }
|
28 | exportRPCs() {
|
29 | const unhookFn = async (uid) => { return await this.unsubscribe(this.apikey, uid); };
|
30 | return [
|
31 | {
|
32 | name: "subscribe",
|
33 | rpc: async (coin, account, callback) => { return await this.subscribe(this.apikey, coin, account, callback); },
|
34 | type: 'hook',
|
35 | unhook: unhookFn
|
36 | }, {
|
37 | name: 'unsubscribe',
|
38 | rpc: unhookFn,
|
39 | type: 'unhook'
|
40 | }
|
41 | ];
|
42 | }
|
43 | start() {
|
44 | if (this.started) {
|
45 | logger.warn("FrontblockApiClient has already been started. Ignoring");
|
46 | return;
|
47 | }
|
48 | this.socket = bsock.connect(this.conf.apiPort, this.conf.apiHost, this.conf.tls != null ? this.conf.tls : false);
|
49 | this.started = true;
|
50 | }
|
51 | stop() {
|
52 | if (!this.started) {
|
53 | logger.warn("FrontblockApiClient has not been started. Ignoring");
|
54 | return;
|
55 | }
|
56 | this.socket.close();
|
57 | }
|
58 | async subscribe(apikey, coin, account, callback) {
|
59 | const r = await this.socket.call("subscribe", apikey, coin, account);
|
60 | const res = Service_1.parseSubResponse(r);
|
61 | if (res instanceof Service_1.SubscriptionResponse) {
|
62 | this.socket.hook(res.uid, callback);
|
63 | }
|
64 | return res;
|
65 | }
|
66 | async unsubscribe(apikey, uid) {
|
67 | const r = await this.socket.call('unsubscribe', apikey, uid);
|
68 | const res = Service_1.parseResponse(r);
|
69 | if (res instanceof Service_1.SuccessResponse)
|
70 | this.socket.unhook(uid);
|
71 | return res;
|
72 | }
|
73 | }
|
74 | exports.default = FrontblockApiClient;
|