2019-10-14 20:47:18 +02:00
|
|
|
#ifndef __LOKI_LOKINET_API_CLIENT_HPP__
|
|
|
|
#define __LOKI_LOKINET_API_CLIENT_HPP__
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QJSValue>
|
2020-08-24 12:44:48 +02:00
|
|
|
#include <QJsonObject>
|
2019-10-14 20:47:18 +02:00
|
|
|
|
2020-08-13 19:48:27 +02:00
|
|
|
#include <lokimq/lokimq.h>
|
|
|
|
#include <optional>
|
2019-10-14 20:47:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A client that implements convenience wrappers around making specific
|
|
|
|
* JSON-RPC requests to a lokinet daemon.
|
|
|
|
*
|
|
|
|
* These are meant to be invoked from QML; as such, they each take a `QJSValue`
|
|
|
|
* object which should be a Javascript callback function. This function should
|
|
|
|
* take two parameters: the message payload and an error, e.g.:
|
|
|
|
*
|
|
|
|
* ```javascript
|
|
|
|
* apiClient.llarpAdminStart(function(response, err) {
|
|
|
|
* if (err) {
|
|
|
|
* console.log("Received error when trying to start lokinet daemon: ", err);
|
|
|
|
* }
|
|
|
|
* });
|
|
|
|
* ```
|
2019-10-21 05:21:37 +02:00
|
|
|
*
|
|
|
|
* TODO: abstract this and just take a string as the endpoint. let QML/Javascript
|
|
|
|
* do the JSON-y things.
|
2019-10-14 20:47:18 +02:00
|
|
|
*/
|
|
|
|
class LokinetApiClient : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2020-08-13 19:48:27 +02:00
|
|
|
using ReplyCallback = std::function<void(std::optional<std::string>)>;
|
|
|
|
|
2019-10-14 20:47:18 +02:00
|
|
|
/**
|
2020-03-05 05:36:35 +01:00
|
|
|
* Invoke an endpoint.
|
2019-10-14 20:47:18 +02:00
|
|
|
*
|
2020-03-05 05:36:35 +01:00
|
|
|
* TODO: provide argument to be used for JSON-RPC params (if/when needed)
|
2019-10-21 03:35:12 +02:00
|
|
|
*
|
2020-03-05 05:36:35 +01:00
|
|
|
* @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
|
2019-10-21 03:35:12 +02:00
|
|
|
*/
|
2020-08-24 12:44:48 +02:00
|
|
|
bool invoke(const std::string& endpoint, QJsonObject args, ReplyCallback callback);
|
|
|
|
Q_INVOKABLE bool invoke(const std::string& endpoint, QJsonObject args ,QJSValue callback);
|
2019-10-21 05:21:37 +02:00
|
|
|
|
|
|
|
/**
|
2020-03-05 05:36:35 +01:00
|
|
|
* The following functions are conveniences for invoking particular API
|
|
|
|
* endpoints. Internally, they delegate to the above 'invoke()' functions.
|
2019-10-21 05:21:37 +02:00
|
|
|
*
|
|
|
|
* @param callback is an optional JS function to invoke on success
|
|
|
|
* @return true if the asynchronous request could be made, false otherwise
|
|
|
|
*/
|
2020-08-24 12:44:48 +02:00
|
|
|
/*
|
|
|
|
Q_INVOKABLE bool llarpAdminWakeup(QJSValue callback) {
|
2020-03-05 05:36:35 +01:00
|
|
|
return invoke("llarp.admin.wakeup", callback);
|
|
|
|
}
|
2020-08-24 12:44:48 +02:00
|
|
|
*/
|
|
|
|
|
2020-03-05 05:36:35 +01:00
|
|
|
Q_INVOKABLE bool llarpVersion(QJSValue callback) {
|
2020-08-24 12:44:48 +02:00
|
|
|
return invoke("llarp.version", QJsonObject{}, callback);
|
2020-03-05 05:36:35 +01:00
|
|
|
}
|
2020-08-24 12:44:48 +02:00
|
|
|
|
2020-08-26 15:17:15 +02:00
|
|
|
Q_INVOKABLE bool llarpAddExit(QString exitAddress, QString exitToken, QJSValue callback)
|
2020-08-24 12:44:48 +02:00
|
|
|
{
|
2020-08-26 15:17:15 +02:00
|
|
|
if(exitToken.isEmpty())
|
|
|
|
{
|
|
|
|
return invoke("llarp.exit", QJsonObject{{"exit", exitAddress}}, callback);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return invoke("llarp.exit", QJsonObject{{"exit", exitAddress}, {"token", exitToken}}, callback);
|
|
|
|
}
|
2020-08-24 12:44:48 +02:00
|
|
|
}
|
2020-08-26 15:17:15 +02:00
|
|
|
|
|
|
|
Q_INVOKABLE bool llarpDelExit(QJSValue callback)
|
2020-08-24 12:44:48 +02:00
|
|
|
{
|
2020-08-26 15:17:15 +02:00
|
|
|
return invoke("llarp.exit", QJsonObject{{"unmap", true}}, callback);
|
2020-03-05 05:36:35 +01:00
|
|
|
}
|
2020-03-05 19:35:40 +01:00
|
|
|
|
2020-08-13 19:48:27 +02:00
|
|
|
bool llarpAdminDie(ReplyCallback callback) {
|
2020-08-24 12:44:48 +02:00
|
|
|
return invoke("llarp.halt", QJsonObject{}, callback);
|
2020-03-05 19:35:40 +01:00
|
|
|
}
|
2019-10-14 20:47:18 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2020-08-13 19:48:27 +02:00
|
|
|
lokimq::LokiMQ m_lmqClient;
|
|
|
|
std::optional<lokimq::ConnectionID> m_lmqConnection;
|
2019-10-14 20:47:18 +02:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // __LOKI_LOKINET_API_CLIENT_HPP__
|