diff --git a/src/lib/api/public/ApiUtils.h b/src/lib/api/public/ApiUtils.h index 30ca5171d..4634b102c 100644 --- a/src/lib/api/public/ApiUtils.h +++ b/src/lib/api/public/ApiUtils.h @@ -7,6 +7,17 @@ namespace bts { namespace api { + template + const std::string toJSON(const T& t) { + google::protobuf::util::JsonPrintOptions options; + options.add_whitespace = false; + options.always_print_primitive_fields = true; + std::string result; + auto status = google::protobuf::util::MessageToJsonString(t, &result, options); + if (status.ok()) return result; + else return ""; + } + template const std::string format_qualifier(const char* type_name, const T& t) { google::protobuf::util::JsonPrintOptions options; diff --git a/src/server/sparkd/http/ApiGateway.cpp b/src/server/sparkd/http/ApiGateway.cpp index a30f5b6a4..27f465aa9 100644 --- a/src/server/sparkd/http/ApiGateway.cpp +++ b/src/server/sparkd/http/ApiGateway.cpp @@ -1,10 +1,12 @@ #include "ApiGateway.h" #include "lib/logging/logging.h" +#include "lib/api/public/ApiUtils.h" #include #include #include + using namespace bts::api::gateway; namespace bts { @@ -52,6 +54,7 @@ ApiGatewaySession::~ApiGatewaySession(){} const GatewayResponse ApiGatewaySession::handle(const api::gateway::GatewayRequest& request) { GatewayResponse response; + LOG(Api,Info, "Gateway Request: {}", api::toJSON(request)); switch(request.request_case()) { case GatewayRequest::kLogin: _me->handle_login(request.login(), response.mutable_logon()); diff --git a/src/web/api/__tests__/ServiceContext.test.ts b/src/web/api/__tests__/ServiceContext.test.ts index 269dbbc1e..c86270929 100644 --- a/src/web/api/__tests__/ServiceContext.test.ts +++ b/src/web/api/__tests__/ServiceContext.test.ts @@ -3,10 +3,6 @@ import {root} from 'proto' import { connect, ServiceContext } from '../src/ServiceContext' import { HelloService } from '../src/HelloService' -// Fix websocket for running in js -// import WebSocket from 'ws' -// global["WebSocket"] = WebSocket - test('log in and say hi', async () => { let ctx = await connect("bts", "password"); expect(ctx instanceof ServiceContext).toBe(true) diff --git a/src/web/api/build.cmd b/src/web/api/build.cmd index f35c8fe47..1219d9b3b 100644 --- a/src/web/api/build.cmd +++ b/src/web/api/build.cmd @@ -2,7 +2,6 @@ setlocal cd %~dp0 set build=%~dp0/../../../build/web/api -del %dp0\..\..\..\buildlweb\api\ServiceContext.d.ts :: run protobuf generator on .proto files call npm run protogen :: build tyepscript diff --git a/src/web/api/jest.config.js b/src/web/api/jest.config.js index 8ff85d50a..8f37480ac 100644 --- a/src/web/api/jest.config.js +++ b/src/web/api/jest.config.js @@ -1,5 +1,4 @@ module.exports = { - // testEnvironment: 'node', transform: { "^.+\\.tsx?$": "ts-jest" }, diff --git a/src/web/api/src/ServiceContext.ts b/src/web/api/src/ServiceContext.ts index ecd040b6b..3405f72f2 100644 --- a/src/web/api/src/ServiceContext.ts +++ b/src/web/api/src/ServiceContext.ts @@ -32,7 +32,8 @@ class PrivateContext { this.url = url || default_ws_uri() this.username = username this.password = password - this.sc = new ServiceContext(this.sendgw) + // If you use just 'this.sengw' this breaks because it uses the wrong 'this' + this.sc = new ServiceContext(m => this.sendgw(m)) } connect() : Promise { @@ -57,9 +58,9 @@ class PrivateContext { console.log("Reply: " + JSON.stringify(response)) if (response.logon.isValid) { // hooking onclose triggers auto-reconnect - this.ws.onclose = this.onclose + this.ws.onclose = e => this.onclose(e) // Switch to steady state onmessage - this.ws.onmessage = this.onmessage + this.ws.onmessage = e => this.onmessage(e) console.log("Authenticated") this.sc.token = response.logon.token resolve(this.sc) @@ -88,7 +89,9 @@ class PrivateContext { onmessage(e){ const buffer = new Uint8Array(e.data) const response = GatewayResponse.decode(buffer) - this.sc.on_msg(response.result, response.addendum) + if (response) { + this.sc.on_msg(response.result, response.addendum) + } } } @@ -101,6 +104,7 @@ export class ServiceContext { constructor(send : (msg)=>void) { this.next_id = 1000 this.send = send + this.outstanding = {} } call(service_id : string, method_id : number, body : Uint8Array) : Promise { @@ -125,6 +129,7 @@ export class ServiceContext { subscribe() {/*not impl*/} on_msg(header: {request_id:number, code: number}, body : Uint8Array) { + console.log("message received: " + JSON.stringify(header)); if (header.request_id in this.outstanding) { const v = this.outstanding[header.request_id] delete this.outstanding[header.request_id] diff --git a/src/web/api/src/bts_assert.ts b/src/web/api/src/bts_assert.ts index 0dbac3c12..970b089e9 100644 --- a/src/web/api/src/bts_assert.ts +++ b/src/web/api/src/bts_assert.ts @@ -7,7 +7,6 @@ export function assert(condition:boolean, message?:string) { } export function assertValid(mtype, message) { const r = mtype.verify(message) - console.log(r) assert(r == null, r) }