UNPKG

2.77 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const Service_1 = require("frontblock-generic/Service");
4const Logger = require("log4js");
5const logger = Logger.getLogger(); // logs to STDOUT
6logger.level = 'debug';
7const bsock = require('bsock');
8/**
9 * Frontblock api connection lib
10 *
11 * Can be used solo and also is a valid frontblock plugin on the customer-side
12 */
13class FrontblockApiClient {
14 constructor(conf) {
15 this.apikey = "";
16 this.started = false;
17 if (conf == null) {
18 conf = {
19 apiHost: "localhost",
20 apiKey: "",
21 apiPort: 10000,
22 tls: false
23 };
24 this.conf = conf;
25 }
26 else {
27 if (conf.apiKey != null)
28 this.apikey = conf.apiKey;
29 this.conf = conf;
30 }
31 }
32 exportRPCs() {
33 const unhookFn = async (uid) => { return await this.unsubscribe(this.apikey, uid); };
34 return [
35 {
36 name: "subscribe",
37 rpc: async (coin, account, callback) => { return await this.subscribe(this.apikey, coin, account, callback); },
38 type: 'hook',
39 unhook: unhookFn,
40 visibility: 'private'
41 }, {
42 name: 'unsubscribe',
43 rpc: unhookFn,
44 type: 'unhook',
45 visibility: 'private'
46 }
47 ];
48 }
49 start() {
50 if (this.started) {
51 logger.warn("FrontblockApiClient has already been started. Ignoring");
52 return;
53 }
54 logger.info("Starting apiClient with ", this.conf);
55 this.socket = bsock.connect(this.conf.apiPort, this.conf.apiHost, this.conf.tls != null ? this.conf.tls : false);
56 this.started = true;
57 }
58 stop() {
59 if (!this.started) {
60 logger.warn("FrontblockApiClient has not been started. Ignoring");
61 return;
62 }
63 this.socket.close();
64 }
65 async subscribe(apikey, coin, account, callback) {
66 const r = await this.socket.call("subscribe", apikey, coin, account);
67 const res = Service_1.parseSubResponse(r);
68 if (res instanceof Service_1.SubscriptionResponse) {
69 this.socket.hook(res.uid, callback);
70 }
71 return res;
72 }
73 async unsubscribe(apikey, uid) {
74 const r = await this.socket.call('unsubscribe', apikey, uid);
75 const res = Service_1.parseResponse(r);
76 if (res instanceof Service_1.SuccessResponse)
77 this.socket.unhook(uid);
78 return res;
79 }
80 async getPluginList() {
81 return await this.socket.call('getPluginList');
82 }
83}
84exports.default = FrontblockApiClient;