// @flow import JsonRpcWs, { InvalidReply } from './jsonrpc-ws-ipc'; import { object, string, number, arrayOf } from 'validated/schema'; import { validate } from 'validated/object'; export type AccountData = {paid_until: string}; export type AccountNumber = string; export type Ip = string; export type Location = { latlong: Array, country: string, city: string, }; const LocationSchema = object({ latlong: arrayOf(number), country: string, city: string, }); export interface IpcFacade { getAccountData(AccountNumber): Promise, setAccount(accountNumber: AccountNumber): Promise, setCountry(address: string): Promise, connect(): Promise, disconnect(): Promise, getIp(): Promise, getLocation(): Promise, } export class RealIpc implements IpcFacade { _ipc: JsonRpcWs; constructor(connectionString: string) { this._ipc = new JsonRpcWs(connectionString); } getAccountData(accountNumber: AccountNumber): Promise { return this._ipc.send('get_account_data', accountNumber) .then(raw => { if (typeof raw === 'object' && raw && raw.paid_until) { return raw; } else { throw new InvalidReply(raw); } }); } setAccount(accountNumber: AccountNumber): Promise { return this._ipc.send('set_account', accountNumber) .then(this._ignoreResponse); } _ignoreResponse(_response: mixed): void { return; } setCountry(address: string): Promise { return this._ipc.send('set_country', address) .then(this._ignoreResponse); } connect(): Promise { return this._ipc.send('connect') .then(this._ignoreResponse); } disconnect(): Promise { return this._ipc.send('disconnect') .then(this._ignoreResponse); } getIp(): Promise { return this._ipc.send('get_ip') .then(raw => { if (typeof raw === 'string' && raw) { return raw; } else { throw new InvalidReply(raw); } }); } getLocation(): Promise { return this._ipc.send('get_location') .then(raw => { try { return validate(LocationSchema, raw); } catch (e) { throw new InvalidReply(raw, e); } }); } }