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: 9999,
|
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 | return [
|
30 | {
|
31 | name: "subscribe",
|
32 | rpc: async (apikey, coin, account, callback) => { return await this.subscribe(apikey, coin, account, callback); },
|
33 | type: 'hook'
|
34 | }, {
|
35 | name: 'unsubscribe',
|
36 | rpc: async (apikey, uid) => { return await this.unsubscribe(apikey, uid); },
|
37 | type: 'unhook'
|
38 | }
|
39 | ];
|
40 | }
|
41 | start() {
|
42 | if (this.started) {
|
43 | logger.warn("FrontblockApiClient has already been started. Ignoring");
|
44 | return;
|
45 | }
|
46 | this.socket = bsock.connect(this.conf.apiPort, this.conf.apiHost, this.conf.tls != null ? this.conf.tls : false);
|
47 | this.started = true;
|
48 | }
|
49 | stop() {
|
50 | if (!this.started) {
|
51 | logger.warn("FrontblockApiClient has not been started. Ignoring");
|
52 | return;
|
53 | }
|
54 | this.socket.close();
|
55 | }
|
56 | async subscribe(apikey, coin, account, callback) {
|
57 | const r = await this.socket.call("subscribe", apikey, coin, account);
|
58 | const res = Service_1.parseSubResponse(r);
|
59 | if (res instanceof Service_1.SubscriptionResponse) {
|
60 | this.socket.hook(res.uid, callback);
|
61 | }
|
62 | return res;
|
63 | }
|
64 | async unsubscribe(apikey, uid) {
|
65 | const r = await this.socket.call('unsubscribe', apikey, uid);
|
66 | const res = Service_1.parseResponse(r);
|
67 | if (res instanceof Service_1.SuccessResponse)
|
68 | this.socket.unhook(uid);
|
69 | return res;
|
70 | }
|
71 | }
|
72 | exports.default = FrontblockApiClient;
|