DRY @ LokinetApiClient
This commit is contained in:
parent
9fa67d02ea
commit
fce787681f
|
@ -20,10 +20,10 @@ class HttpClient : public QObject
|
|||
Q_OBJECT
|
||||
Q_DISABLE_COPY(HttpClient);
|
||||
|
||||
using ReplyCallback = std::function<void(QNetworkReply*)>;
|
||||
|
||||
public:
|
||||
|
||||
using ReplyCallback = std::function<void(QNetworkReply*)>;
|
||||
|
||||
HttpClient();
|
||||
~HttpClient();
|
||||
|
||||
|
|
|
@ -1,75 +1,45 @@
|
|||
#include "LokinetApiClient.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <cstdio>
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
|
|
@ -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:
|
||||
|
||||
|
|
Loading…
Reference in New Issue