From fce787681f6fdae409f89e141226a7337a8310bc Mon Sep 17 00:00:00 2001 From: Stephen Shelton Date: Wed, 4 Mar 2020 21:36:35 -0700 Subject: [PATCH] DRY @ LokinetApiClient --- src/HttpClient.hpp | 4 +- src/LokinetApiClient.cpp | 86 +++++++++++++--------------------------- src/LokinetApiClient.hpp | 35 ++++++++++------ 3 files changed, 52 insertions(+), 73 deletions(-) diff --git a/src/HttpClient.hpp b/src/HttpClient.hpp index 22c04a2..a3d6660 100644 --- a/src/HttpClient.hpp +++ b/src/HttpClient.hpp @@ -20,10 +20,10 @@ class HttpClient : public QObject Q_OBJECT Q_DISABLE_COPY(HttpClient); - using ReplyCallback = std::function; - public: + using ReplyCallback = std::function; + HttpClient(); ~HttpClient(); diff --git a/src/LokinetApiClient.cpp b/src/LokinetApiClient.cpp index c06d0f2..e708e6c 100644 --- a/src/LokinetApiClient.cpp +++ b/src/LokinetApiClient.cpp @@ -1,75 +1,45 @@ #include "LokinetApiClient.hpp" #include +#include -// LokinetApiClient::llarpAdminWakeup -bool LokinetApiClient::llarpAdminWakeup(QJSValue callback) { +bool LokinetApiClient::invoke(const std::string& endpoint, HttpClient::ReplyCallback callback) { + + char buffer[1024]; + int result = snprintf( + buffer, + sizeof(buffer), + R"JSON({ + "jsonrpc": "2.0", + "method": "%s", + "params": {}, + "id": "empty" + })JSON", + endpoint.c_str()); + + if (result < 0) { + qDebug() << "snprintf failed: " << result; + return false; + } + + qDebug() << "invoking json rpc payload: " << buffer; + + m_httpClient.postJson("http://localhost:1190", buffer, std::move(callback)); + return true; +} + +Q_INVOKABLE bool LokinetApiClient::invoke(const std::string& endpoint, QJSValue callback) { if (! callback.isUndefined() && ! callback.isCallable()) { qDebug() << "callback should be a function (if present)"; return false; } - const std::string jsonRpcPayload = R"JSON({ - "jsonrpc": "2.0", - "method": "llarp.admin.wakeup", - "params": {}, - "id": "empty" - })JSON"; - m_httpClient.postJson("http://localhost:1190", jsonRpcPayload, [=](QNetworkReply* reply) mutable { + return invoke(std::move(endpoint), [=](QNetworkReply* reply) mutable { QJSValueList args; args << QJSValue(reply->readAll().data()); args << QJSValue(reply->error()); callback.call(args); }); - - return true; } -// LokinetApiClient::llarpVersion -bool LokinetApiClient::llarpVersion(QJSValue callback) { - - if (! callback.isUndefined() && ! callback.isCallable()) { - qDebug() << "callback should be a function (if present)"; - return false; - } - - const std::string jsonRpcPayload = R"JSON({ - "jsonrpc": "2.0", - "method": "llarp.version", - "params": {}, - "id": "empty" - })JSON"; - m_httpClient.postJson("http://localhost:1190", jsonRpcPayload, [=](QNetworkReply* reply) mutable { - QJSValueList args; - args << QJSValue(reply->readAll().data()); - args << QJSValue(reply->error()); - callback.call(args); - }); - - return true; -} - -// LokinetApiClient::llarpAdminStatus -bool LokinetApiClient::llarpAdminStatus(QJSValue callback) { - - if (! callback.isUndefined() && ! callback.isCallable()) { - qDebug() << "callback should be a function (if present)"; - return false; - } - - const std::string jsonRpcPayload = R"JSON({ - "jsonrpc": "2.0", - "method": "llarp.admin.status", - "params": {}, - "id": "empty" - })JSON"; - m_httpClient.postJson("http://localhost:1190", jsonRpcPayload, [=](QNetworkReply* reply) mutable { - QJSValueList args; - args << QJSValue(reply->readAll().data()); - args << QJSValue(reply->error()); - callback.call(args); - }); - - return true; -} diff --git a/src/LokinetApiClient.hpp b/src/LokinetApiClient.hpp index 8181697..8e60bb3 100644 --- a/src/LokinetApiClient.hpp +++ b/src/LokinetApiClient.hpp @@ -32,28 +32,37 @@ class LokinetApiClient : public QObject public: /** - * Invoke the `llarp.admin.wakeup` endpoint. + * Invoke an endpoint. * - * @param callback is an optional JS function to invoke on success - * @return true if the asynchronous request could be made, false otherwise + * TODO: provide argument to be used for JSON-RPC params (if/when needed) + * + * @param endpoint should be the full API endpoint (e.g. "llarp.admin.status") + * to invoke + * @param callback is a callback that will receive the reply or error + * @return true on success, false otherwise */ - Q_INVOKABLE bool llarpAdminWakeup(QJSValue callback); + bool invoke(const std::string& endpoint, HttpClient::ReplyCallback callback); + Q_INVOKABLE bool invoke(const std::string& endpoint, QJSValue callback); /** - * Invoke the `llarp.version` endpoint. + * The following functions are conveniences for invoking particular API + * endpoints. Internally, they delegate to the above 'invoke()' functions. * * @param callback is an optional JS function to invoke on success * @return true if the asynchronous request could be made, false otherwise */ - Q_INVOKABLE bool llarpVersion(QJSValue callback); - /** - * Invoke the `llarp.admin.status` endpoint. - * - * @param callback is an optional JS function to invoke on success - * @return true if the asynchronous request could be made, false otherwise - */ - Q_INVOKABLE bool llarpAdminStatus(QJSValue callback); + Q_INVOKABLE bool llarpAdminWakeup(QJSValue callback) { + return invoke("llarp.admin.wakeup", callback); + } + + Q_INVOKABLE bool llarpVersion(QJSValue callback) { + return invoke("llarp.version", callback); + } + + Q_INVOKABLE bool llarpAdminStatus(QJSValue callback) { + return invoke("llarp.admin.status", callback); + } private: