FinceptTerminal is a modern finance application offering advanced market analytics, investment research, and economic data tools, designed for interactive exploration and data-driven decision-making in a user-friendly environment.
C++ ai-agentsalgorithmic-tradingbloomberg-terminalcppfinancefinancial-marketsfintechgood-first-issueinvestmentinvestment-researchmachine-learningopensourcepythonqtquantitative-financestock-markettrading
#include "services/notifications/providers/GotifyProvider.h"
#include "network/http/HttpClient.h"
#include <QJsonObject>
namespace fincept::notifications {
void GotifyProvider::load_fields(SettingsRepository& r, const QString& cat) {
server_url_ = get_str(r, cat + ".server_url");
app_token_ = get_secret(r, cat + ".app_token");
}
void GotifyProvider::save_fields(SettingsRepository& r, const QString& cat) {
r.set(cat + ".server_url", server_url_, cat);
set_secret(r, cat + ".app_token", app_token_, cat);
}
void GotifyProvider::send(const NotificationRequest& req, std::function<void(bool, QString)> cb) {
if (!is_configured()) {
cb(false, "Not configured");
return;
}
const int priority = [&]() -> int {
switch (req.level) {
case NotifLevel::Warning:
return 5;
case NotifLevel::Alert:
return 7;
case NotifLevel::Critical:
return 10;
default:
return 3;
}
}();
const QString base = server_url_.endsWith('/') ? server_url_.chopped(1) : server_url_;
// The app token rides in X-Gotify-Key, not the query string, so it can never
// end up in a URL log line.
const QString url = QString("%1/message").arg(base);
QJsonObject body;
body["title"] = req.title;
body["message"] = req.message;
body["priority"] = priority;
HttpClient::Headers headers;
headers.insert("X-Gotify-Key", app_token_.toUtf8());
HttpClient::instance().post(
url, body,
[cb](Result<QJsonDocument> res) {
if (res.is_err()) {
cb(false, QString::fromStdString(res.error()));
return;
}
cb(true, {});
},
nullptr, headers);
}
} // namespace fincept::notifications