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
#pragma once
// QuantLibClient.h — Shared HTTP client for all QuantLib API calls.
// Used by QuantLibScreen (async) and MCP QuantLibTools (sync-wrapped).
#include "mcp/McpTypes.h"
#include <QJsonObject>
#include <QObject>
#include <functional>
class QNetworkAccessManager;
namespace fincept::services {
/// Callback type: delivers unwrapped data payload or error string.
/// On success: result.success == true, result.data has the payload.
/// On failure: result.success == false, result.error has the message.
using QuantLibCallback = std::function<void(mcp::ToolResult)>;
class QuantLibClient : public QObject {
Q_OBJECT
public:
static QuantLibClient& instance();
/// Async call — callback posted on Qt event loop (use from UI thread). A call
/// from any other thread is re-posted to the client's thread first.
void call(const QString& endpoint, const QJsonObject& body, QuantLibCallback callback);
// call_sync() was removed: it had zero call sites and blocked on an unbounded
// QEventLoop, so any future caller would have inherited a hang with no timeout.
// Use call() and continue from the callback. See the note in QuantLibClient.cpp.
/// GET-only endpoints (no request body).
static bool is_get_endpoint(const QString& endpoint);
/// Query-param endpoints: body fields become URL query params, POST with empty body.
static bool is_query_param_endpoint(const QString& endpoint);
QuantLibClient(const QuantLibClient&) = delete;
QuantLibClient& operator=(const QuantLibClient&) = delete;
private:
explicit QuantLibClient(QObject* parent = nullptr);
/// One long-lived QNAM for every call (created on first use, owned by this
/// object, lives on its thread): keep-alive TCP/TLS connections to the API
/// host are reused instead of a fresh handshake per QuantLib request.
QNetworkAccessManager* network();
/// Parses raw HTTP response bytes into a ToolResult, unwrapping the
/// {"success", "message", "data"} envelope and handling 422/4xx errors.
static mcp::ToolResult parse_response(int http_status, const QByteArray& raw);
QNetworkAccessManager* nam_ = nullptr;
};
} // namespace fincept::services