-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.cpp
147 lines (134 loc) · 6.01 KB
/
api.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "api.h"
#include "env.h"
#include "qassert.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QMessageBox>
#include <QNetworkDiskCache>
#include <QNetworkReply>
#include <QSqlError>
#include <QSqlQuery>
#include <QStandardPaths>
Quote lookup(const QString& symbol)
{
QSqlQuery query;
query.prepare("SELECT * FROM quotes WHERE symbol = ? limit 1");
query.addBindValue(symbol);
query.exec();
if (query.first()) {
Q_ASSERT(symbol == query.value("symbol").toString());
Quote quote {
.symbol = symbol,
.response = query.value("response").toString(),
.open = query.value("open").toDouble(),
.high = query.value("high").toDouble(),
.low = query.value("low").toDouble(),
.price = query.value("price").toDouble(),
.volume = query.value("volume").toInt(),
.latest_trading_day = query.value("latest_trading_date").toString(),
.previous_close = query.value("previous_close").toDouble(),
.change = query.value("change").toDouble(),
.change_percent = query.value("change_percent").toString(),
.updated_at = query.value("updated_at").toString()
};
return quote;
}
return Quote {};
}
Quote parseQuoteResponse(const QString& response)
{
QJsonParseError error;
auto json = QJsonDocument::fromJson(response.toUtf8(), &error);
if (json.isNull()) {
qDebug() << "Json error:" << &error;
return Quote {};
}
QJsonObject global = json["Global Quote"].toObject();
auto symbol = global["01. symbol"].toString();
auto open = global["02. open"].toString();
auto high = global["03. high"].toString();
auto low = global["04. low"].toString();
auto price = global["05. price"].toString();
auto volume = global["06. volume"].toString();
auto latest_trading_day = global["07. latest trading day"].toString();
auto previous_close = global["08. previous close"].toString();
auto change = global["09. change"].toString();
auto change_percent = global["10. change percent"].toString();
return Quote { symbol, response, open.toDouble(), high.toDouble(), low.toDouble(), price.toDouble(),
volume.toInt(), latest_trading_day, previous_close.toDouble(), change.toDouble(), change_percent, "" /*updated_at*/ };
}
void save(const Quote& quote)
{
qDebug() << "-- save --";
QSqlQuery query;
query.prepare("INSERT INTO quotes (symbol, response, open, high, low, price, volume, latest_trading_date, previous_close, change, change_percent) "
"values (:symbol, :response, :open, :high, :low, :price, :volume, :latest_trading_date, :previous_close, :change, :change_percent) "
"ON CONFLICT(symbol) DO UPDATE "
"SET response=excluded.response, open=excluded.open, high=excluded.high, low=excluded.low, price=excluded.price, "
"volume=excluded.volume, latest_trading_date=excluded.latest_trading_date, previous_close=excluded.previous_close, change=excluded.change, change_percent=excluded.change_percent,"
"updated_at=CURRENT_TIMESTAMP");
query.bindValue(":symbol", quote.symbol);
query.bindValue(":response", quote.response);
query.bindValue(":open", quote.open);
query.bindValue(":high", quote.high);
query.bindValue(":low", quote.low);
query.bindValue(":price", quote.price);
query.bindValue(":volume", quote.volume);
query.bindValue(":latest_trading_date", quote.latest_trading_day);
query.bindValue(":previous_close", quote.previous_close);
query.bindValue(":change", quote.change);
query.bindValue(":change_percent", quote.change_percent);
if (!query.exec()) {
qWarning() << "save symbol error:" << query.lastError();
qWarning() << "quote:" << "e;
}
}
void Api::getSymbol(const QString& symbol, const std::function<void(Quote&&)>&& callback, CacheParam cache_param)
{
// define VANTAGE api key in env.h
static const auto GLOBAL_QUOTE_URL = u"https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=%1&apikey="_s VANTAGE;
if (cache_param == CacheParam::USE_CACHE) {
Quote find = lookup(symbol);
if (!find.symbol.isEmpty()) {
callback(std::move(find));
return;
}
}
qDebug() << "getSymbol initiating request for symbol:" << symbol;
auto request = QNetworkRequest { QUrl { GLOBAL_QUOTE_URL.arg(symbol) } };
request.setAttribute(QNetworkRequest::AutoDeleteReplyOnFinishAttribute, true);
// request.setAttribute(QNetworkRequest::CacheSaveControlAttribute, true);
// request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysCache);
auto* reply = getManager().get(request);
QObject::connect(reply, &QNetworkReply::errorOccurred, [=]([[maybe_unused]] QNetworkReply::NetworkError error) {
qWarning() << "NetworkError: " << reply->errorString();
});
QObject::connect(reply, &QNetworkReply::finished, [=]() {
if (reply->error() == QNetworkReply::NoError) {
auto fromCache = reply->attribute(QNetworkRequest::SourceIsFromCacheAttribute);
if (fromCache.toBool()) {
qInfo() << "\nREPLY IS from network CACHE!" << '\n';
}
const QString response = reply->readAll();
qInfo() << "QNetworkReply::finished response:" << response;
Quote quote = parseQuoteResponse(response);
save(quote);
callback(std::move(quote));
} else {
qInfo() << "finished but with error, not processing response.";
}
});
}
QNetworkAccessManager& Api::getManager()
{
static bool isSetup { false };
static QNetworkDiskCache diskCache;
static QNetworkAccessManager manager;
if (!isSetup) {
// /home/user/.cache/C14/Ragnar/cache
diskCache.setCacheDirectory(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + u"/cache/"_s);
manager.setCache(&diskCache);
isSetup = true;
}
return manager;
}